(tableNames []string)
| 1390 | } |
| 1391 | |
| 1392 | func (d *PostgresDatabase) getIndexDefsForTables(tableNames []string) (map[string][]string, error) { |
| 1393 | // Exclude indexes that are implicitly created for primary keys or unique constraints or exclusion constraints. |
| 1394 | const query = `WITH |
| 1395 | exclude_constraints AS ( |
| 1396 | SELECT con.conname AS name, nsp.nspname || '.' || cls.relname AS qualified_table_name |
| 1397 | FROM pg_constraint con |
| 1398 | JOIN pg_namespace nsp ON nsp.oid = con.connamespace |
| 1399 | JOIN pg_class cls ON cls.oid = con.conrelid |
| 1400 | WHERE con.contype IN ('p', 'u', 'x') |
| 1401 | AND nsp.nspname || '.' || cls.relname = ANY($1::text[]) |
| 1402 | ) |
| 1403 | SELECT schemaname || '.' || tablename AS qualified_table_name, indexName, indexdef |
| 1404 | FROM pg_indexes |
| 1405 | WHERE schemaname || '.' || tablename = ANY($1::text[]) |
| 1406 | AND indexName NOT IN ( |
| 1407 | SELECT name FROM exclude_constraints |
| 1408 | WHERE qualified_table_name = schemaname || '.' || tablename |
| 1409 | ) |
| 1410 | ORDER BY schemaname, tablename, indexdef |
| 1411 | ` |
| 1412 | |
| 1413 | rows, err := d.db.Query(query, pq.Array(tableNames)) |
| 1414 | if err != nil { |
| 1415 | return nil, err |
| 1416 | } |
| 1417 | defer rows.Close() |
| 1418 | |
| 1419 | result := make(map[string][]string, len(tableNames)) |
| 1420 | for rows.Next() { |
| 1421 | var tableName, indexName, indexdef string |
| 1422 | err = rows.Scan(&tableName, &indexName, &indexdef) |
| 1423 | if err != nil { |
| 1424 | return nil, err |
| 1425 | } |
| 1426 | indexName = strings.Trim(indexName, `" `) |
| 1427 | result[tableName] = append(result[tableName], indexdef) |
| 1428 | } |
| 1429 | return result, nil |
| 1430 | } |
| 1431 | |
| 1432 | // refs: https://gist.github.com/PickledDragon/dd41f4e72b428175354d |
| 1433 | func (d *PostgresDatabase) getForeignDefsForTables(tableNames []string) (map[string][]string, error) { |
no test coverage detected