Skip to content

Commit

Permalink
#6 - Update SQL queries used by postgres connector to fetch database …
Browse files Browse the repository at this point in the history
…structure.
  • Loading branch information
spo committed May 7, 2024
1 parent 7c80b8d commit 2984a7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 3 additions & 3 deletions app/Services/PostgreSQLConnectorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,15 @@ public function getGrants(): array

// Exclude row which contains the password.
foreach ($this->connection->select('
SELECT concat(table_schema, \'.\', table_name) as table_name, group_concat(privilege_type)
SELECT concat(table_schema, \'.\', table_name) as table_name, string_agg(privilege_type, \', \')
FROM information_schema.role_table_grants
WHERE grantee = \'' . $this->confConnector->username . '\'
AND table_schema !~ \'information_schema\'
AND table_schema !~ \'^pg_\'
GROUP BY table_schema, table_name
ORDER BY table_name', []) as $grants) {

$all_grants[] = '<strong>' . $grants->table_name . '</strong>: ' . $grants->group_concat;
$all_grants[] = '<strong>' . $grants->table_name . '</strong>: ' . $grants->string_agg;

}

Expand Down Expand Up @@ -962,7 +962,7 @@ private function _getPrimaryAndForeignKeys(string $table_schema): array

$stmt = $this->connection->getPdo()
->prepare("
SELECT GROUP_CONCAT(kc.column_name) AS column_names, tc.table_name, tc.constraint_name, ccu.column_name as referenced_column_name, ccu.table_name as referenced_table_name
SELECT string_agg(kc.column_name, ',') AS column_names, tc.table_name, tc.constraint_name, ccu.column_name as referenced_column_name, ccu.table_name as referenced_table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kc ON kc.table_name = tc.table_name AND kc.table_schema = tc.table_schema AND kc.constraint_name = tc.constraint_name
LEFT JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema AND tc.constraint_type = 'FOREIGN KEY'
Expand Down
9 changes: 4 additions & 5 deletions dev/connector/postgres.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Get grants
SELECT concat(table_schema, '.', table_name) as table_name, group_concat(privilege_type)
SELECT concat(table_schema, '.', table_name) as table_name, string_agg(privilege_type, ', ')
FROM information_schema.role_table_grants
WHERE grantee = 'localuser'
WHERE grantee = 'redmine'
AND table_schema !~ 'information_schema'
AND table_schema !~ '^pg_'
GROUP BY table_schema, table_name
Expand All @@ -28,13 +28,12 @@ WHERE c.table_schema = 'public'
ORDER BY c.table_schema, c.table_name;

-- Get primary and foreign keys
SELECT GROUP_CONCAT(kc.column_name) AS column_names, tc.table_name, tc.constraint_name, ccu.column_name as referenced_column_name, ccu.table_name as referenced_table_name
SELECT string_agg(kc.column_name, ',') AS column_names, tc.table_name, tc.constraint_name, ccu.column_name as referenced_column_name, ccu.table_name as referenced_table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kc ON kc.table_name = tc.table_name AND kc.table_schema = tc.table_schema AND kc.constraint_name = tc.constraint_name
LEFT JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema AND tc.constraint_type = 'FOREIGN KEY'
WHERE tc.table_schema = 'public'
AND tc.constraint_type IN ('PRIMARY KEY', 'FOREIGN KEY')
AND tc.table_name = 'film_actor'
AND kc.ordinal_position IS NOT NULL
GROUP BY tc.table_schema, tc.table_name, tc.constraint_name, referenced_column_name, referenced_table_name
;
Expand All @@ -51,7 +50,7 @@ FROM pg_index AS pi
JOIN pg_class as pc ON pc.oid = pi.indexrelid
JOIN pg_am as pa ON pc.relam = pa.oid
JOIN pg_namespace as pn ON pn.oid = pc.relnamespace AND pn.nspname = ANY (current_schemas(false))
WHERE pc.relname = 'idx_fk_film_id';
WHERE pn.nspname = 'public'
;


Expand Down

0 comments on commit 2984a7c

Please sign in to comment.