33 and_ ,
44 bindparam ,
55 column ,
6+ exists ,
67 func ,
78 literal ,
89 table ,
910 text ,
1011 union ,
1112)
12- from sqlalchemy .dialects .postgresql import ARRAY , CHAR
13+ from sqlalchemy .dialects .postgresql import ARRAY , CHAR , REGCLASS
1314
1415from sqlalchemy_declarative_extensions .sqlalchemy import select
1516
3940 column ("datname" ),
4041)
4142
43+ pg_depend = table (
44+ "pg_depend" ,
45+ column ("objid" ),
46+ column ("classid" ),
47+ column ("deptype" ),
48+ )
49+
50+ pg_extension = table (
51+ "pg_extension" ,
52+ column ("oid" ),
53+ column ("extname" ),
54+ column ("extversion" ),
55+ column ("extconfig" ),
56+ )
57+
4258pg_namespace = table (
4359 "pg_namespace" ,
4460 column ("oid" ),
@@ -146,11 +162,24 @@ def _schema_not_pg(column=pg_namespace.c.nspname):
146162 )
147163
148164
165+ def _schema_not_from_extension (namespace_oid_column = pg_namespace .c .oid ):
166+ return _not_from_extension (namespace_oid_column , "pg_namespace" )
167+
168+
169+ def _not_from_extension (obj_id_col , class_name ):
170+ return ~ exists ().where (pg_depend .c .objid == obj_id_col ).where (
171+ pg_depend .c .classid == literal (class_name ).cast (REGCLASS )
172+ ).where (pg_depend .c .deptype == literal ("e" ))
173+
174+
149175_schema_not_public = pg_namespace .c .nspname != "public"
150176_table_not_pg = pg_class .c .relname .notlike ("pg_%" )
151177
152178schemas_query = (
153- select (pg_namespace .c .nspname ).where (_schema_not_pg ()).where (_schema_not_public )
179+ select (pg_namespace .c .nspname )
180+ .where (_schema_not_pg ())
181+ .where (_schema_not_public )
182+ .where (_schema_not_from_extension ())
154183)
155184
156185
@@ -160,6 +189,10 @@ def _schema_not_pg(column=pg_namespace.c.nspname):
160189 .where (_schema_not_public )
161190)
162191
192+ extensions_query = select (
193+ pg_extension .c .extname .label ("name" ), pg_extension .c .extversion .label ("version" )
194+ )
195+
163196schema_exists_query = text (
164197 "SELECT schema_name FROM information_schema.schemata WHERE schema_name = :schema"
165198)
@@ -238,26 +271,25 @@ def _schema_not_pg(column=pg_namespace.c.nspname):
238271 )
239272 .where (_table_not_pg )
240273 .where (_schema_not_pg ())
274+ .where (_schema_not_from_extension ())
241275)
242276
243- views_query = union (
277+ views_query = (
244278 select (
245- pg_views .c .schemaname .label ("schema" ),
246- pg_views .c .viewname .label ("name" ),
247- pg_views . c . definition .label ("definition" ),
248- literal (False ).label ("materialized" ),
279+ pg_namespace .c .nspname .label ("schema" ),
280+ pg_class .c .relname .label ("name" ),
281+ func . pg_get_viewdef ( pg_class . c . oid ) .label ("definition" ),
282+ ( pg_class . c . relkind == literal ("m" ) ).label ("materialized" ),
249283 )
250- .where (_schema_not_pg (pg_views .c .schemaname ))
251- .where (pg_views .c .viewname .notin_ (["pg_stat_statements" ])),
252- select (
253- pg_matviews .c .schemaname .label ("schema" ),
254- pg_matviews .c .matviewname .label ("name" ),
255- pg_matviews .c .definition .label ("definition" ),
256- literal (True ).label ("materialized" ),
257- ).where (_schema_not_pg (pg_matviews .c .schemaname )),
284+ .select_from (
285+ pg_class .join (pg_namespace , pg_class .c .relnamespace == pg_namespace .c .oid )
286+ )
287+ .where (pg_class .c .relkind .in_ (["v" , "m" ]))
288+ .where (_schema_not_pg (pg_namespace .c .nspname ))
289+ .where (_schema_not_from_extension ())
290+ .where (_not_from_extension (pg_class .c .oid , "pg_class" ))
258291)
259292
260-
261293views_subquery = views_query .cte ()
262294view_query = (
263295 select (views_subquery )
@@ -283,6 +315,8 @@ def _schema_not_pg(column=pg_namespace.c.nspname):
283315 )
284316 .where (pg_namespace .c .nspname .notin_ (["pg_catalog" , "information_schema" ]))
285317 .where (pg_proc .c .prokind == "p" )
318+ .where (_schema_not_from_extension ())
319+ .where (_not_from_extension (pg_proc .c .oid , "pg_proc" ))
286320)
287321
288322functions_query = (
@@ -302,6 +336,7 @@ def _schema_not_pg(column=pg_namespace.c.nspname):
302336 )
303337 .where (pg_namespace .c .nspname .notin_ (["pg_catalog" , "information_schema" ]))
304338 .where (pg_proc .c .prokind != "p" )
339+ .where (_not_from_extension (pg_proc .c .oid , "pg_proc" ))
305340)
306341
307342
@@ -335,4 +370,6 @@ def _schema_not_pg(column=pg_namespace.c.nspname):
335370 .join (proc_nsp , pg_proc .c .pronamespace == proc_nsp .c .oid )
336371 )
337372 .where (pg_trigger .c .tgisinternal .is_ (False ))
373+ .where (_schema_not_from_extension ())
374+ .where (_not_from_extension (pg_trigger .c .oid , "pg_trigger" ))
338375)
0 commit comments