@supabase/pg-delta 1.0.0-alpha.16 → 1.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/expand-replace-dependencies.js +27 -0
- package/dist/core/objects/procedure/procedure.diff.js +33 -20
- package/dist/core/objects/table/table.diff.js +1 -0
- package/dist/core/objects/table/table.model.d.ts +4 -0
- package/dist/core/objects/table/table.model.js +2 -0
- package/dist/core/objects/trigger/trigger.diff.js +7 -2
- package/dist/core/objects/trigger/trigger.model.d.ts +1 -1
- package/dist/core/objects/trigger/trigger.model.js +5 -1
- package/dist/core/plan/sql-format/fixtures.js +4 -0
- package/dist/core/post-diff-cycle-breaking.d.ts +7 -0
- package/dist/core/post-diff-cycle-breaking.js +69 -3
- package/package.json +1 -1
- package/src/core/expand-replace-dependencies.test.ts +118 -0
- package/src/core/expand-replace-dependencies.ts +26 -0
- package/src/core/objects/procedure/procedure.diff.test.ts +100 -2
- package/src/core/objects/procedure/procedure.diff.ts +39 -21
- package/src/core/objects/table/changes/table.alter.test.ts +1 -0
- package/src/core/objects/table/table.diff.test.ts +102 -0
- package/src/core/objects/table/table.diff.ts +1 -0
- package/src/core/objects/table/table.model.ts +2 -0
- package/src/core/objects/trigger/trigger.diff.ts +7 -2
- package/src/core/objects/trigger/trigger.model.ts +5 -1
- package/src/core/plan/sql-format/fixtures.ts +4 -0
- package/src/core/post-diff-cycle-breaking.test.ts +142 -0
- package/src/core/post-diff-cycle-breaking.ts +83 -2
|
@@ -144,12 +144,13 @@ describe.concurrent("procedure.diff", () => {
|
|
|
144
144
|
expect(changes[0]).toBeInstanceOf(AlterProcedureSetParallel);
|
|
145
145
|
});
|
|
146
146
|
|
|
147
|
-
test("create or replace when non-alterable property changes", () => {
|
|
147
|
+
test("create or replace when body-only non-alterable property changes", () => {
|
|
148
|
+
// Changing only the language (or source) is OR-REPLACE-safe: no DROP needed.
|
|
148
149
|
const main = new Procedure(base);
|
|
149
150
|
const branch = new Procedure({
|
|
150
151
|
...base,
|
|
151
|
-
return_type: "text",
|
|
152
152
|
language: "plpgsql",
|
|
153
|
+
source_code: "BEGIN RETURN 1; END",
|
|
153
154
|
});
|
|
154
155
|
const changes = diffProcedures(
|
|
155
156
|
testContext,
|
|
@@ -158,6 +159,82 @@ describe.concurrent("procedure.diff", () => {
|
|
|
158
159
|
);
|
|
159
160
|
expect(changes).toHaveLength(1);
|
|
160
161
|
expect(changes[0]).toBeInstanceOf(CreateProcedure);
|
|
162
|
+
expect((changes[0] as CreateProcedure).orReplace).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("drop + create when return type changes", () => {
|
|
166
|
+
// `CREATE OR REPLACE FUNCTION` cannot change the return type.
|
|
167
|
+
const main = new Procedure(base);
|
|
168
|
+
const branch = new Procedure({ ...base, return_type: "text" });
|
|
169
|
+
const changes = diffProcedures(
|
|
170
|
+
testContext,
|
|
171
|
+
{ [main.stableId]: main },
|
|
172
|
+
{ [branch.stableId]: branch },
|
|
173
|
+
);
|
|
174
|
+
expect(changes[0]).toBeInstanceOf(DropProcedure);
|
|
175
|
+
expect(changes[1]).toBeInstanceOf(CreateProcedure);
|
|
176
|
+
expect((changes[1] as CreateProcedure).orReplace).toBe(false);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("drop + create when parameter name changes but type is identical", () => {
|
|
180
|
+
// Same argument_types means same stableId (altered path), but
|
|
181
|
+
// `CREATE OR REPLACE` rejects changing an IN parameter's name.
|
|
182
|
+
const main = new Procedure({
|
|
183
|
+
...base,
|
|
184
|
+
argument_count: 1,
|
|
185
|
+
argument_names: ["p"],
|
|
186
|
+
argument_types: ["int4"],
|
|
187
|
+
all_argument_types: ["int4"],
|
|
188
|
+
argument_modes: ["i"],
|
|
189
|
+
});
|
|
190
|
+
const branch = new Procedure({
|
|
191
|
+
...base,
|
|
192
|
+
argument_count: 1,
|
|
193
|
+
argument_names: ["renamed"],
|
|
194
|
+
argument_types: ["int4"],
|
|
195
|
+
all_argument_types: ["int4"],
|
|
196
|
+
argument_modes: ["i"],
|
|
197
|
+
});
|
|
198
|
+
const changes = diffProcedures(
|
|
199
|
+
testContext,
|
|
200
|
+
{ [main.stableId]: main },
|
|
201
|
+
{ [branch.stableId]: branch },
|
|
202
|
+
);
|
|
203
|
+
expect(changes[0]).toBeInstanceOf(DropProcedure);
|
|
204
|
+
expect(changes[1]).toBeInstanceOf(CreateProcedure);
|
|
205
|
+
expect((changes[1] as CreateProcedure).orReplace).toBe(false);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("drop + create when a parameter default is removed", () => {
|
|
209
|
+
// `CREATE OR REPLACE` rejects removing parameter defaults.
|
|
210
|
+
const main = new Procedure({
|
|
211
|
+
...base,
|
|
212
|
+
argument_count: 1,
|
|
213
|
+
argument_default_count: 1,
|
|
214
|
+
argument_names: ["p"],
|
|
215
|
+
argument_types: ["int4"],
|
|
216
|
+
all_argument_types: ["int4"],
|
|
217
|
+
argument_modes: ["i"],
|
|
218
|
+
argument_defaults: "0",
|
|
219
|
+
});
|
|
220
|
+
const branch = new Procedure({
|
|
221
|
+
...base,
|
|
222
|
+
argument_count: 1,
|
|
223
|
+
argument_default_count: 0,
|
|
224
|
+
argument_names: ["p"],
|
|
225
|
+
argument_types: ["int4"],
|
|
226
|
+
all_argument_types: ["int4"],
|
|
227
|
+
argument_modes: ["i"],
|
|
228
|
+
argument_defaults: null,
|
|
229
|
+
});
|
|
230
|
+
const changes = diffProcedures(
|
|
231
|
+
testContext,
|
|
232
|
+
{ [main.stableId]: main },
|
|
233
|
+
{ [branch.stableId]: branch },
|
|
234
|
+
);
|
|
235
|
+
expect(changes[0]).toBeInstanceOf(DropProcedure);
|
|
236
|
+
expect(changes[1]).toBeInstanceOf(CreateProcedure);
|
|
237
|
+
expect((changes[1] as CreateProcedure).orReplace).toBe(false);
|
|
161
238
|
});
|
|
162
239
|
|
|
163
240
|
test("create or replace also emits a procedure comment when the comment changes", () => {
|
|
@@ -183,4 +260,25 @@ describe.concurrent("procedure.diff", () => {
|
|
|
183
260
|
changes.some((change) => change instanceof CreateCommentOnProcedure),
|
|
184
261
|
).toBe(true);
|
|
185
262
|
});
|
|
263
|
+
|
|
264
|
+
test("signature change re-emits comment even when comment itself is unchanged", () => {
|
|
265
|
+
// DROP destroys the old comment, so the new CREATE path must re-emit it.
|
|
266
|
+
const main = new Procedure({ ...base, comment: "hello" });
|
|
267
|
+
const branch = new Procedure({
|
|
268
|
+
...base,
|
|
269
|
+
return_type: "text",
|
|
270
|
+
comment: "hello",
|
|
271
|
+
});
|
|
272
|
+
const changes = diffProcedures(
|
|
273
|
+
testContext,
|
|
274
|
+
{ [main.stableId]: main },
|
|
275
|
+
{ [branch.stableId]: branch },
|
|
276
|
+
);
|
|
277
|
+
expect(changes.some((change) => change instanceof DropProcedure)).toBe(
|
|
278
|
+
true,
|
|
279
|
+
);
|
|
280
|
+
expect(
|
|
281
|
+
changes.some((change) => change instanceof CreateCommentOnProcedure),
|
|
282
|
+
).toBe(true);
|
|
283
|
+
});
|
|
186
284
|
});
|
|
@@ -49,8 +49,7 @@ export function diffProcedures(
|
|
|
49
49
|
|
|
50
50
|
const changes: ProcedureChange[] = [];
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
const proc = branch[procedureId];
|
|
52
|
+
const appendCreateProcedureChanges = (proc: Procedure) => {
|
|
54
53
|
changes.push(new CreateProcedure({ procedure: proc }));
|
|
55
54
|
|
|
56
55
|
// OWNER: If the procedure should be owned by someone other than the current user,
|
|
@@ -112,6 +111,10 @@ export function diffProcedures(
|
|
|
112
111
|
ctx.version,
|
|
113
112
|
) as ProcedureChange[]),
|
|
114
113
|
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
for (const procedureId of created) {
|
|
117
|
+
appendCreateProcedureChanges(branch[procedureId]);
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
for (const procedureId of dropped) {
|
|
@@ -122,22 +125,18 @@ export function diffProcedures(
|
|
|
122
125
|
const mainProcedure = main[procedureId];
|
|
123
126
|
const branchProcedure = branch[procedureId];
|
|
124
127
|
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
|
|
128
|
+
// Fields that are part of the function's identity/signature. PostgreSQL
|
|
129
|
+
// rejects `CREATE OR REPLACE FUNCTION` for any of these changes with
|
|
130
|
+
// errors such as:
|
|
131
|
+
// - cannot change return type of existing function
|
|
132
|
+
// - cannot change name of input parameter "..."
|
|
133
|
+
// - cannot change whether a procedure has output parameters
|
|
134
|
+
// - cannot remove parameter defaults from existing function
|
|
135
|
+
// These require `DROP FUNCTION` followed by `CREATE FUNCTION`.
|
|
136
|
+
const SIGNATURE_BREAKING_FIELDS: Array<keyof Procedure> = [
|
|
128
137
|
"kind",
|
|
129
138
|
"return_type",
|
|
130
139
|
"return_type_schema",
|
|
131
|
-
"language",
|
|
132
|
-
// The following properties are alterable in SQL, but our generator may choose
|
|
133
|
-
// to replace on changes not covered by explicit ALTER actions. Keep them out here
|
|
134
|
-
// to allow ALTER for those we implement below.
|
|
135
|
-
// security_definer,
|
|
136
|
-
// volatility,
|
|
137
|
-
// parallel_safety,
|
|
138
|
-
// is_strict,
|
|
139
|
-
// leakproof,
|
|
140
|
-
// Returns-set is part of the signature and not alterable
|
|
141
140
|
"returns_set",
|
|
142
141
|
"argument_count",
|
|
143
142
|
"argument_default_count",
|
|
@@ -146,26 +145,45 @@ export function diffProcedures(
|
|
|
146
145
|
"all_argument_types",
|
|
147
146
|
"argument_modes",
|
|
148
147
|
"argument_defaults",
|
|
148
|
+
];
|
|
149
|
+
// Fields where `CREATE OR REPLACE` is sufficient - body replacement only.
|
|
150
|
+
// Other fields (security_definer, volatility, parallel_safety, is_strict,
|
|
151
|
+
// leakproof, config) are alterable via dedicated ALTER actions below.
|
|
152
|
+
const OR_REPLACEABLE_NON_ALTERABLE_FIELDS: Array<keyof Procedure> = [
|
|
153
|
+
"language",
|
|
149
154
|
"source_code",
|
|
150
155
|
"binary_path",
|
|
151
156
|
"sql_body",
|
|
152
|
-
// config is alterable via SET/RESET
|
|
153
157
|
];
|
|
154
|
-
const
|
|
158
|
+
const signatureChanged = hasNonAlterableChanges(
|
|
155
159
|
mainProcedure,
|
|
156
160
|
branchProcedure,
|
|
157
|
-
|
|
161
|
+
SIGNATURE_BREAKING_FIELDS,
|
|
158
162
|
{
|
|
159
163
|
argument_names: deepEqual,
|
|
160
164
|
argument_types: deepEqual,
|
|
161
165
|
all_argument_types: deepEqual,
|
|
162
166
|
argument_modes: deepEqual,
|
|
163
|
-
config: deepEqual,
|
|
164
167
|
},
|
|
165
168
|
);
|
|
169
|
+
const nonAlterablePropsChanged =
|
|
170
|
+
signatureChanged ||
|
|
171
|
+
hasNonAlterableChanges(
|
|
172
|
+
mainProcedure,
|
|
173
|
+
branchProcedure,
|
|
174
|
+
OR_REPLACEABLE_NON_ALTERABLE_FIELDS,
|
|
175
|
+
);
|
|
166
176
|
|
|
167
|
-
if (
|
|
168
|
-
//
|
|
177
|
+
if (signatureChanged) {
|
|
178
|
+
// PostgreSQL cannot change an existing function's signature via
|
|
179
|
+
// `CREATE OR REPLACE`. Drop the old signature, then recreate.
|
|
180
|
+
// `expandReplaceDependencies` will cascade the replacement to dependent
|
|
181
|
+
// objects (views, triggers, column defaults) via pg_depend edges.
|
|
182
|
+
changes.push(new DropProcedure({ procedure: mainProcedure }));
|
|
183
|
+
appendCreateProcedureChanges(branchProcedure);
|
|
184
|
+
} else if (nonAlterablePropsChanged) {
|
|
185
|
+
// Body-only non-alterable change - `CREATE OR REPLACE` preserves the
|
|
186
|
+
// function OID and keeps dependent objects attached.
|
|
169
187
|
changes.push(
|
|
170
188
|
new CreateProcedure({ procedure: branchProcedure, orReplace: true }),
|
|
171
189
|
);
|
|
@@ -112,6 +112,7 @@ describe.concurrent("table.diff", () => {
|
|
|
112
112
|
validated: false,
|
|
113
113
|
is_local: true,
|
|
114
114
|
no_inherit: false,
|
|
115
|
+
is_temporal: false,
|
|
115
116
|
is_partition_clone: false,
|
|
116
117
|
parent_constraint_schema: null,
|
|
117
118
|
parent_constraint_name: null,
|
|
@@ -328,6 +329,7 @@ describe.concurrent("table.diff", () => {
|
|
|
328
329
|
validated: false,
|
|
329
330
|
is_local: true,
|
|
330
331
|
no_inherit: false,
|
|
332
|
+
is_temporal: false,
|
|
331
333
|
is_partition_clone: false,
|
|
332
334
|
parent_constraint_schema: null,
|
|
333
335
|
parent_constraint_name: null,
|
|
@@ -453,6 +455,7 @@ describe.concurrent("table.diff", () => {
|
|
|
453
455
|
validated: true,
|
|
454
456
|
is_local: true,
|
|
455
457
|
no_inherit: false,
|
|
458
|
+
is_temporal: false,
|
|
456
459
|
is_partition_clone: false,
|
|
457
460
|
parent_constraint_schema: null,
|
|
458
461
|
parent_constraint_name: null,
|
|
@@ -531,6 +534,7 @@ describe.concurrent("table.diff", () => {
|
|
|
531
534
|
validated: true,
|
|
532
535
|
is_local: true,
|
|
533
536
|
no_inherit: false,
|
|
537
|
+
is_temporal: false,
|
|
534
538
|
is_partition_clone: false,
|
|
535
539
|
parent_constraint_schema: null,
|
|
536
540
|
parent_constraint_name: null,
|
|
@@ -606,6 +610,104 @@ describe.concurrent("table.diff", () => {
|
|
|
606
610
|
);
|
|
607
611
|
});
|
|
608
612
|
|
|
613
|
+
test("altered temporal constraint metadata triggers drop+add", () => {
|
|
614
|
+
const tMain = new Table({
|
|
615
|
+
...base,
|
|
616
|
+
name: "t_temporal",
|
|
617
|
+
columns: [
|
|
618
|
+
{
|
|
619
|
+
name: "room_id",
|
|
620
|
+
position: 1,
|
|
621
|
+
data_type: "integer",
|
|
622
|
+
data_type_str: "integer",
|
|
623
|
+
is_custom_type: false,
|
|
624
|
+
custom_type_type: null,
|
|
625
|
+
custom_type_category: null,
|
|
626
|
+
custom_type_schema: null,
|
|
627
|
+
custom_type_name: null,
|
|
628
|
+
not_null: false,
|
|
629
|
+
is_identity: false,
|
|
630
|
+
is_identity_always: false,
|
|
631
|
+
is_generated: false,
|
|
632
|
+
collation: null,
|
|
633
|
+
default: null,
|
|
634
|
+
comment: null,
|
|
635
|
+
},
|
|
636
|
+
{
|
|
637
|
+
name: "booking_period",
|
|
638
|
+
position: 2,
|
|
639
|
+
data_type: "tstzrange",
|
|
640
|
+
data_type_str: "tstzrange",
|
|
641
|
+
is_custom_type: false,
|
|
642
|
+
custom_type_type: null,
|
|
643
|
+
custom_type_category: null,
|
|
644
|
+
custom_type_schema: null,
|
|
645
|
+
custom_type_name: null,
|
|
646
|
+
not_null: false,
|
|
647
|
+
is_identity: false,
|
|
648
|
+
is_identity_always: false,
|
|
649
|
+
is_generated: false,
|
|
650
|
+
collation: null,
|
|
651
|
+
default: null,
|
|
652
|
+
comment: null,
|
|
653
|
+
},
|
|
654
|
+
],
|
|
655
|
+
constraints: [
|
|
656
|
+
{
|
|
657
|
+
name: "bookings_pkey",
|
|
658
|
+
constraint_type: "p",
|
|
659
|
+
deferrable: false,
|
|
660
|
+
initially_deferred: false,
|
|
661
|
+
validated: true,
|
|
662
|
+
is_local: true,
|
|
663
|
+
no_inherit: false,
|
|
664
|
+
is_temporal: false,
|
|
665
|
+
is_partition_clone: false,
|
|
666
|
+
parent_constraint_schema: null,
|
|
667
|
+
parent_constraint_name: null,
|
|
668
|
+
parent_table_schema: null,
|
|
669
|
+
parent_table_name: null,
|
|
670
|
+
key_columns: ["room_id", "booking_period"],
|
|
671
|
+
foreign_key_columns: null,
|
|
672
|
+
foreign_key_table: null,
|
|
673
|
+
foreign_key_schema: null,
|
|
674
|
+
foreign_key_table_is_partition: null,
|
|
675
|
+
foreign_key_parent_schema: null,
|
|
676
|
+
foreign_key_parent_table: null,
|
|
677
|
+
foreign_key_effective_schema: null,
|
|
678
|
+
foreign_key_effective_table: null,
|
|
679
|
+
on_update: null,
|
|
680
|
+
on_delete: null,
|
|
681
|
+
match_type: null,
|
|
682
|
+
check_expression: null,
|
|
683
|
+
owner: "o1",
|
|
684
|
+
definition: "PRIMARY KEY (room_id, booking_period)",
|
|
685
|
+
},
|
|
686
|
+
],
|
|
687
|
+
});
|
|
688
|
+
const tBranch = new Table({
|
|
689
|
+
...tMain,
|
|
690
|
+
constraints: [
|
|
691
|
+
{
|
|
692
|
+
...tMain.constraints[0],
|
|
693
|
+
is_temporal: true,
|
|
694
|
+
definition: "PRIMARY KEY (room_id, booking_period WITHOUT OVERLAPS)",
|
|
695
|
+
},
|
|
696
|
+
],
|
|
697
|
+
});
|
|
698
|
+
const changes = diffTables(
|
|
699
|
+
testContext,
|
|
700
|
+
{ [tMain.stableId]: tMain },
|
|
701
|
+
{ [tBranch.stableId]: tBranch },
|
|
702
|
+
);
|
|
703
|
+
expect(changes.some((c) => c instanceof AlterTableDropConstraint)).toBe(
|
|
704
|
+
true,
|
|
705
|
+
);
|
|
706
|
+
expect(changes.some((c) => c instanceof AlterTableAddConstraint)).toBe(
|
|
707
|
+
true,
|
|
708
|
+
);
|
|
709
|
+
});
|
|
710
|
+
|
|
609
711
|
test("columns added/dropped/altered (type, default, not null)", () => {
|
|
610
712
|
const main = new Table({ ...base, name: "t2", columns: [] });
|
|
611
713
|
const withCol = new Table({
|
|
@@ -130,6 +130,7 @@ function createAlterConstraintChange(mainTable: Table, branchTable: Table) {
|
|
|
130
130
|
mainC.validated !== branchC.validated ||
|
|
131
131
|
mainC.is_local !== branchC.is_local ||
|
|
132
132
|
mainC.no_inherit !== branchC.no_inherit ||
|
|
133
|
+
mainC.is_temporal !== branchC.is_temporal ||
|
|
133
134
|
JSON.stringify(mainC.key_columns) !==
|
|
134
135
|
JSON.stringify(branchC.key_columns) ||
|
|
135
136
|
JSON.stringify(mainC.foreign_key_columns) !==
|
|
@@ -56,6 +56,7 @@ const tableConstraintPropsSchema = z.object({
|
|
|
56
56
|
validated: z.boolean(),
|
|
57
57
|
is_local: z.boolean(),
|
|
58
58
|
no_inherit: z.boolean(),
|
|
59
|
+
is_temporal: z.boolean(),
|
|
59
60
|
is_partition_clone: z.boolean(),
|
|
60
61
|
parent_constraint_schema: z.string().nullable(),
|
|
61
62
|
parent_constraint_name: z.string().nullable(),
|
|
@@ -284,6 +285,7 @@ select
|
|
|
284
285
|
'validated', c.convalidated,
|
|
285
286
|
'is_local', c.conislocal,
|
|
286
287
|
'no_inherit', c.connoinherit,
|
|
288
|
+
'is_temporal', coalesce((to_jsonb(c)->>'conperiod')::boolean, false),
|
|
287
289
|
|
|
288
290
|
-- NEW: propagated-to-partition tagging (PG15+)
|
|
289
291
|
'is_partition_clone', (c.conparentid <> 0::oid),
|
|
@@ -67,6 +67,11 @@ export function diffTriggers(
|
|
|
67
67
|
continue;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
// Note: column_numbers is excluded because it contains pg_trigger.tgattr
|
|
71
|
+
// attnums that differ between databases when physical column layouts
|
|
72
|
+
// diverge but logical (named) columns match. The definition field
|
|
73
|
+
// (pg_get_triggerdef) already captures the UPDATE OF column list by name,
|
|
74
|
+
// so we compare by definition instead.
|
|
70
75
|
const NON_ALTERABLE_FIELDS: Array<keyof Trigger> = [
|
|
71
76
|
"function_schema",
|
|
72
77
|
"function_name",
|
|
@@ -76,18 +81,18 @@ export function diffTriggers(
|
|
|
76
81
|
"deferrable",
|
|
77
82
|
"initially_deferred",
|
|
78
83
|
"argument_count",
|
|
79
|
-
"column_numbers",
|
|
80
84
|
"arguments",
|
|
81
85
|
"when_condition",
|
|
82
86
|
"old_table",
|
|
83
87
|
"new_table",
|
|
84
88
|
"owner",
|
|
89
|
+
"definition", // Compare by definition instead of column_numbers (tgattr)
|
|
85
90
|
];
|
|
86
91
|
const shouldReplace = hasNonAlterableChanges(
|
|
87
92
|
mainTrigger,
|
|
88
93
|
branchTrigger,
|
|
89
94
|
NON_ALTERABLE_FIELDS,
|
|
90
|
-
{
|
|
95
|
+
{ arguments: deepEqual },
|
|
91
96
|
);
|
|
92
97
|
if (shouldReplace) {
|
|
93
98
|
const tableStableId = stableId.table(
|
|
@@ -134,7 +134,10 @@ export class Trigger extends BasePgModel {
|
|
|
134
134
|
deferrable: this.deferrable,
|
|
135
135
|
initially_deferred: this.initially_deferred,
|
|
136
136
|
argument_count: this.argument_count,
|
|
137
|
-
column_numbers:
|
|
137
|
+
// column_numbers excluded: contains pg_trigger.tgattr attnums that differ
|
|
138
|
+
// between databases when physical column layouts diverge but logical
|
|
139
|
+
// (named) columns match. The definition field (pg_get_triggerdef) captures
|
|
140
|
+
// the UPDATE OF column list by name, so we compare by definition instead.
|
|
138
141
|
arguments: this.arguments,
|
|
139
142
|
when_condition: this.when_condition,
|
|
140
143
|
old_table: this.old_table,
|
|
@@ -145,6 +148,7 @@ export class Trigger extends BasePgModel {
|
|
|
145
148
|
parent_table_name: this.parent_table_name,
|
|
146
149
|
is_on_partitioned_table: this.is_on_partitioned_table,
|
|
147
150
|
owner: this.owner,
|
|
151
|
+
definition: this.definition,
|
|
148
152
|
comment: this.comment,
|
|
149
153
|
};
|
|
150
154
|
}
|
|
@@ -578,6 +578,7 @@ const pkConstraint = {
|
|
|
578
578
|
validated: true,
|
|
579
579
|
is_local: true,
|
|
580
580
|
no_inherit: false,
|
|
581
|
+
is_temporal: false,
|
|
581
582
|
is_partition_clone: false,
|
|
582
583
|
parent_constraint_schema: null,
|
|
583
584
|
parent_constraint_name: null,
|
|
@@ -609,6 +610,7 @@ const uniqueConstraint = {
|
|
|
609
610
|
validated: true,
|
|
610
611
|
is_local: true,
|
|
611
612
|
no_inherit: false,
|
|
613
|
+
is_temporal: false,
|
|
612
614
|
is_partition_clone: false,
|
|
613
615
|
parent_constraint_schema: null,
|
|
614
616
|
parent_constraint_name: null,
|
|
@@ -639,6 +641,7 @@ const fkConstraint = {
|
|
|
639
641
|
validated: true,
|
|
640
642
|
is_local: true,
|
|
641
643
|
no_inherit: false,
|
|
644
|
+
is_temporal: false,
|
|
642
645
|
is_partition_clone: false,
|
|
643
646
|
parent_constraint_schema: null,
|
|
644
647
|
parent_constraint_name: null,
|
|
@@ -670,6 +673,7 @@ const checkConstraint = {
|
|
|
670
673
|
validated: true,
|
|
671
674
|
is_local: true,
|
|
672
675
|
no_inherit: true,
|
|
676
|
+
is_temporal: false,
|
|
673
677
|
is_partition_clone: false,
|
|
674
678
|
parent_constraint_schema: null,
|
|
675
679
|
parent_constraint_name: null,
|
|
@@ -2,12 +2,15 @@ import { describe, expect, test } from "bun:test";
|
|
|
2
2
|
import { Catalog, createEmptyCatalog } from "./catalog.model.ts";
|
|
3
3
|
import type { Change } from "./change.types.ts";
|
|
4
4
|
import {
|
|
5
|
+
AlterTableAddConstraint,
|
|
5
6
|
AlterTableChangeOwner,
|
|
6
7
|
AlterTableDropColumn,
|
|
7
8
|
AlterTableDropConstraint,
|
|
8
9
|
AlterTableEnableRowLevelSecurity,
|
|
9
10
|
AlterTableSetReplicaIdentity,
|
|
11
|
+
AlterTableValidateConstraint,
|
|
10
12
|
} from "./objects/table/changes/table.alter.ts";
|
|
13
|
+
import { CreateCommentOnConstraint } from "./objects/table/changes/table.comment.ts";
|
|
11
14
|
import { CreateTable } from "./objects/table/changes/table.create.ts";
|
|
12
15
|
import { DropTable } from "./objects/table/changes/table.drop.ts";
|
|
13
16
|
import { GrantTablePrivileges } from "./objects/table/changes/table.privilege.ts";
|
|
@@ -77,6 +80,7 @@ describe("normalizePostDiffCycles", () => {
|
|
|
77
80
|
validated: true,
|
|
78
81
|
is_local: true,
|
|
79
82
|
no_inherit: false,
|
|
83
|
+
is_temporal: false,
|
|
80
84
|
is_partition_clone: false,
|
|
81
85
|
parent_constraint_schema: null,
|
|
82
86
|
parent_constraint_name: null,
|
|
@@ -117,6 +121,7 @@ describe("normalizePostDiffCycles", () => {
|
|
|
117
121
|
validated: true,
|
|
118
122
|
is_local: true,
|
|
119
123
|
no_inherit: false,
|
|
124
|
+
is_temporal: false,
|
|
120
125
|
is_partition_clone: false,
|
|
121
126
|
parent_constraint_schema: null,
|
|
122
127
|
parent_constraint_name: null,
|
|
@@ -237,6 +242,7 @@ describe("normalizePostDiffCycles", () => {
|
|
|
237
242
|
validated: true,
|
|
238
243
|
is_local: true,
|
|
239
244
|
no_inherit: false,
|
|
245
|
+
is_temporal: false,
|
|
240
246
|
is_partition_clone: false,
|
|
241
247
|
parent_constraint_schema: null,
|
|
242
248
|
parent_constraint_name: null,
|
|
@@ -314,4 +320,140 @@ describe("normalizePostDiffCycles", () => {
|
|
|
314
320
|
expect(normalized).toContain(preExistingReplicaIdentity);
|
|
315
321
|
expect(normalized).toContain(preExistingGrant);
|
|
316
322
|
});
|
|
323
|
+
|
|
324
|
+
test("dedupes duplicate constraint Add/Validate/Comment on replaced tables keeping last occurrence", async () => {
|
|
325
|
+
const baseline = await createEmptyCatalog(170000, "postgres");
|
|
326
|
+
const branchChildren = new Table({
|
|
327
|
+
...baseTableProps,
|
|
328
|
+
name: "children",
|
|
329
|
+
columns: [
|
|
330
|
+
{ ...integerColumn("id", 1), not_null: true },
|
|
331
|
+
integerColumn("parent_ref", 2),
|
|
332
|
+
],
|
|
333
|
+
});
|
|
334
|
+
const otherTable = new Table({
|
|
335
|
+
...baseTableProps,
|
|
336
|
+
name: "other",
|
|
337
|
+
columns: [{ ...integerColumn("id", 1), not_null: true }],
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
const fkConstraint = {
|
|
341
|
+
name: "children_parent_ref_fkey",
|
|
342
|
+
constraint_type: "f" as const,
|
|
343
|
+
deferrable: false,
|
|
344
|
+
initially_deferred: false,
|
|
345
|
+
validated: false,
|
|
346
|
+
is_local: true,
|
|
347
|
+
no_inherit: false,
|
|
348
|
+
is_temporal: true,
|
|
349
|
+
is_partition_clone: false,
|
|
350
|
+
parent_constraint_schema: null,
|
|
351
|
+
parent_constraint_name: null,
|
|
352
|
+
parent_table_schema: null,
|
|
353
|
+
parent_table_name: null,
|
|
354
|
+
key_columns: ["parent_ref"],
|
|
355
|
+
foreign_key_columns: ["id"],
|
|
356
|
+
foreign_key_table: "parents",
|
|
357
|
+
foreign_key_schema: "public",
|
|
358
|
+
foreign_key_table_is_partition: false,
|
|
359
|
+
foreign_key_parent_schema: null,
|
|
360
|
+
foreign_key_parent_table: null,
|
|
361
|
+
foreign_key_effective_schema: "public",
|
|
362
|
+
foreign_key_effective_table: "parents",
|
|
363
|
+
on_update: "a" as const,
|
|
364
|
+
on_delete: "a" as const,
|
|
365
|
+
match_type: "s" as const,
|
|
366
|
+
check_expression: null,
|
|
367
|
+
owner: "postgres",
|
|
368
|
+
definition:
|
|
369
|
+
"FOREIGN KEY (parent_ref, PERIOD valid_period) REFERENCES public.parents(id, PERIOD valid_period)",
|
|
370
|
+
comment: "fk comment",
|
|
371
|
+
};
|
|
372
|
+
const otherConstraint = {
|
|
373
|
+
...fkConstraint,
|
|
374
|
+
name: "other_unique",
|
|
375
|
+
constraint_type: "u" as const,
|
|
376
|
+
foreign_key_table: null,
|
|
377
|
+
foreign_key_schema: null,
|
|
378
|
+
foreign_key_effective_schema: null,
|
|
379
|
+
foreign_key_effective_table: null,
|
|
380
|
+
foreign_key_columns: [],
|
|
381
|
+
key_columns: ["id"],
|
|
382
|
+
definition: "UNIQUE (id)",
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
const diffTablesAdd = new AlterTableAddConstraint({
|
|
386
|
+
table: branchChildren,
|
|
387
|
+
constraint: fkConstraint,
|
|
388
|
+
});
|
|
389
|
+
const diffTablesValidate = new AlterTableValidateConstraint({
|
|
390
|
+
table: branchChildren,
|
|
391
|
+
constraint: fkConstraint,
|
|
392
|
+
});
|
|
393
|
+
const diffTablesComment = new CreateCommentOnConstraint({
|
|
394
|
+
table: branchChildren,
|
|
395
|
+
constraint: fkConstraint,
|
|
396
|
+
});
|
|
397
|
+
const expansionAdd = new AlterTableAddConstraint({
|
|
398
|
+
table: branchChildren,
|
|
399
|
+
constraint: fkConstraint,
|
|
400
|
+
});
|
|
401
|
+
const expansionValidate = new AlterTableValidateConstraint({
|
|
402
|
+
table: branchChildren,
|
|
403
|
+
constraint: fkConstraint,
|
|
404
|
+
});
|
|
405
|
+
const expansionComment = new CreateCommentOnConstraint({
|
|
406
|
+
table: branchChildren,
|
|
407
|
+
constraint: fkConstraint,
|
|
408
|
+
});
|
|
409
|
+
const soloOtherTableAdd = new AlterTableAddConstraint({
|
|
410
|
+
table: otherTable,
|
|
411
|
+
constraint: otherConstraint,
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const changes: Change[] = [
|
|
415
|
+
new DropTable({ table: branchChildren }),
|
|
416
|
+
new CreateTable({ table: branchChildren }),
|
|
417
|
+
diffTablesAdd,
|
|
418
|
+
diffTablesValidate,
|
|
419
|
+
diffTablesComment,
|
|
420
|
+
soloOtherTableAdd,
|
|
421
|
+
expansionAdd,
|
|
422
|
+
expansionValidate,
|
|
423
|
+
expansionComment,
|
|
424
|
+
];
|
|
425
|
+
|
|
426
|
+
const mainCatalog = new Catalog({
|
|
427
|
+
...baseline,
|
|
428
|
+
tables: { [branchChildren.stableId]: branchChildren },
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
const normalized = normalizePostDiffCycles({
|
|
432
|
+
changes,
|
|
433
|
+
mainCatalog,
|
|
434
|
+
replacedTableIds: new Set([branchChildren.stableId]),
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
expect(normalized).not.toContain(diffTablesAdd);
|
|
438
|
+
expect(normalized).not.toContain(diffTablesValidate);
|
|
439
|
+
expect(normalized).not.toContain(diffTablesComment);
|
|
440
|
+
expect(normalized).toContain(expansionAdd);
|
|
441
|
+
expect(normalized).toContain(expansionValidate);
|
|
442
|
+
expect(normalized).toContain(expansionComment);
|
|
443
|
+
expect(normalized).toContain(soloOtherTableAdd);
|
|
444
|
+
|
|
445
|
+
expect(
|
|
446
|
+
normalized.filter((change) => change instanceof AlterTableAddConstraint),
|
|
447
|
+
).toHaveLength(2);
|
|
448
|
+
expect(
|
|
449
|
+
normalized.filter(
|
|
450
|
+
(change) => change instanceof AlterTableValidateConstraint,
|
|
451
|
+
),
|
|
452
|
+
).toHaveLength(1);
|
|
453
|
+
expect(
|
|
454
|
+
normalized.filter(
|
|
455
|
+
(change) => change instanceof CreateCommentOnConstraint,
|
|
456
|
+
),
|
|
457
|
+
).toHaveLength(1);
|
|
458
|
+
});
|
|
317
459
|
});
|