Skip to content

Commit

Permalink
Updated relations.sql to recognize tables with enum_type[] arrays and…
Browse files Browse the repository at this point in the history
… manage dependencies correctly so those enums can be changed; I think this fixes djrobstep/migra#203 on the related migra package
  • Loading branch information
gjbadros committed May 27, 2023
1 parent 066262d commit 59c7ab0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions schemainspect/pg/sql/relations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ with extension_oids as (
-- SKIP_INTERNAL and n.nspname not like 'pg_temp_%' and n.nspname not like 'pg_toast_temp_%'
ORDER BY 1, 2
),
enums_arrays as (
SELECT
t.oid as enum_array_oid,
n.nspname as "schema",
substring(t.typname from 2) as name
FROM pg_catalog.pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
left outer join extension_oids e
on t.oid = e.objid
WHERE
(t.typcategory = 'A' and t.typelem in (select enum_oid from enums)) and e.objid is null
-- SKIP_INTERNAL and n.nspname not in ('pg_catalog', 'information_schema', 'pg_toast')
-- SKIP_INTERNAL and n.nspname not like 'pg_temp_%' and n.nspname not like 'pg_toast_temp_%'
ORDER BY 1, 2

),
r as (
select
c.relname as name,
Expand Down Expand Up @@ -82,9 +98,13 @@ select
pg_get_expr(ad.adbin, ad.adrelid) as defaultdef,
r.oid as oid,
format_type(atttypid, atttypmod) AS datatypestring,
e.enum_oid is not null as is_enum,
e.name as enum_name,
e.schema as enum_schema,
(e.enum_oid is not null or ea.enum_array_oid is not null) as is_enum,
(case when e.enum_oid is not null then e.name
when ea.enum_array_oid is not null then ea.name
end) as enum_name,
(case when e.enum_oid is not null then e.schema
when ea.enum_array_oid is not null then ea.schema
end) as enum_schema,
pg_catalog.obj_description(r.oid) as comment,
r.parent_table,
r.partition_def,
Expand All @@ -102,7 +122,9 @@ FROM
and a.attnum = ad.adnum
left join enums e
on a.atttypid = e.enum_oid
where a.attisdropped is not true
left join enums_arrays ea
on a.atttypid = ea.enum_array_oid
where a.attisdropped is not true -- FOR TESTING: and a.attname like 'cap%'
-- SKIP_INTERNAL and r.schema not in ('pg_catalog', 'information_schema', 'pg_toast')
-- SKIP_INTERNAL and r.schema not like 'pg_temp_%' and r.schema not like 'pg_toast_temp_%'
order by relationtype, r.schema, r.name, position_number;

0 comments on commit 59c7ab0

Please sign in to comment.