Skip to content

Commit 38c8a86

Browse files
committed
fix: obscure policies CTE bug
1 parent 3a96614 commit 38c8a86

File tree

3 files changed

+10
-19
lines changed

3 files changed

+10
-19
lines changed

src/lib/PostgresMetaRoles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ WITH roles AS (${
3535
})
3636
SELECT
3737
*,
38-
${coalesceRowsToArray('grants', 'SELECT * FROM grants WHERE grants.grantee = roles.name')}
38+
${coalesceRowsToArray('grants', 'grants.grantee = roles.name')}
3939
FROM
4040
roles`
4141
if (limit) {

src/lib/PostgresMetaTables.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,13 @@ WITH tables AS (${tablesSql}),
211211
relationships AS (${relationshipsSql})
212212
SELECT
213213
*,
214-
${coalesceRowsToArray('columns', 'SELECT * FROM columns WHERE columns.table_id = tables.id')},
215-
${coalesceRowsToArray('grants', 'SELECT * FROM grants WHERE grants.table_id = tables.id')},
216-
${coalesceRowsToArray('policies', 'SELECT * FROM policies WHERE policies.table_id = tables.id')},
217-
${coalesceRowsToArray(
218-
'primary_keys',
219-
'SELECT * FROM primary_keys WHERE primary_keys.table_id = tables.id'
220-
)},
214+
${coalesceRowsToArray('columns', 'columns.table_id = tables.id')},
215+
${coalesceRowsToArray('grants', 'grants.table_id = tables.id')},
216+
${coalesceRowsToArray('policies', 'policies.table_id = tables.id')},
217+
${coalesceRowsToArray('primary_keys', 'primary_keys.table_id = tables.id')},
221218
${coalesceRowsToArray(
222219
'relationships',
223-
`SELECT
224-
*
225-
FROM
226-
relationships
227-
WHERE
228-
(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
220+
`(relationships.source_schema = tables.schema AND relationships.source_table_name = tables.name)
229221
OR (relationships.target_table_schema = tables.schema AND relationships.target_table_name = tables.name)`
230222
)}
231223
FROM tables`

src/lib/helpers.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
export const coalesceRowsToArray = (source: string, joinQuery: string) => {
2-
// Note that array_to_json(array_agg(row_to_json())) seems to perform better than json_agg
1+
export const coalesceRowsToArray = (source: string, filter: string) => {
32
return `
43
COALESCE(
54
(
65
SELECT
7-
array_to_json(array_agg(row_to_json(${source})))
6+
array_agg(row_to_json(${source})) FILTER (WHERE ${filter})
87
FROM
9-
( ${joinQuery} ) ${source}
8+
${source}
109
),
11-
'[]'
10+
'{}'
1211
) AS ${source}`
1312
}

0 commit comments

Comments
 (0)