|
| 1 | +DO $$ |
| 2 | +DECLARE |
| 3 | + enum_name text; |
| 4 | + public_enum regtype; |
| 5 | + workflow_enum regtype; |
| 6 | + is_used_by_workflow_columns boolean; |
| 7 | + has_dependents_outside_workflow_tables boolean; |
| 8 | +BEGIN |
| 9 | + FOREACH enum_name IN ARRAY ARRAY['status', 'step_status', 'wait_status'] LOOP |
| 10 | + public_enum := to_regtype(format('public.%I', enum_name)); |
| 11 | + workflow_enum := to_regtype(format('workflow.%I', enum_name)); |
| 12 | + |
| 13 | + -- Nothing to migrate when the legacy public enum does not exist, or when |
| 14 | + -- the enum has already been moved to the workflow schema. |
| 15 | + IF public_enum IS NULL OR workflow_enum IS NOT NULL THEN |
| 16 | + CONTINUE; |
| 17 | + END IF; |
| 18 | + |
| 19 | + -- Only move enums that are actually used by workflow table columns. |
| 20 | + -- pg_depend has an index on referenced objects, so this lookup is based |
| 21 | + -- on the enum type OID instead of scanning user data or workflow rows. |
| 22 | + SELECT EXISTS ( |
| 23 | + SELECT 1 |
| 24 | + FROM pg_depend dependency |
| 25 | + JOIN pg_class dependent_table |
| 26 | + ON dependency.classid = 'pg_class'::regclass AND dependency.objid = dependent_table.oid |
| 27 | + JOIN pg_namespace dependent_table_schema |
| 28 | + ON dependent_table_schema.oid = dependent_table.relnamespace |
| 29 | + WHERE dependency.refclassid = 'pg_type'::regclass |
| 30 | + AND dependency.refobjid = public_enum::oid |
| 31 | + AND dependency.objsubid > 0 |
| 32 | + AND dependency.deptype != 'i' |
| 33 | + AND dependent_table_schema.nspname = 'workflow' |
| 34 | + ) INTO is_used_by_workflow_columns; |
| 35 | + |
| 36 | + IF NOT is_used_by_workflow_columns THEN |
| 37 | + CONTINUE; |
| 38 | + END IF; |
| 39 | + |
| 40 | + /* |
| 41 | + pg_depend is Postgres' dependency graph. |
| 42 | +
|
| 43 | + We use it here as a safety check before moving the enum object itself. |
| 44 | + A normal workflow-owned enum has only workflow table-column dependents, |
| 45 | + so ALTER TYPE ... SET SCHEMA is a fast metadata change. |
| 46 | +
|
| 47 | + If anything outside workflow tables also depends on this enum, moving it |
| 48 | + would silently rename that user's public enum to workflow.<enum_name>. |
| 49 | + In that case, leave the enum in public, warn, and continue so this cleanup |
| 50 | + migration does not block later migrations. |
| 51 | + */ |
| 52 | + SELECT EXISTS ( |
| 53 | + SELECT 1 |
| 54 | + FROM pg_depend dependency |
| 55 | + LEFT JOIN pg_class dependent_table |
| 56 | + ON dependency.classid = 'pg_class'::regclass AND dependency.objid = dependent_table.oid |
| 57 | + LEFT JOIN pg_namespace dependent_table_schema |
| 58 | + ON dependent_table_schema.oid = dependent_table.relnamespace |
| 59 | + WHERE dependency.refclassid = 'pg_type'::regclass |
| 60 | + AND dependency.refobjid = public_enum::oid |
| 61 | + AND dependency.deptype != 'i' |
| 62 | + AND NOT ( |
| 63 | + dependency.classid = 'pg_class'::regclass |
| 64 | + AND dependent_table_schema.nspname = 'workflow' |
| 65 | + ) |
| 66 | + ) INTO has_dependents_outside_workflow_tables; |
| 67 | + |
| 68 | + IF has_dependents_outside_workflow_tables THEN |
| 69 | + RAISE WARNING 'Skipping move of public.% to workflow schema because objects outside workflow tables depend on it', enum_name; |
| 70 | + CONTINUE; |
| 71 | + END IF; |
| 72 | + |
| 73 | + EXECUTE format('ALTER TYPE public.%I SET SCHEMA workflow', enum_name); |
| 74 | + END LOOP; |
| 75 | +END $$; |
0 commit comments