Skip to content

Commit 97b8469

Browse files
Fix workflow Postgres enum schemas (#2705)
1 parent 65f1dbc commit 97b8469

4 files changed

Lines changed: 92 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@workflow/world-postgres': minor
3+
---
4+
5+
Move Workflow Postgres enum types into the workflow schema.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 $$;

‎packages/world-postgres/src/drizzle/migrations/meta/_journal.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@
106106
"when": 1780185600000,
107107
"tag": "0014_add_attr_set_event_unique_index",
108108
"breakpoints": true
109+
},
110+
{
111+
"idx": 15,
112+
"version": "7",
113+
"when": 1782691200000,
114+
"tag": "0015_move_enums_to_workflow_schema",
115+
"breakpoints": true
109116
}
110117
]
111118
}

‎packages/world-postgres/src/drizzle/schema.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
integer,
1818
/** @deprecated: use Cbor instead */
1919
jsonb,
20-
pgEnum,
2120
pgSchema,
2221
primaryKey,
2322
text,
@@ -27,21 +26,23 @@ import {
2726
} from 'drizzle-orm/pg-core';
2827
import { Cbor, type Cborized } from './cbor.js';
2928

29+
export const schema = pgSchema('workflow');
30+
3031
function mustBeMoreThanOne<T>(t: T[]) {
3132
return t as [T, ...T[]];
3233
}
3334

34-
export const workflowRunStatus = pgEnum(
35+
export const workflowRunStatus = schema.enum(
3536
'status',
3637
mustBeMoreThanOne(WorkflowRunStatusSchema.options)
3738
);
3839

39-
export const stepStatus = pgEnum(
40+
export const stepStatus = schema.enum(
4041
'step_status',
4142
mustBeMoreThanOne(StepStatusSchema.options)
4243
);
4344

44-
export const waitStatus = pgEnum(
45+
export const waitStatus = schema.enum(
4546
'wait_status',
4647
mustBeMoreThanOne(WaitStatusSchema.options)
4748
);
@@ -61,8 +62,6 @@ type DrizzlishOfType<T extends object> = {
6162
*/
6263
export type SerializedContent = any[];
6364

64-
export const schema = pgSchema('workflow');
65-
6665
export const runs = schema.table(
6766
'workflow_runs',
6867
{

0 commit comments

Comments
 (0)