refs: https://gist.github.com/PickledDragon/dd41f4e72b428175354d
(tableNames []string)
| 1431 | |
| 1432 | // refs: https://gist.github.com/PickledDragon/dd41f4e72b428175354d |
| 1433 | func (d *PostgresDatabase) getForeignDefsForTables(tableNames []string) (map[string][]string, error) { |
| 1434 | var periodCol string |
| 1435 | if d.supportsConperiod() { |
| 1436 | periodCol = "c.conperiod" |
| 1437 | } else { |
| 1438 | periodCol = "false" |
| 1439 | } |
| 1440 | query := fmt.Sprintf(`SELECT |
| 1441 | nc.nspname AS constraint_schema, |
| 1442 | n1.nspname AS table_schema, |
| 1443 | c.conname AS constraint_name, |
| 1444 | r1.relname AS table_name, |
| 1445 | a1.attname AS column_name, |
| 1446 | n2.nspname AS foreign_table_schema, |
| 1447 | r2.relname AS foreign_table_name, |
| 1448 | a2.attname AS foreign_column_name, |
| 1449 | CASE c.confupdtype |
| 1450 | WHEN 'c' THEN 'CASCADE' |
| 1451 | WHEN 'n' THEN 'SET NULL' |
| 1452 | WHEN 'd' THEN 'SET DEFAULT' |
| 1453 | WHEN 'r' THEN 'RESTRICT' |
| 1454 | WHEN 'a' THEN 'NO ACTION' |
| 1455 | END AS foreign_update_rule, |
| 1456 | CASE c.confdeltype |
| 1457 | WHEN 'c' THEN 'CASCADE' |
| 1458 | WHEN 'n' THEN 'SET NULL' |
| 1459 | WHEN 'd' THEN 'SET DEFAULT' |
| 1460 | WHEN 'r' THEN 'RESTRICT' |
| 1461 | WHEN 'a' THEN 'NO ACTION' |
| 1462 | END AS foreign_delete_rule, |
| 1463 | c.condeferrable AS deferrable, |
| 1464 | c.condeferred AS initially_deferred, |
| 1465 | %s AS period |
| 1466 | FROM pg_constraint AS c |
| 1467 | INNER JOIN pg_namespace AS nc ON nc.oid = c.connamespace |
| 1468 | INNER JOIN pg_class AS r1 ON r1.oid = c.conrelid |
| 1469 | INNER JOIN pg_class AS r2 ON r2.oid = c.confrelid |
| 1470 | INNER JOIN pg_namespace AS n1 ON n1.oid = r1.relnamespace |
| 1471 | INNER JOIN pg_namespace AS n2 ON n2.oid = r2.relnamespace |
| 1472 | CROSS JOIN UNNEST(c.conkey, c.confkey) with ordinality AS k(key1, key2, ordinality) |
| 1473 | INNER JOIN pg_attribute AS a1 |
| 1474 | ON a1.attrelid = c.conrelid |
| 1475 | AND a1.attnum = k.key1 |
| 1476 | INNER JOIN pg_attribute AS a2 |
| 1477 | ON a2.attrelid = c.confrelid |
| 1478 | AND a2.attnum = k.key2 |
| 1479 | WHERE c.contype = 'f' AND n1.nspname || '.' || r1.relname = ANY($1::text[]) |
| 1480 | ORDER BY constraint_schema, constraint_name, k.ordinality |
| 1481 | `, periodCol) |
| 1482 | |
| 1483 | rows, err := d.db.Query(query, pq.Array(tableNames)) |
| 1484 | if err != nil { |
| 1485 | return nil, err |
| 1486 | } |
| 1487 | defer rows.Close() |
| 1488 | |
| 1489 | type identifier struct { |
| 1490 | schema, name string |
no test coverage detected