|
1 | | -from sqlalchemy import column, select, String, table, text |
| 1 | +from sqlalchemy import and_, column, literal, select, String, table, text, union |
2 | 2 | from sqlalchemy.dialects.postgresql import ARRAY |
3 | 3 |
|
4 | 4 | pg_class = table( |
|
15 | 15 | "pg_namespace", |
16 | 16 | column("oid"), |
17 | 17 | column("nspname"), |
| 18 | + column("nspowner"), |
| 19 | + column("nspacl"), |
18 | 20 | ) |
19 | 21 |
|
20 | 22 | pg_roles = table( |
|
55 | 57 | """ |
56 | 58 | ) |
57 | 59 |
|
| 60 | +_schema_not_pg = and_( |
| 61 | + pg_namespace.c.nspname != "information_schema", |
| 62 | + pg_namespace.c.nspname.not_like("pg_%"), |
| 63 | +) |
| 64 | +_schema_not_public = pg_namespace.c.nspname != "public" |
| 65 | +_table_not_pg = pg_class.c.relname.not_like("pg_%") |
| 66 | + |
58 | 67 | schemas_query = ( |
59 | | - select(pg_namespace.c.nspname) |
60 | | - .where(pg_namespace.c.nspname.not_in(["information_schema", "public"])) |
61 | | - .where(pg_namespace.c.nspname.not_like("pg_%")) |
| 68 | + select(pg_namespace.c.nspname).where(_schema_not_pg).where(_schema_not_public) |
62 | 69 | ) |
63 | 70 |
|
64 | 71 |
|
|
78 | 85 | ) |
79 | 86 | ) |
80 | 87 |
|
81 | | -object_acl_query = ( |
| 88 | +object_acl_query = union( |
82 | 89 | select( |
83 | 90 | pg_namespace.c.nspname.label("schema"), |
84 | 91 | pg_class.c.relname.label("name"), |
|
92 | 99 | ) |
93 | 100 | ) |
94 | 101 | .where(pg_class.c.relkind.in_(["r", "S", "f", "n", "T"])) |
95 | | - .where(pg_class.c.relname.not_like("pg_%")) |
96 | | - .where(pg_namespace.c.nspname != "information_schema") |
| 102 | + .where(_table_not_pg) |
| 103 | + .where(_schema_not_pg), |
| 104 | + select( |
| 105 | + literal(None).label("schema"), |
| 106 | + pg_namespace.c.nspname.label("name"), |
| 107 | + literal("n").label("relkind"), |
| 108 | + pg_authid.c.rolname.label("owner"), |
| 109 | + pg_namespace.c.nspacl.cast(ARRAY(String)), |
| 110 | + ) |
| 111 | + .select_from( |
| 112 | + pg_namespace.join(pg_authid, pg_namespace.c.nspowner == pg_authid.c.oid) |
| 113 | + ) |
| 114 | + .where(_schema_not_pg) |
| 115 | + .where(_schema_not_public), |
97 | 116 | ) |
98 | 117 |
|
99 | 118 | objects_query = ( |
|
106 | 125 | pg_class.join(pg_namespace, pg_class.c.relnamespace == pg_namespace.c.oid) |
107 | 126 | ) |
108 | 127 | .where(pg_class.c.relkind.in_(["r", "S", "f", "n", "T"])) |
109 | | - .where(pg_class.c.relname.not_like("pg_%")) |
110 | | - .where(pg_namespace.c.nspname != "information_schema") |
| 128 | + .where(_table_not_pg) |
| 129 | + .where(_schema_not_pg) |
111 | 130 | ) |
0 commit comments