@metaobjectsdev/migrate-ts 1.0.0 → 1.0.2
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/diff/index.d.ts +2 -1
- package/dist/diff/index.d.ts.map +1 -1
- package/dist/diff/index.js +18 -4
- package/dist/diff/index.js.map +1 -1
- package/dist/diff/status.d.ts.map +1 -1
- package/dist/diff/status.js +25 -13
- package/dist/diff/status.js.map +1 -1
- package/dist/emit/postgres.d.ts.map +1 -1
- package/dist/emit/postgres.js +130 -4
- package/dist/emit/postgres.js.map +1 -1
- package/dist/errors.d.ts +8 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +24 -9
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/pg-identity-default.d.ts +6 -0
- package/dist/pg-identity-default.d.ts.map +1 -1
- package/dist/pg-identity-default.js +7 -0
- package/dist/pg-identity-default.js.map +1 -1
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/diff/index.ts +15 -4
- package/src/diff/status.ts +28 -15
- package/src/emit/postgres.ts +158 -4
- package/src/errors.ts +26 -10
- package/src/index.ts +1 -1
- package/src/pg-identity-default.ts +10 -0
- package/src/types.ts +9 -1
package/dist/errors.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { dropsAutoSequenceDefault } from "./pg-identity-default.js";
|
|
1
2
|
// ---------------------------------------------------------------------------
|
|
2
3
|
// SetNullNotNullableError — surfaced by buildExpectedSchema
|
|
3
4
|
// ---------------------------------------------------------------------------
|
|
@@ -26,18 +27,31 @@ export class SetNullNotNullableError extends Error {
|
|
|
26
27
|
this.offendingFields = offendingFields;
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
|
-
const
|
|
30
|
-
"drop-column": "
|
|
31
|
-
"drop-table": "
|
|
32
|
-
"change-column-type": "
|
|
33
|
-
"change-column-nullable": "
|
|
34
|
-
"drop-index": "
|
|
35
|
-
"drop-fk": "
|
|
30
|
+
const ALLOW_OPTION_BY_KIND = {
|
|
31
|
+
"drop-column": "dropColumn",
|
|
32
|
+
"drop-table": "dropTable",
|
|
33
|
+
"change-column-type": "typeChange",
|
|
34
|
+
"change-column-nullable": "nullableToNotNull",
|
|
35
|
+
"drop-index": "dropIndex",
|
|
36
|
+
"drop-fk": "dropFk",
|
|
37
|
+
"drop-check": "dropCheck",
|
|
36
38
|
// change-column-default is blocked in exactly one shape — dropping a live
|
|
37
39
|
// Postgres auto-sequence default with no declared @generation (see
|
|
38
40
|
// diff/status.ts) — and allow.dropIdentityDefault is the flag that enables it.
|
|
39
|
-
"change-column-default": "
|
|
41
|
+
"change-column-default": "dropIdentityDefault",
|
|
40
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* The `AllowOptions` field that unblocks `c`, chosen by what BLOCKED it rather than by its kind
|
|
45
|
+
* alone. A type change carries its column's default change, so one that would drop a live
|
|
46
|
+
* auto-sequence default is blocked by THAT, and `typeChange` would not unblock it. The single
|
|
47
|
+
* source for this error's hints and the CLI's `--allow` token.
|
|
48
|
+
*/
|
|
49
|
+
export function allowOptionFor(c) {
|
|
50
|
+
if (c.kind === "change-column-type" && dropsAutoSequenceDefault(c.fromDefault, c.toDefault)) {
|
|
51
|
+
return "dropIdentityDefault";
|
|
52
|
+
}
|
|
53
|
+
return ALLOW_OPTION_BY_KIND[c.kind];
|
|
54
|
+
}
|
|
41
55
|
function changeLocator(c) {
|
|
42
56
|
switch (c.kind) {
|
|
43
57
|
case "drop-column":
|
|
@@ -78,7 +92,8 @@ export class BlockedChangesError extends Error {
|
|
|
78
92
|
enableHints;
|
|
79
93
|
constructor(blocked) {
|
|
80
94
|
const hints = blocked.map((c) => {
|
|
81
|
-
const
|
|
95
|
+
const option = allowOptionFor(c);
|
|
96
|
+
const flag = option === undefined ? "(no flag enables this)" : `allow.${option}`;
|
|
82
97
|
return `${c.kind} on ${changeLocator(c)}: pass ${flag}`;
|
|
83
98
|
});
|
|
84
99
|
const msg = `${blocked.length} blocked change(s):\n - ${hints.join("\n - ")}`;
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AAEpE,8EAA8E;AAC9E,4DAA4D;AAC5D,8EAA8E;AAE9E;;;;;;;;;GASG;AACH,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAC9B,IAAI,GAAG,yBAAyB,CAAC;IAC1C,UAAU,CAAS;IACnB,cAAc,CAAS;IACvB,eAAe,CAAW;IAEnC,YAAY,UAAkB,EAAE,cAAsB,EAAE,eAAyB;QAC/E,MAAM,SAAS,GAAG,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC7C,KAAK,CACH,WAAW,UAAU,qBAAqB,cAAc,4BAA4B;YACpF,iBAAiB,SAAS,oCAAoC;YAC9D,uHAAuH,CACxH,CAAC;QACF,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;IACzC,CAAC;CACF;AAED,MAAM,oBAAoB,GAAoD;IAC5E,aAAa,EAAE,YAAY;IAC3B,YAAY,EAAE,WAAW;IACzB,oBAAoB,EAAE,YAAY;IAClC,wBAAwB,EAAE,mBAAmB;IAC7C,YAAY,EAAE,WAAW;IACzB,SAAS,EAAE,QAAQ;IACnB,YAAY,EAAE,WAAW;IACzB,0EAA0E;IAC1E,mEAAmE;IACnE,+EAA+E;IAC/E,uBAAuB,EAAE,qBAAqB;CAC/C,CAAC;AA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;;;;;;;GASG;AACH,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC5B,IAAI,GAAG,uBAAuB,CAAC;IACxC,KAAK,CAAS;IACd,cAAc,CAAW;IACzB,kBAAkB,CAAW;IAC7B,MAAM,CAAU;IAEzB,YAAY,KAAa,EAAE,cAAwB,EAAE,kBAA4B,EAAE,MAAe;QAChG,MAAM,SAAS,GAAG,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;QACtE,MAAM,GAAG,GAAG,CAAC,IAAc,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,gBAAgB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC;QAC1G,KAAK,CACH,mBAAmB,SAAS,0CAA0C,GAAG,CAAC,cAAc,CAAC,MAAM;YAC7F,YAAY,GAAG,CAAC,kBAAkB,CAAC,6DAA6D;YAChG,gGAAgG;YAChG,qGAAqG;YACrG,sDAAsD,CACzD,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;QAC7C,IAAI,MAAM,KAAK,SAAS;YAAE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACjD,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export { snapshotChecksum } from "./snapshot/checksum.js";
|
|
|
19
19
|
export { snapshotPath, readSnapshot, writeSnapshot } from "./snapshot/store.js";
|
|
20
20
|
export { planOffline, baselineFromMetadata } from "./snapshot/plan.js";
|
|
21
21
|
export type { PlanOfflineArgs, PlanOfflineResult } from "./snapshot/plan.js";
|
|
22
|
-
export { BlockedChangesError, SetNullNotNullableError, PrimaryKeyChangeError } from "./errors.js";
|
|
22
|
+
export { BlockedChangesError, SetNullNotNullableError, PrimaryKeyChangeError, allowOptionFor } from "./errors.js";
|
|
23
23
|
export { isWidening, sqlTypeEquals } from "./sql-type.js";
|
|
24
24
|
export type { SqlType } from "./sql-type.js";
|
|
25
25
|
export type { SchemaSnapshot, SnapshotMeta, TableDescriptor, ColumnDescriptor, IndexDescriptor, FkDescriptor, ColumnDefault, ViewDescriptor, FkAction, Change, ChangeKind, ChangeStatus, AllowOptions, AmbiguousChange, AmbiguousResolution, AmbiguousCallback, DiffResult, EmitResult, Dialect, } from "./types.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,mBAAmB,EAAE,iCAAiC,EAAE,MAAM,sBAAsB,CAAC;AAC9F,YAAY,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAC3F,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAGvD,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC5F,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,KAAK,mBAAmB,EAAE,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpH,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC1E,YAAY,EAAE,m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gB,GACtB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,YAAY,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAG9D,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,gBAAgB,EAChB,KAAK,SAAS,EACd,KAAK,cAAc,GACpB,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ export { snapshotChecksum } from "./snapshot/checksum.js";
|
|
|
30
30
|
export { snapshotPath, readSnapshot, writeSnapshot } from "./snapshot/store.js";
|
|
31
31
|
export { planOffline, baselineFromMetadata } from "./snapshot/plan.js";
|
|
32
32
|
// Errors
|
|
33
|
-
export { BlockedChangesError, SetNullNotNullableError, PrimaryKeyChangeError } from "./errors.js";
|
|
33
|
+
export { BlockedChangesError, SetNullNotNullableError, PrimaryKeyChangeError, allowOptionFor } from "./errors.js";
|
|
34
34
|
// SqlType helpers (rarely needed but useful for advanced consumers)
|
|
35
35
|
export { isWidening, sqlTypeEquals } from "./sql-type.js";
|
|
36
36
|
// View-body comparison — used by the diff to detect body drift (replace-view).
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAC3D,EAAE;AACF,gEAAgE;AAChE,yEAAyE;AACzE,sEAAsE;AACtE,2BAA2B;AAC3B,EAAE;AACF,2DAA2D;AAE3D,qBAAqB;AACrB,OAAO,EAAE,mBAAmB,EAAE,iCAAiC,EAAE,MAAM,sBAAsB,CAAC;AAE9F,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;A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AAmB,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElH,oEAAoE;AACpE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAkB1D,+EAA+E;AAC/E,gFAAgF;AAChF,mFAAmF;AACnF,4EAA4E;AAC5E,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAExE,oCAAoC;AACpC,wEAAwE;AACxE,yEAAyE;AACzE,8CAA8C;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAC1F,OAAO,EAAE,sBAAsB,EAAE,6BAA6B,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAGxH,mBAAmB;AACnB,OAAO,EAAE,YAAY,EAA2C,MAAM,oBAAoB,CAAC;AAE3F,2EAA2E;AAC3E,OAAO,EACL,YAAY,EACZ,aAAa,EACb,aAAa,EACb,YAAY,EACZ,cAAc,EACd,cAAc,EACd,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,qBAAqB,GAItB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,UAAU,GAKX,MAAM,kBAAkB,CAAC;AAE1B,0EAA0E;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD,wEAAwE;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAG7D,0BAA0B;AAC1B,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,gBAAgB,GAGjB,MAAM,sBAAsB,CAAC"}
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
import type { ColumnDefault } from "./types.js";
|
|
1
2
|
export declare function isPgAutoSequenceDefault(raw: string | null | undefined): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* A default change that DROPS a live auto-sequence default: the one default shape the diff
|
|
5
|
+
* gates (diff/status.ts), whether it arrives as its own change or folded into a type change.
|
|
6
|
+
*/
|
|
7
|
+
export declare function dropsAutoSequenceDefault(from: ColumnDefault | undefined, to: ColumnDefault | undefined): boolean;
|
|
2
8
|
//# sourceMappingURL=pg-identity-default.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-identity-default.d.ts","sourceRoot":"","sources":["../src/pg-identity-default.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pg-identity-default.d.ts","sourceRoot":"","sources":["../src/pg-identity-default.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAkBhD,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAE/E;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,aAAa,GAAG,SAAS,EAAE,EAAE,EAAE,aAAa,GAAG,SAAS,GAAG,OAAO,CAEhH"}
|
|
@@ -17,4 +17,11 @@
|
|
|
17
17
|
export function isPgAutoSequenceDefault(raw) {
|
|
18
18
|
return raw !== null && raw !== undefined && /^nextval\(/i.test(raw);
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* A default change that DROPS a live auto-sequence default: the one default shape the diff
|
|
22
|
+
* gates (diff/status.ts), whether it arrives as its own change or folded into a type change.
|
|
23
|
+
*/
|
|
24
|
+
export function dropsAutoSequenceDefault(from, to) {
|
|
25
|
+
return to === undefined && from?.kind === "expr" && isPgAutoSequenceDefault(from.value);
|
|
26
|
+
}
|
|
20
27
|
//# sourceMappingURL=pg-identity-default.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-identity-default.js","sourceRoot":"","sources":["../src/pg-identity-default.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pg-identity-default.js","sourceRoot":"","sources":["../src/pg-identity-default.ts"],"names":[],"mappings":"AAEA,6BAA6B;AAC7B,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,4DAA4D;AAC5D,kFAAkF;AAClF,4EAA4E;AAC5E,4EAA4E;AAC5E,2CAA2C;AAC3C,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,+EAA+E;AAC/E,kFAAkF;AAClF,yDAAyD;AACzD,MAAM,UAAU,uBAAuB,CAAC,GAA8B;IACpE,OAAO,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,IAAI,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACtE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,wBAAwB,CAAC,IAA+B,EAAE,EAA6B;IACrG,OAAO,EAAE,KAAK,SAAS,IAAI,IAAI,EAAE,IAAI,KAAK,MAAM,IAAI,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1F,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -247,6 +247,14 @@ export type Change = {
|
|
|
247
247
|
column: string;
|
|
248
248
|
from: SqlType;
|
|
249
249
|
to: SqlType;
|
|
250
|
+
/** The column's live default and the default it should end with, when either exists.
|
|
251
|
+
* Postgres converts a default with an ASSIGNMENT cast, never through USING, so a type
|
|
252
|
+
* change that needs USING must drop the default first and set it after, which the
|
|
253
|
+
* emitter can only do knowing both. A type change CARRIES its column's default change
|
|
254
|
+
* rather than sitting beside a separate `change-column-default`, whose down would run
|
|
255
|
+
* first and set the old default on a column still holding the new type. */
|
|
256
|
+
fromDefault?: ColumnDefault;
|
|
257
|
+
toDefault?: ColumnDefault;
|
|
250
258
|
status: ChangeStatus;
|
|
251
259
|
} | {
|
|
252
260
|
kind: "change-column-nullable";
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":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
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Schema migration tooling for MetaObjects: diff metadata vs DB and emit SQL for Postgres and SQLite.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@iarna/toml": "^2.2.5",
|
|
48
|
-
"@metaobjectsdev/metadata": "1.0.
|
|
48
|
+
"@metaobjectsdev/metadata": "1.0.2"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"kysely": ">=0.27.0 <0.30.0",
|
package/src/diff/index.ts
CHANGED
|
@@ -380,11 +380,15 @@ function diffTableColumns(
|
|
|
380
380
|
// dialect-aware: SQLite/D1 cannot physically represent a VARCHAR length or a
|
|
381
381
|
// native json type, so those distinctions must not read as drift (see
|
|
382
382
|
// canonTypeForDialect). Postgres stays exact.
|
|
383
|
+
// A type change CARRIES the column's default change (fromDefault/toDefault, types.ts),
|
|
384
|
+
// filled in below once the identity rule has decided what the default should be.
|
|
385
|
+
let typeChange: Extract<Change, { kind: "change-column-type" }> | undefined;
|
|
383
386
|
if (!sqlTypeEqualsForDialect(ec.sqlType, ac.sqlType, dialect)) {
|
|
384
|
-
|
|
387
|
+
typeChange = {
|
|
385
388
|
kind: "change-column-type", table, ...sx, column: name,
|
|
386
389
|
from: ac.sqlType, to: ec.sqlType, status: ALLOWED,
|
|
387
|
-
}
|
|
390
|
+
};
|
|
391
|
+
changes.push(typeChange);
|
|
388
392
|
}
|
|
389
393
|
if (ec.nullable !== ac.nullable) {
|
|
390
394
|
changes.push({
|
|
@@ -420,7 +424,14 @@ function diffTableColumns(
|
|
|
420
424
|
ac.default !== undefined && ac.default.kind === "expr" && isPgAutoSequenceDefault(ac.default.value);
|
|
421
425
|
const skipIdentityDefaultDiff =
|
|
422
426
|
ec.identity === "uuid" || (ec.identity === "increment" && liveIsAutoSequenceDefault);
|
|
423
|
-
if (
|
|
427
|
+
if (typeChange) {
|
|
428
|
+
// An identity default the diff deliberately never compares is KEPT: the emitter drops
|
|
429
|
+
// and re-sets a default around a converting type change, so leaving `toDefault` unset
|
|
430
|
+
// here would drop it for good.
|
|
431
|
+
const toDefault = skipIdentityDefaultDiff ? ac.default : ec.default;
|
|
432
|
+
if (ac.default !== undefined) typeChange.fromDefault = ac.default;
|
|
433
|
+
if (toDefault !== undefined) typeChange.toDefault = toDefault;
|
|
434
|
+
} else if (!skipIdentityDefaultDiff && !columnDefaultsEqual(ec.default, ac.default)) {
|
|
424
435
|
const change: Change = {
|
|
425
436
|
kind: "change-column-default", table, ...sx, column: name,
|
|
426
437
|
status: ALLOWED,
|
|
@@ -830,7 +841,7 @@ function recreateViewsDependingOnChangedTables(
|
|
|
830
841
|
}
|
|
831
842
|
}
|
|
832
843
|
|
|
833
|
-
function columnDefaultsEqual(a: ColumnDescriptor["default"], b: ColumnDescriptor["default"]): boolean {
|
|
844
|
+
export function columnDefaultsEqual(a: ColumnDescriptor["default"], b: ColumnDescriptor["default"]): boolean {
|
|
834
845
|
if (a === undefined && b === undefined) return true;
|
|
835
846
|
if (a === undefined || b === undefined) return false;
|
|
836
847
|
return a.kind === b.kind && a.value === b.value;
|
package/src/diff/status.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Change, AllowOptions } from "../types.js";
|
|
1
|
+
import type { Change, AllowOptions, ColumnDefault } from "../types.js";
|
|
2
2
|
import { isWidening } from "../sql-type.js";
|
|
3
|
-
import {
|
|
3
|
+
import { dropsAutoSequenceDefault } from "../pg-identity-default.js";
|
|
4
4
|
import { DEFAULT_DB_SCHEMA_POSTGRES } from "@metaobjectsdev/metadata";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -101,9 +101,14 @@ function blockedReasonFor(
|
|
|
101
101
|
}
|
|
102
102
|
return null;
|
|
103
103
|
|
|
104
|
-
case "change-column-type":
|
|
104
|
+
case "change-column-type": {
|
|
105
|
+
// A type change carries its column's default change (types.ts), so the one gated
|
|
106
|
+
// default shape (see change-column-default below) is gated here as well.
|
|
107
|
+
const refusal = autoSequenceDropRefusal(c.table, c.column, c.fromDefault, c.toDefault, allow);
|
|
108
|
+
if (refusal !== null) return refusal;
|
|
105
109
|
if (isWidening(c.from, c.to)) return null; // widening always allowed
|
|
106
110
|
return allow.typeChange ? null : "lossy type change (pass allow.typeChange)";
|
|
111
|
+
}
|
|
107
112
|
|
|
108
113
|
case "change-column-nullable":
|
|
109
114
|
// from = actual.nullable, to = expected.nullable
|
|
@@ -129,18 +134,7 @@ function blockedReasonFor(
|
|
|
129
134
|
// than guess. Anything else about change-column-default — including
|
|
130
135
|
// dropping a plain literal default — falls through to the unconditional
|
|
131
136
|
// `return null` below.
|
|
132
|
-
|
|
133
|
-
c.to === undefined && c.from?.kind === "expr" && isPgAutoSequenceDefault(c.from.value);
|
|
134
|
-
if (droppingAutoSequence && !allow.dropIdentityDefault) {
|
|
135
|
-
return `column "${c.table}"."${c.column}" has a live Postgres auto-increment default `
|
|
136
|
-
+ `(${c.from!.value}) but its metadata declares no @generation — this is ambiguous: it `
|
|
137
|
-
+ `could mean @generation was never declared, or that auto-increment is being removed on `
|
|
138
|
-
+ `purpose. Dropping the default is destructive (every insert that omits the column starts `
|
|
139
|
-
+ `failing), so this refuses rather than guessing. Declare @generation: increment on the `
|
|
140
|
-
+ `identity to keep the sequence, or pass --allow drop-identity-default if removing it is `
|
|
141
|
-
+ `intentional`;
|
|
142
|
-
}
|
|
143
|
-
return null;
|
|
137
|
+
return autoSequenceDropRefusal(c.table, c.column, c.from, c.to, allow);
|
|
144
138
|
}
|
|
145
139
|
|
|
146
140
|
// Always-allowed kinds
|
|
@@ -155,3 +149,22 @@ function blockedReasonFor(
|
|
|
155
149
|
return null;
|
|
156
150
|
}
|
|
157
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Dropping a live Postgres auto-sequence default (`nextval(...)`) while the metadata declares
|
|
155
|
+
* no identity is ambiguous, so it is refused unless allowed. Shared by `change-column-default`
|
|
156
|
+
* and `change-column-type`, which carries its column's default change.
|
|
157
|
+
*/
|
|
158
|
+
function autoSequenceDropRefusal(
|
|
159
|
+
table: string, column: string, from: ColumnDefault | undefined, to: ColumnDefault | undefined,
|
|
160
|
+
allow: { dropIdentityDefault?: boolean },
|
|
161
|
+
): string | null {
|
|
162
|
+
if (!dropsAutoSequenceDefault(from, to) || allow.dropIdentityDefault) return null;
|
|
163
|
+
return `column "${table}"."${column}" has a live Postgres auto-increment default `
|
|
164
|
+
+ `(${from!.value}) but its metadata declares no @generation — this is ambiguous: it `
|
|
165
|
+
+ `could mean @generation was never declared, or that auto-increment is being removed on `
|
|
166
|
+
+ `purpose. Dropping the default is destructive (every insert that omits the column starts `
|
|
167
|
+
+ `failing), so this refuses rather than guessing. Declare @generation: increment on the `
|
|
168
|
+
+ `identity to keep the sequence, or pass --allow drop-identity-default if removing it is `
|
|
169
|
+
+ `intentional`;
|
|
170
|
+
}
|
package/src/emit/postgres.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { SqlType } from "../sql-type.js";
|
|
|
6
6
|
import { DEFAULT_DB_SCHEMA_POSTGRES } from "@metaobjectsdev/metadata";
|
|
7
7
|
import { renderFingerprintMarker, viewFingerprint } from "../view-fingerprint.js";
|
|
8
8
|
import { viewReplaceIsLegal } from "../view-column-types.js";
|
|
9
|
+
import { columnDefaultsEqual } from "../diff/index.js";
|
|
9
10
|
|
|
10
11
|
// Stages run low → high. drop-view runs BEFORE drop-table so a view that
|
|
11
12
|
// depends on a soon-to-be-dropped table is removed first. create-view runs
|
|
@@ -53,9 +54,17 @@ export function renderPostgres(changes: Change[]): EmitResult {
|
|
|
53
54
|
downStmts.push(renderDown(c));
|
|
54
55
|
}
|
|
55
56
|
// Down runs in reverse order (so creates undo correctly w.r.t. FKs).
|
|
57
|
+
const upHelpers = conversionHelpers(sorted, "up");
|
|
58
|
+
const downHelpers = conversionHelpers(sorted, "down");
|
|
56
59
|
return {
|
|
57
|
-
up: [
|
|
58
|
-
|
|
60
|
+
up: [
|
|
61
|
+
...createSchemaStmts(sorted), ...upHelpers.map(createHelper),
|
|
62
|
+
...upStmts, ...upHelpers.map(dropHelper),
|
|
63
|
+
].join("\n\n"),
|
|
64
|
+
down: [
|
|
65
|
+
...downHelpers.map(createHelper),
|
|
66
|
+
...[...downStmts].reverse(), ...downHelpers.map(dropHelper),
|
|
67
|
+
].join("\n\n"),
|
|
59
68
|
recreatedTables: new Set(), // postgres alters in place; no recreate-and-copy
|
|
60
69
|
};
|
|
61
70
|
}
|
|
@@ -109,7 +118,7 @@ function renderUp(c: Change): string {
|
|
|
109
118
|
}
|
|
110
119
|
case "drop-column": return `ALTER TABLE ${quoteQualified(c.table, c.schema)} DROP COLUMN ${quote(c.column)};`;
|
|
111
120
|
case "rename-column": return `ALTER TABLE ${quoteQualified(c.table, c.schema)} RENAME COLUMN ${quote(c.from)} TO ${quote(c.to)};`;
|
|
112
|
-
case "change-column-type": return
|
|
121
|
+
case "change-column-type": return renderAlterColumnType(c, c.from, c.to, c.fromDefault, c.toDefault);
|
|
113
122
|
case "change-column-nullable":
|
|
114
123
|
return c.to
|
|
115
124
|
? `ALTER TABLE ${quoteQualified(c.table, c.schema)} ALTER COLUMN ${quote(c.column)} DROP NOT NULL;`
|
|
@@ -191,7 +200,7 @@ function renderDown(c: Change): string {
|
|
|
191
200
|
? `ALTER TABLE ${quoteQualified(c.table, c.schema)} ADD COLUMN ${renderColumn(c.restore)};\n-- NOTE: column data is not restored by this down migration.`
|
|
192
201
|
: `-- WARNING: down migration cannot restore data\n-- TODO: re-add dropped column "${c.column}" manually with original type/nullable/default`;
|
|
193
202
|
case "rename-column": return `ALTER TABLE ${quoteQualified(c.table, c.schema)} RENAME COLUMN ${quote(c.to)} TO ${quote(c.from)};`;
|
|
194
|
-
case "change-column-type": return
|
|
203
|
+
case "change-column-type": return renderAlterColumnType(c, c.to, c.from, c.toDefault, c.fromDefault);
|
|
195
204
|
case "change-column-nullable":
|
|
196
205
|
return c.from
|
|
197
206
|
? `ALTER TABLE ${quoteQualified(c.table, c.schema)} ALTER COLUMN ${quote(c.column)} DROP NOT NULL;`
|
|
@@ -356,6 +365,151 @@ function fkActionSql(a: FkAction): string {
|
|
|
356
365
|
}
|
|
357
366
|
}
|
|
358
367
|
|
|
368
|
+
type ColumnTypeChange = Extract<Change, { kind: "change-column-type" }>;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* `ALTER COLUMN … TYPE`, with the `USING` clause the conversion needs.
|
|
372
|
+
*
|
|
373
|
+
* Postgres changes a column's type in place only along an ASSIGNMENT cast. Many cross-kind
|
|
374
|
+
* changes have none (jsonb → text[], text[] → jsonb, text → text[], boolean → integer), and the
|
|
375
|
+
* bare ALTER is refused ("cannot be cast automatically"), so a migration `meta migrate` wrote
|
|
376
|
+
* could not be applied by anything. {@link conversion} names each one.
|
|
377
|
+
*
|
|
378
|
+
* A DEFAULT is converted by an assignment cast too, and never through USING, so a default in
|
|
379
|
+
* the way of a converting change is dropped first and set again after. The change carries both
|
|
380
|
+
* defaults for exactly this (types.ts).
|
|
381
|
+
*/
|
|
382
|
+
function renderAlterColumnType(
|
|
383
|
+
c: ColumnTypeChange, from: SqlType, to: SqlType,
|
|
384
|
+
fromDefault: ColumnDefault | undefined, toDefault: ColumnDefault | undefined,
|
|
385
|
+
): string {
|
|
386
|
+
const col = quote(c.column);
|
|
387
|
+
const alterColumn = `ALTER TABLE ${quoteQualified(c.table, c.schema)} ALTER COLUMN ${col}`;
|
|
388
|
+
const { using } = conversion(col, from, to, c.schema);
|
|
389
|
+
const alterType = `${alterColumn} TYPE ${pgType(to)}${using};`;
|
|
390
|
+
if (using === "" && columnDefaultsEqual(fromDefault, toDefault)) return alterType;
|
|
391
|
+
return [
|
|
392
|
+
...(fromDefault !== undefined ? [`${alterColumn} DROP DEFAULT;`] : []),
|
|
393
|
+
alterType,
|
|
394
|
+
...(toDefault !== undefined ? [`${alterColumn} SET DEFAULT ${renderDefault(toDefault)};`] : []),
|
|
395
|
+
].join("\n");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* The `USING` clause for a change (`""` when the assignment cast a bare ALTER uses is right),
|
|
400
|
+
* and the helper function it calls, if any.
|
|
401
|
+
*
|
|
402
|
+
* - jsonb → T[]: a subquery is not allowed in USING, so the rows are unpacked by a helper. It
|
|
403
|
+
* keeps element order, maps a JSON `null` to NULL, and RAISES on a scalar or an object.
|
|
404
|
+
* - T[] → jsonb: `to_jsonb`, since no cast from an array to jsonb exists.
|
|
405
|
+
* - scalar → T[]: the value becomes the array's one element. An explicit `::T[]` would PARSE the
|
|
406
|
+
* text as an array literal instead ('{a,b}' → two elements, 'hello' → an error).
|
|
407
|
+
* - T[] → scalar: the one element a row holds, through a helper that RAISES on a row holding
|
|
408
|
+
* more. Neither alternative is safe: `c[1]` silently keeps the first element, and the bare
|
|
409
|
+
* assignment cast to text stores the array literal '{a,b}' as the value.
|
|
410
|
+
* - anything else cross-kind: an explicit cast, which is the assignment cast where one exists
|
|
411
|
+
* and the only cast where it does not.
|
|
412
|
+
*/
|
|
413
|
+
function conversion(
|
|
414
|
+
col: string, from: SqlType, to: SqlType, schema: string | undefined,
|
|
415
|
+
): { using: string; helper?: ConversionHelper } {
|
|
416
|
+
if (from.kind === "json" && to.kind === "array") {
|
|
417
|
+
const helper = JSONB_ARRAY_TO_TEXT_ARRAY;
|
|
418
|
+
return { using: ` USING ${helperCall(helper, col, schema)}${castIfNeeded(TEXT_ARRAY, to)}`, helper };
|
|
419
|
+
}
|
|
420
|
+
if (from.kind === "array" && to.kind === "json") return { using: ` USING to_jsonb(${col})` };
|
|
421
|
+
if (from.kind !== "array" && to.kind === "array") {
|
|
422
|
+
return {
|
|
423
|
+
using: ` USING (CASE WHEN ${col} IS NULL THEN NULL ELSE ARRAY[${col}] END)`
|
|
424
|
+
+ castIfNeeded({ kind: "array", element: from }, to),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
if (from.kind === "array" && to.kind !== "array") {
|
|
428
|
+
const helper = ARRAY_TO_SCALAR;
|
|
429
|
+
return { using: ` USING ${helperCall(helper, col, schema)}${castIfNeeded(from.element, to)}`, helper };
|
|
430
|
+
}
|
|
431
|
+
const cast = castIfNeeded(from, to);
|
|
432
|
+
return { using: cast === "" ? "" : ` USING ${col}${cast}` };
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* `::T` when a value of `from` has no assignment cast to `to`. A change TO text never needs one,
|
|
437
|
+
* since every type has an assignment cast to text, and must not get one: an explicit cast to
|
|
438
|
+
* VARCHAR(n) TRUNCATES an over-length value that the assignment cast refuses.
|
|
439
|
+
*/
|
|
440
|
+
function castIfNeeded(from: SqlType, to: SqlType): string {
|
|
441
|
+
return needsExplicitCast(from, to) ? `::${pgType(to)}` : "";
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function needsExplicitCast(from: SqlType, to: SqlType): boolean {
|
|
445
|
+
if (to.kind === "text") return false;
|
|
446
|
+
if (from.kind !== to.kind) return true;
|
|
447
|
+
if (from.kind === "array" && to.kind === "array") return needsExplicitCast(from.element, to.element);
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const TEXT_ARRAY: SqlType = { kind: "array", element: { kind: "text" } };
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* A function a conversion calls. It is an ordinary function created in the table's schema
|
|
455
|
+
* before the ALTERs and dropped at the end of the same file, not a `pg_temp` one: a database
|
|
456
|
+
* that revokes TEMP from PUBLIC (a common hardening step) refuses pg_temp, while CREATE on the
|
|
457
|
+
* schema is a privilege the migration already needs for its tables.
|
|
458
|
+
*/
|
|
459
|
+
interface ConversionHelper {
|
|
460
|
+
readonly name: string;
|
|
461
|
+
/** Parameter list, return type and language, as written after the function name. */
|
|
462
|
+
readonly signature: string;
|
|
463
|
+
/** Argument types, as `DROP FUNCTION` needs them. */
|
|
464
|
+
readonly argTypes: string;
|
|
465
|
+
readonly body: string;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const JSONB_ARRAY_TO_TEXT_ARRAY: ConversionHelper = {
|
|
469
|
+
name: "metaobjects_jsonb_array_to_text_array",
|
|
470
|
+
signature: "(j jsonb) RETURNS text[] LANGUAGE sql IMMUTABLE",
|
|
471
|
+
argTypes: "jsonb",
|
|
472
|
+
body: "SELECT CASE WHEN j IS NULL OR jsonb_typeof(j) = 'null' THEN NULL "
|
|
473
|
+
+ "ELSE ARRAY(SELECT e FROM jsonb_array_elements_text(j) WITH ORDINALITY AS x(e, i) ORDER BY i) END",
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
const ARRAY_TO_SCALAR: ConversionHelper = {
|
|
477
|
+
name: "metaobjects_array_to_scalar",
|
|
478
|
+
signature: "(a anyarray) RETURNS anyelement LANGUAGE plpgsql IMMUTABLE",
|
|
479
|
+
argTypes: "anyarray",
|
|
480
|
+
body: "BEGIN IF cardinality(a) > 1 THEN RAISE EXCEPTION "
|
|
481
|
+
+ "'metaobjects: cannot narrow an array of % elements to one value', cardinality(a); "
|
|
482
|
+
+ "END IF; RETURN a[1]; END",
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
function helperCall(helper: ConversionHelper, col: string, schema: string | undefined): string {
|
|
486
|
+
return `${quoteQualified(helper.name, schema)}(${col})`;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** The helpers one direction of the migration calls: each once per schema, in a stable order. */
|
|
490
|
+
function conversionHelpers(
|
|
491
|
+
changes: readonly Change[], direction: "up" | "down",
|
|
492
|
+
): { helper: ConversionHelper; schema: string | undefined }[] {
|
|
493
|
+
const needed = new Map<string, { helper: ConversionHelper; schema: string | undefined }>();
|
|
494
|
+
for (const c of changes) {
|
|
495
|
+
if (c.kind !== "change-column-type") continue;
|
|
496
|
+
const [from, to] = direction === "up" ? [c.from, c.to] : [c.to, c.from];
|
|
497
|
+
const { helper } = conversion("", from, to, c.schema);
|
|
498
|
+
if (helper === undefined) continue;
|
|
499
|
+
const schema = c.schema === DEFAULT_DB_SCHEMA_POSTGRES ? undefined : c.schema;
|
|
500
|
+
needed.set(`${schema ?? ""}.${helper.name}`, { helper, schema });
|
|
501
|
+
}
|
|
502
|
+
return [...needed.keys()].sort().flatMap((k) => needed.get(k) ?? []);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function createHelper({ helper, schema }: { helper: ConversionHelper; schema: string | undefined }): string {
|
|
506
|
+
return `CREATE OR REPLACE FUNCTION ${quoteQualified(helper.name, schema)}${helper.signature} AS $$ ${helper.body} $$;`;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function dropHelper({ helper, schema }: { helper: ConversionHelper; schema: string | undefined }): string {
|
|
510
|
+
return `DROP FUNCTION IF EXISTS ${quoteQualified(helper.name, schema)}(${helper.argTypes});`;
|
|
511
|
+
}
|
|
512
|
+
|
|
359
513
|
function quote(ident: string): string {
|
|
360
514
|
// Conservative double-quoting; reject embedded quotes (defense).
|
|
361
515
|
if (ident.includes('"')) throw new Error(`unsafe identifier: ${ident}`);
|
package/src/errors.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Change, ChangeKind } from "./types.js";
|
|
1
|
+
import type { AllowOptions, Change, ChangeKind } from "./types.js";
|
|
2
|
+
import { dropsAutoSequenceDefault } from "./pg-identity-default.js";
|
|
2
3
|
|
|
3
4
|
// ---------------------------------------------------------------------------
|
|
4
5
|
// SetNullNotNullableError — surfaced by buildExpectedSchema
|
|
@@ -33,19 +34,33 @@ export class SetNullNotNullableError extends Error {
|
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
const
|
|
37
|
-
"drop-column": "
|
|
38
|
-
"drop-table": "
|
|
39
|
-
"change-column-type": "
|
|
40
|
-
"change-column-nullable": "
|
|
41
|
-
"drop-index": "
|
|
42
|
-
"drop-fk": "
|
|
37
|
+
const ALLOW_OPTION_BY_KIND: Partial<Record<ChangeKind, keyof AllowOptions>> = {
|
|
38
|
+
"drop-column": "dropColumn",
|
|
39
|
+
"drop-table": "dropTable",
|
|
40
|
+
"change-column-type": "typeChange",
|
|
41
|
+
"change-column-nullable": "nullableToNotNull",
|
|
42
|
+
"drop-index": "dropIndex",
|
|
43
|
+
"drop-fk": "dropFk",
|
|
44
|
+
"drop-check": "dropCheck",
|
|
43
45
|
// change-column-default is blocked in exactly one shape — dropping a live
|
|
44
46
|
// Postgres auto-sequence default with no declared @generation (see
|
|
45
47
|
// diff/status.ts) — and allow.dropIdentityDefault is the flag that enables it.
|
|
46
|
-
"change-column-default": "
|
|
48
|
+
"change-column-default": "dropIdentityDefault",
|
|
47
49
|
};
|
|
48
50
|
|
|
51
|
+
/**
|
|
52
|
+
* The `AllowOptions` field that unblocks `c`, chosen by what BLOCKED it rather than by its kind
|
|
53
|
+
* alone. A type change carries its column's default change, so one that would drop a live
|
|
54
|
+
* auto-sequence default is blocked by THAT, and `typeChange` would not unblock it. The single
|
|
55
|
+
* source for this error's hints and the CLI's `--allow` token.
|
|
56
|
+
*/
|
|
57
|
+
export function allowOptionFor(c: Change): keyof AllowOptions | undefined {
|
|
58
|
+
if (c.kind === "change-column-type" && dropsAutoSequenceDefault(c.fromDefault, c.toDefault)) {
|
|
59
|
+
return "dropIdentityDefault";
|
|
60
|
+
}
|
|
61
|
+
return ALLOW_OPTION_BY_KIND[c.kind];
|
|
62
|
+
}
|
|
63
|
+
|
|
49
64
|
function changeLocator(c: Change): string {
|
|
50
65
|
switch (c.kind) {
|
|
51
66
|
case "drop-column":
|
|
@@ -88,7 +103,8 @@ export class BlockedChangesError extends Error {
|
|
|
88
103
|
|
|
89
104
|
constructor(blocked: Change[]) {
|
|
90
105
|
const hints = blocked.map((c) => {
|
|
91
|
-
const
|
|
106
|
+
const option = allowOptionFor(c);
|
|
107
|
+
const flag = option === undefined ? "(no flag enables this)" : `allow.${option}`;
|
|
92
108
|
return `${c.kind} on ${changeLocator(c)}: pass ${flag}`;
|
|
93
109
|
});
|
|
94
110
|
const msg = `${blocked.length} blocked change(s):\n - ${hints.join("\n - ")}`;
|
package/src/index.ts
CHANGED
|
@@ -47,7 +47,7 @@ export { planOffline, baselineFromMetadata } from "./snapshot/plan.js";
|
|
|
47
47
|
export type { PlanOfflineArgs, PlanOfflineResult } from "./snapshot/plan.js";
|
|
48
48
|
|
|
49
49
|
// Errors
|
|
50
|
-
export { BlockedChangesError, SetNullNotNullableError, PrimaryKeyChangeError } from "./errors.js";
|
|
50
|
+
export { BlockedChangesError, SetNullNotNullableError, PrimaryKeyChangeError, allowOptionFor } from "./errors.js";
|
|
51
51
|
|
|
52
52
|
// SqlType helpers (rarely needed but useful for advanced consumers)
|
|
53
53
|
export { isWidening, sqlTypeEquals } from "./sql-type.js";
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { ColumnDefault } from "./types.js";
|
|
2
|
+
|
|
1
3
|
// src/pg-identity-default.ts
|
|
2
4
|
//
|
|
3
5
|
// Postgres "auto-sequence" column DEFAULT — the `nextval('<seq>'::regclass)`
|
|
@@ -17,3 +19,11 @@
|
|
|
17
19
|
export function isPgAutoSequenceDefault(raw: string | null | undefined): boolean {
|
|
18
20
|
return raw !== null && raw !== undefined && /^nextval\(/i.test(raw);
|
|
19
21
|
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A default change that DROPS a live auto-sequence default: the one default shape the diff
|
|
25
|
+
* gates (diff/status.ts), whether it arrives as its own change or folded into a type change.
|
|
26
|
+
*/
|
|
27
|
+
export function dropsAutoSequenceDefault(from: ColumnDefault | undefined, to: ColumnDefault | undefined): boolean {
|
|
28
|
+
return to === undefined && from?.kind === "expr" && isPgAutoSequenceDefault(from.value);
|
|
29
|
+
}
|