@uncutsql/core 0.7.0 → 0.9.0
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/README.md +8 -3
- package/dist/index.cjs +585 -93
- package/dist/index.d.cts +169 -22
- package/dist/index.d.ts +169 -22
- package/dist/index.js +582 -95
- package/package.json +4 -3
package/dist/index.cjs
CHANGED
|
@@ -200,6 +200,35 @@ var SchemaDiffError = class extends effect.Data.TaggedError("SchemaDiffError") {
|
|
|
200
200
|
return this.diagnostics.map((item) => `${item.side}: ${item.message}`).join("\n");
|
|
201
201
|
}
|
|
202
202
|
};
|
|
203
|
+
/**
|
|
204
|
+
* Every column a table change carries over: changed, renamed and unchanged ones. A table option
|
|
205
|
+
* can change how a column with an unchanged declaration stores values (SQLite `STRICT`), so the
|
|
206
|
+
* dialect classifies every pair, not only declaration changes (ADR 0042).
|
|
207
|
+
*/
|
|
208
|
+
const pairedColumns = (before, after, changes) => {
|
|
209
|
+
const pairs = [];
|
|
210
|
+
const touchedBefore = /* @__PURE__ */ new Set();
|
|
211
|
+
const touchedAfter = /* @__PURE__ */ new Set();
|
|
212
|
+
for (const change of changes) if (change._tag === "ColumnChanged" || change._tag === "ColumnRenamed") {
|
|
213
|
+
pairs.push({
|
|
214
|
+
after: change.after,
|
|
215
|
+
before: change.before,
|
|
216
|
+
renamed: change._tag === "ColumnRenamed"
|
|
217
|
+
});
|
|
218
|
+
touchedBefore.add(change.before.key);
|
|
219
|
+
touchedAfter.add(change.after.key);
|
|
220
|
+
} else if (change._tag === "ColumnAdded") touchedAfter.add(change.column.key);
|
|
221
|
+
else if (change._tag === "ColumnRemoved") touchedBefore.add(change.column.key);
|
|
222
|
+
for (const column of after.columns) {
|
|
223
|
+
const source = touchedAfter.has(column.key) ? void 0 : before.columns.find((item) => item.key === column.key && !touchedBefore.has(item.key));
|
|
224
|
+
if (source) pairs.push({
|
|
225
|
+
after: column,
|
|
226
|
+
before: source,
|
|
227
|
+
renamed: false
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
return pairs;
|
|
231
|
+
};
|
|
203
232
|
const validateDatabase = (database, side) => {
|
|
204
233
|
const diagnostics = [];
|
|
205
234
|
const tables = /* @__PURE__ */ new Set();
|
|
@@ -1026,6 +1055,7 @@ const diffDatabases = (before, after) => effect.Effect.gen(function* () {
|
|
|
1026
1055
|
/** Low-level logical lowering of caller-approved intent. Resolve migration decisions first in user-facing workflows. */
|
|
1027
1056
|
const planDatabaseMigration = (diff) => ({
|
|
1028
1057
|
...diff.dropGuards ? { dropGuards: diff.dropGuards } : {},
|
|
1058
|
+
...diff.typeChanges ? { typeChanges: diff.typeChanges } : {},
|
|
1029
1059
|
procedures: diff.changes.map((change) => {
|
|
1030
1060
|
switch (change._tag) {
|
|
1031
1061
|
case "NamespaceAdded":
|
|
@@ -1322,6 +1352,157 @@ function swap(value) {
|
|
|
1322
1352
|
before: value.after
|
|
1323
1353
|
};
|
|
1324
1354
|
}
|
|
1355
|
+
// src/migration-checksum.ts
|
|
1356
|
+
const encoder = new TextEncoder();
|
|
1357
|
+
const domain = encoder.encode("uncutsql-migration-pair-v1\0");
|
|
1358
|
+
const blockSize = 64;
|
|
1359
|
+
const digestSize = 32;
|
|
1360
|
+
const initialState = [
|
|
1361
|
+
1779033703,
|
|
1362
|
+
3144134277,
|
|
1363
|
+
1013904242,
|
|
1364
|
+
2773480762,
|
|
1365
|
+
1359893119,
|
|
1366
|
+
2600822924,
|
|
1367
|
+
528734635,
|
|
1368
|
+
1541459225
|
|
1369
|
+
];
|
|
1370
|
+
const roundConstants = [
|
|
1371
|
+
1116352408,
|
|
1372
|
+
1899447441,
|
|
1373
|
+
3049323471,
|
|
1374
|
+
3921009573,
|
|
1375
|
+
961987163,
|
|
1376
|
+
1508970993,
|
|
1377
|
+
2453635748,
|
|
1378
|
+
2870763221,
|
|
1379
|
+
3624381080,
|
|
1380
|
+
310598401,
|
|
1381
|
+
607225278,
|
|
1382
|
+
1426881987,
|
|
1383
|
+
1925078388,
|
|
1384
|
+
2162078206,
|
|
1385
|
+
2614888103,
|
|
1386
|
+
3248222580,
|
|
1387
|
+
3835390401,
|
|
1388
|
+
4022224774,
|
|
1389
|
+
264347078,
|
|
1390
|
+
604807628,
|
|
1391
|
+
770255983,
|
|
1392
|
+
1249150122,
|
|
1393
|
+
1555081692,
|
|
1394
|
+
1996064986,
|
|
1395
|
+
2554220882,
|
|
1396
|
+
2821834349,
|
|
1397
|
+
2952996808,
|
|
1398
|
+
3210313671,
|
|
1399
|
+
3336571891,
|
|
1400
|
+
3584528711,
|
|
1401
|
+
113926993,
|
|
1402
|
+
338241895,
|
|
1403
|
+
666307205,
|
|
1404
|
+
773529912,
|
|
1405
|
+
1294757372,
|
|
1406
|
+
1396182291,
|
|
1407
|
+
1695183700,
|
|
1408
|
+
1986661051,
|
|
1409
|
+
2177026350,
|
|
1410
|
+
2456956037,
|
|
1411
|
+
2730485921,
|
|
1412
|
+
2820302411,
|
|
1413
|
+
3259730800,
|
|
1414
|
+
3345764771,
|
|
1415
|
+
3516065817,
|
|
1416
|
+
3600352804,
|
|
1417
|
+
4094571909,
|
|
1418
|
+
275423344,
|
|
1419
|
+
430227734,
|
|
1420
|
+
506948616,
|
|
1421
|
+
659060556,
|
|
1422
|
+
883997877,
|
|
1423
|
+
958139571,
|
|
1424
|
+
1322822218,
|
|
1425
|
+
1537002063,
|
|
1426
|
+
1747873779,
|
|
1427
|
+
1955562222,
|
|
1428
|
+
2024104815,
|
|
1429
|
+
2227730452,
|
|
1430
|
+
2361852424,
|
|
1431
|
+
2428436474,
|
|
1432
|
+
2756734187,
|
|
1433
|
+
3204031479,
|
|
1434
|
+
3329325298
|
|
1435
|
+
];
|
|
1436
|
+
function rotateRight(word, bits) {
|
|
1437
|
+
return word >>> bits | word << 32 - bits;
|
|
1438
|
+
}
|
|
1439
|
+
function compress(state, schedule, bytes, offset) {
|
|
1440
|
+
for (let i = 0; i < 16; i++) schedule[i] = bytes.getUint32(offset + i * 4);
|
|
1441
|
+
for (let i = 16; i < schedule.length; i++) {
|
|
1442
|
+
const word15 = schedule[i - 15];
|
|
1443
|
+
const word2 = schedule[i - 2];
|
|
1444
|
+
const sigma0 = rotateRight(word15, 7) ^ rotateRight(word15, 18) ^ word15 >>> 3;
|
|
1445
|
+
const sigma1 = rotateRight(word2, 17) ^ rotateRight(word2, 19) ^ word2 >>> 10;
|
|
1446
|
+
schedule[i] = schedule[i - 16] + sigma0 + schedule[i - 7] + sigma1;
|
|
1447
|
+
}
|
|
1448
|
+
let a = state[0];
|
|
1449
|
+
let b = state[1];
|
|
1450
|
+
let c = state[2];
|
|
1451
|
+
let d = state[3];
|
|
1452
|
+
let e = state[4];
|
|
1453
|
+
let f = state[5];
|
|
1454
|
+
let g = state[6];
|
|
1455
|
+
let h = state[7];
|
|
1456
|
+
for (let i = 0; i < schedule.length; i++) {
|
|
1457
|
+
const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
|
|
1458
|
+
const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
|
|
1459
|
+
const choose = e & f ^ ~e & g;
|
|
1460
|
+
const majority = a & b ^ a & c ^ b & c;
|
|
1461
|
+
const temp1 = h + sum1 + choose + roundConstants[i] + schedule[i];
|
|
1462
|
+
const temp2 = sum0 + majority;
|
|
1463
|
+
h = g;
|
|
1464
|
+
g = f;
|
|
1465
|
+
f = e;
|
|
1466
|
+
e = d + temp1 >>> 0;
|
|
1467
|
+
d = c;
|
|
1468
|
+
c = b;
|
|
1469
|
+
b = a;
|
|
1470
|
+
a = temp1 + temp2 >>> 0;
|
|
1471
|
+
}
|
|
1472
|
+
state[0] = state[0] + a;
|
|
1473
|
+
state[1] = state[1] + b;
|
|
1474
|
+
state[2] = state[2] + c;
|
|
1475
|
+
state[3] = state[3] + d;
|
|
1476
|
+
state[4] = state[4] + e;
|
|
1477
|
+
state[5] = state[5] + f;
|
|
1478
|
+
state[6] = state[6] + g;
|
|
1479
|
+
state[7] = state[7] + h;
|
|
1480
|
+
}
|
|
1481
|
+
function sha256(bytes) {
|
|
1482
|
+
const state = new Uint32Array(initialState);
|
|
1483
|
+
const schedule = new Uint32Array(roundConstants.length);
|
|
1484
|
+
const input = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1485
|
+
const remainder = bytes.byteLength % blockSize;
|
|
1486
|
+
const tailOffset = bytes.byteLength - remainder;
|
|
1487
|
+
for (let offset = 0; offset < tailOffset; offset += blockSize) compress(state, schedule, input, offset);
|
|
1488
|
+
const tail = new Uint8Array(remainder + 9 <= blockSize ? blockSize : 128);
|
|
1489
|
+
tail.set(bytes.subarray(tailOffset));
|
|
1490
|
+
tail[remainder] = 128;
|
|
1491
|
+
const padded = new DataView(tail.buffer);
|
|
1492
|
+
padded.setBigUint64(tail.byteLength - 8, BigInt(bytes.byteLength) * 8n);
|
|
1493
|
+
for (let offset = 0; offset < tail.byteLength; offset += blockSize) compress(state, schedule, padded, offset);
|
|
1494
|
+
const digest = new Uint8Array(digestSize);
|
|
1495
|
+
const output = new DataView(digest.buffer);
|
|
1496
|
+
for (let i = 0; i < state.length; i++) output.setUint32(i * 4, state[i]);
|
|
1497
|
+
return digest;
|
|
1498
|
+
}
|
|
1499
|
+
function hashMigrationPair(up, down) {
|
|
1500
|
+
const pair = new Uint8Array(domain.length + 64);
|
|
1501
|
+
pair.set(domain);
|
|
1502
|
+
pair.set(sha256(typeof up === "string" ? encoder.encode(up) : up), domain.length);
|
|
1503
|
+
pair.set(sha256(typeof down === "string" ? encoder.encode(down) : down), domain.length + digestSize);
|
|
1504
|
+
return Array.from(sha256(pair), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1505
|
+
}
|
|
1325
1506
|
// src/dialect.ts
|
|
1326
1507
|
var StatementEditError = class extends effect.Data.TaggedError("StatementEditError") {};
|
|
1327
1508
|
// src/structure.ts
|
|
@@ -1671,13 +1852,17 @@ const formatExtractOmissions = (omitted) => [...omitted].sort((left, right) => o
|
|
|
1671
1852
|
});
|
|
1672
1853
|
const checksumPattern = /^[a-f0-9]{64}$/;
|
|
1673
1854
|
/** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
|
|
1674
|
-
const migrationRun = (direction, migrations, checkpoint
|
|
1855
|
+
const migrationRun = (direction, migrations, checkpoint) => {
|
|
1675
1856
|
const down = direction === "down";
|
|
1676
1857
|
const baseline = migrations[0]?.baselineCheckpoint !== void 0 ? migrations[0] : void 0;
|
|
1677
1858
|
const checkpointId = checkpoint?.id ?? 0;
|
|
1678
1859
|
const firstUpId = baseline?.id ?? checkpointId + 1;
|
|
1679
1860
|
const offset = down ? Math.max(0, checkpointId - 1) : firstUpId - 1;
|
|
1680
|
-
if (baseline !== void 0 && (down || checkpointId !== 0 || !Number.isSafeInteger(baseline.id) || baseline.id < 2 || !checksumPattern.test(baseline.baselineCheckpoint)) || down && checkpoint !== void 0 && (migrations.length !== (checkpoint.id ? 1 : 0) || checkpoint.id > 0 && migrations[0]?.sha256 !== checkpoint.sha256) || migrations.some((migration, index) =>
|
|
1861
|
+
if (baseline !== void 0 && (down || checkpointId !== 0 || !Number.isSafeInteger(baseline.id) || baseline.id < 2 || !checksumPattern.test(baseline.baselineCheckpoint)) || down && checkpoint !== void 0 && (migrations.length !== (checkpoint.id ? 1 : 0) || checkpoint.id > 0 && migrations[0]?.sha256 !== checkpoint.sha256) || migrations.some((migration, index) => {
|
|
1862
|
+
const { down, sql } = migration;
|
|
1863
|
+
const downText = "sql" in down ? down.sql : down.text;
|
|
1864
|
+
return index > 0 && migration.baselineCheckpoint !== void 0 || migration.id !== offset + index + 1 || !sql.trim() || sql.includes("\0") || !downText.trim() || downText.includes("\0") || hashMigrationPair(sql, downText) !== migration.sha256;
|
|
1865
|
+
})) return;
|
|
1681
1866
|
return {
|
|
1682
1867
|
...baseline ? { baseline } : {},
|
|
1683
1868
|
direction,
|
|
@@ -1696,6 +1881,7 @@ const nextMigration = (run, migrations, tipId) => run.baseline && tipId === 0 ?
|
|
|
1696
1881
|
var MigrationDecisionError = class extends effect.Data.TaggedError("MigrationDecisionError") {};
|
|
1697
1882
|
const equal = (a, b) => schemaValueEqual(a, b);
|
|
1698
1883
|
const record = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1884
|
+
const isJson = effect.Schema.is(effect.Schema.Json);
|
|
1699
1885
|
const idFor = (table, column) => JSON.stringify([tableKey(table), column ?? null]);
|
|
1700
1886
|
const renamedColumnId = (source, target, column) => JSON.stringify([
|
|
1701
1887
|
tableKey(source),
|
|
@@ -1710,12 +1896,103 @@ const valueId = (source, target, value) => JSON.stringify([
|
|
|
1710
1896
|
tableKey(target),
|
|
1711
1897
|
value
|
|
1712
1898
|
]);
|
|
1713
|
-
const
|
|
1899
|
+
const qualifiedName = (identity) => [
|
|
1714
1900
|
identity.catalog,
|
|
1715
1901
|
identity.schema,
|
|
1716
1902
|
identity.name
|
|
1717
1903
|
].filter((part) => part !== void 0).join(".");
|
|
1718
1904
|
const quoted = (value) => `'${JSON.stringify(value).slice(1, -1)}'`;
|
|
1905
|
+
const isTypeChangeKind = (kind) => kind === "type_change_loss" || kind === "type_change_failure";
|
|
1906
|
+
const typeChangeId = (kind, change, support) => {
|
|
1907
|
+
const { source, target } = support.displayTypes(change);
|
|
1908
|
+
return JSON.stringify([
|
|
1909
|
+
kind,
|
|
1910
|
+
tableKey(change.before.table.identity),
|
|
1911
|
+
tableKey(change.after.table.identity),
|
|
1912
|
+
change.before.column.key,
|
|
1913
|
+
change.after.column.key,
|
|
1914
|
+
source,
|
|
1915
|
+
target
|
|
1916
|
+
]);
|
|
1917
|
+
};
|
|
1918
|
+
const typeChangePair = (document, id, kind, support, renames) => {
|
|
1919
|
+
if (!isTypeChangeKind(kind)) return;
|
|
1920
|
+
let parts;
|
|
1921
|
+
try {
|
|
1922
|
+
parts = JSON.parse(id);
|
|
1923
|
+
} catch {
|
|
1924
|
+
return;
|
|
1925
|
+
}
|
|
1926
|
+
if (!Array.isArray(parts) || parts.length !== 7 || parts[0] !== kind) return;
|
|
1927
|
+
const [, source, target, sourceColumn, targetColumn] = parts;
|
|
1928
|
+
const side = (schema, tableId, columnKey) => {
|
|
1929
|
+
const table = schema.tables.find((item) => tableKey(item.identity) === tableId);
|
|
1930
|
+
const column = table?.columns.find((item) => item.key === columnKey);
|
|
1931
|
+
return table && column ? {
|
|
1932
|
+
column,
|
|
1933
|
+
schema,
|
|
1934
|
+
table
|
|
1935
|
+
} : void 0;
|
|
1936
|
+
};
|
|
1937
|
+
const before = side(document.before, source, sourceColumn);
|
|
1938
|
+
const after = side(document.after, target, targetColumn);
|
|
1939
|
+
if (before && after && id === typeChangeId(kind, {
|
|
1940
|
+
after,
|
|
1941
|
+
before
|
|
1942
|
+
}, support)) return {
|
|
1943
|
+
after,
|
|
1944
|
+
before,
|
|
1945
|
+
...renames ? { enumRenames: renames } : {}
|
|
1946
|
+
};
|
|
1947
|
+
};
|
|
1948
|
+
const validateFallback = (document, decision, literal, typeChange, renames) => {
|
|
1949
|
+
const pair = typeChangePair(document, decision.id, decision.kind, typeChange, renames);
|
|
1950
|
+
return pair ? typeChange.validateFallback(pair, literal) : "The type-change decision does not identify a column pair in the snapshots.";
|
|
1951
|
+
};
|
|
1952
|
+
const typeChangeReviews = (diff, schemas, support, scope, renames) => {
|
|
1953
|
+
if (!support) return [];
|
|
1954
|
+
const reviews = [];
|
|
1955
|
+
for (const table of diff.changes) {
|
|
1956
|
+
if (!("changes" in table)) continue;
|
|
1957
|
+
for (const column of pairedColumns(table.before, table.after, table.changes)) {
|
|
1958
|
+
if (scope !== "all" && scope === "nested" !== (table._tag === "TableRenamed" || column.renamed)) continue;
|
|
1959
|
+
const change = {
|
|
1960
|
+
after: {
|
|
1961
|
+
column: column.after,
|
|
1962
|
+
schema: schemas.after,
|
|
1963
|
+
table: table.after
|
|
1964
|
+
},
|
|
1965
|
+
before: {
|
|
1966
|
+
column: column.before,
|
|
1967
|
+
schema: schemas.before,
|
|
1968
|
+
table: table.before
|
|
1969
|
+
},
|
|
1970
|
+
...renames ? { enumRenames: renames } : {}
|
|
1971
|
+
};
|
|
1972
|
+
const classification = support.classify(change);
|
|
1973
|
+
if (classification === void 0 || classification === "lossless") continue;
|
|
1974
|
+
const { source, target } = support.displayTypes(change);
|
|
1975
|
+
const label = `${qualifiedName(table.after.identity)}.${column.after.name}`;
|
|
1976
|
+
const question = (kind, text) => ({
|
|
1977
|
+
column: column.before.key,
|
|
1978
|
+
id: typeChangeId(kind, change, support),
|
|
1979
|
+
kind,
|
|
1980
|
+
question: text,
|
|
1981
|
+
resolution: { action: "pending" },
|
|
1982
|
+
table: table.before.identity
|
|
1983
|
+
});
|
|
1984
|
+
const questions = [];
|
|
1985
|
+
if (classification !== "may_lose") questions.push(question("type_change_failure", `How should the migration handle values that cannot convert when changing ${label} from ${source} to ${target}?`));
|
|
1986
|
+
if (classification !== "may_fail") questions.push(question("type_change_loss", `How should the migration handle values that would lose information when converting ${label} from ${source} to ${target}?`));
|
|
1987
|
+
reviews.push({
|
|
1988
|
+
change,
|
|
1989
|
+
classification,
|
|
1990
|
+
questions
|
|
1991
|
+
});
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
return reviews;
|
|
1995
|
+
};
|
|
1719
1996
|
const missingFrom = (values, other) => {
|
|
1720
1997
|
const present = new Set(other);
|
|
1721
1998
|
return values.filter((value) => !present.has(value));
|
|
@@ -1729,7 +2006,7 @@ const enumQuestions = (change, diff) => {
|
|
|
1729
2006
|
candidates,
|
|
1730
2007
|
enum: identity,
|
|
1731
2008
|
id: enumId(identity),
|
|
1732
|
-
question: `Drop type ${
|
|
2009
|
+
question: `Drop type ${qualifiedName(identity)}, or rename it to an added type?`,
|
|
1733
2010
|
resolution: { action: "pending" }
|
|
1734
2011
|
}] : [];
|
|
1735
2012
|
}
|
|
@@ -1739,7 +2016,7 @@ const enumQuestions = (change, diff) => {
|
|
|
1739
2016
|
candidates,
|
|
1740
2017
|
enum: before.identity,
|
|
1741
2018
|
id: valueId(before.identity, after.identity, value),
|
|
1742
|
-
question: `Rename value ${quoted(value)} of ${
|
|
2019
|
+
question: `Rename value ${quoted(value)} of ${qualifiedName(before.identity)} to an added value?`,
|
|
1743
2020
|
resolution: { action: "pending" },
|
|
1744
2021
|
value
|
|
1745
2022
|
})) : [];
|
|
@@ -1767,13 +2044,27 @@ const questions = (diff) => diff.changes.flatMap((change) => {
|
|
|
1767
2044
|
table: change.table
|
|
1768
2045
|
}));
|
|
1769
2046
|
});
|
|
1770
|
-
const
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
2047
|
+
const defersTypeChanges = (decisions) => decisions.some((item) => "enum" in item && item.candidates.length > 0);
|
|
2048
|
+
const rootReview = (diff, before, after, typeChange) => {
|
|
2049
|
+
const removals = questions(diff);
|
|
2050
|
+
const typeChanges = defersTypeChanges(removals) ? [] : typeChangeReviews(diff, {
|
|
2051
|
+
after,
|
|
2052
|
+
before
|
|
2053
|
+
}, typeChange, "root");
|
|
2054
|
+
return {
|
|
2055
|
+
document: {
|
|
2056
|
+
after,
|
|
2057
|
+
before,
|
|
2058
|
+
decisions: [...removals, ...typeChanges.flatMap((review) => review.questions)],
|
|
2059
|
+
version: 1
|
|
2060
|
+
},
|
|
2061
|
+
typeChanges
|
|
2062
|
+
};
|
|
2063
|
+
};
|
|
2064
|
+
function prepareMigrationDecisions(before, after, typeChange) {
|
|
2065
|
+
return effect.Effect.map(diffDatabases(before, after), (diff) => rootReview(diff, before, after, typeChange).document);
|
|
2066
|
+
}
|
|
2067
|
+
const readAnswers = (pending, inputs, allowPending, typeChange, renames) => effect.Effect.gen(function* () {
|
|
1777
2068
|
const fail = (message) => effect.Effect.fail(new MigrationDecisionError({
|
|
1778
2069
|
message,
|
|
1779
2070
|
pending
|
|
@@ -1785,13 +2076,37 @@ const readAnswers = (pending, inputs, allowPending) => effect.Effect.gen(functio
|
|
|
1785
2076
|
if (!record(input) || typeof input.id !== "string") return yield* fail("Each decision needs its original id.");
|
|
1786
2077
|
const question = expected.get(input.id);
|
|
1787
2078
|
if (!question || answers.has(input.id)) return yield* fail(`Unknown or duplicate decision: ${input.id}`);
|
|
1788
|
-
if (!("enum" in question ? input.table === void 0 && input.column === void 0 && equal(input.enum, question.enum) && input.value === question.value : input.enum === void 0 && input.value === void 0 && equal(input.table, question.table) && input.column === question.column)) return yield* fail(`Decision identity changed: ${input.id}`);
|
|
2079
|
+
if (!("enum" in question ? input.table === void 0 && input.column === void 0 && equal(input.enum, question.enum) && input.value === question.value : input.enum === void 0 && input.value === void 0 && equal(input.table, question.table) && input.column === question.column) || "kind" in question && input.kind !== question.kind) return yield* fail(`Decision identity changed: ${input.id}`);
|
|
1789
2080
|
const resolution = input.resolution;
|
|
1790
2081
|
if (allowPending && record(resolution) && resolution.action === "pending" && Object.keys(resolution).length === 1) {
|
|
1791
2082
|
answers.set(input.id, question);
|
|
1792
2083
|
continue;
|
|
1793
2084
|
}
|
|
1794
2085
|
if (!record(resolution) || resolution.action === "pending") return yield* fail(`Unresolved decision: ${question.question}`);
|
|
2086
|
+
if ("kind" in question) {
|
|
2087
|
+
const action = resolution.action;
|
|
2088
|
+
if (Object.keys(resolution).length === 1 && ((action === "accept_loss" || action === "guard") && question.kind === "type_change_loss" || action === "accept_failure" && question.kind === "type_change_failure")) {
|
|
2089
|
+
answers.set(input.id, {
|
|
2090
|
+
...question,
|
|
2091
|
+
resolution: { action }
|
|
2092
|
+
});
|
|
2093
|
+
continue;
|
|
2094
|
+
}
|
|
2095
|
+
const fallback = resolution.fallback;
|
|
2096
|
+
if (question.kind === "type_change_failure" && Object.keys(resolution).length === 1 && record(fallback) && Object.keys(fallback).length === 1 && isJson(fallback.literal)) {
|
|
2097
|
+
const literal = fallback.literal;
|
|
2098
|
+
if (!allowPending && typeChange) {
|
|
2099
|
+
const reason = validateFallback(pending, question, literal, typeChange, renames);
|
|
2100
|
+
if (reason !== void 0) return yield* fail(`Invalid fallback for ${question.question} ${quoted(reason)}`);
|
|
2101
|
+
}
|
|
2102
|
+
answers.set(input.id, {
|
|
2103
|
+
...question,
|
|
2104
|
+
resolution: { fallback: { literal } }
|
|
2105
|
+
});
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
return yield* fail(`Invalid resolution for ${question.question}`);
|
|
2109
|
+
}
|
|
1795
2110
|
if ((resolution.action === "drop" || resolution.action === "drop_if_empty" && !("enum" in question)) && Object.keys(resolution).length === 1) answers.set(input.id, {
|
|
1796
2111
|
...question,
|
|
1797
2112
|
resolution: { action: resolution.action }
|
|
@@ -1815,7 +2130,7 @@ const readAnswers = (pending, inputs, allowPending) => effect.Effect.gen(functio
|
|
|
1815
2130
|
});
|
|
1816
2131
|
} else return yield* fail(`Invalid resolution for ${question.question}`);
|
|
1817
2132
|
}
|
|
1818
|
-
if (answers.size !== expected.size) return yield* fail("Missing migration decisions: every removal requires an explicit resolution.");
|
|
2133
|
+
if (answers.size !== expected.size) return yield* fail(pending.decisions.some((item) => "kind" in item && !answers.has(item.id)) ? "Missing migration decisions: every type-change question requires an explicit resolution." : "Missing migration decisions: every removal requires an explicit resolution.");
|
|
1819
2134
|
return answers;
|
|
1820
2135
|
});
|
|
1821
2136
|
const expandRenames = (diff, answers) => {
|
|
@@ -1859,7 +2174,47 @@ const expandRenames = (diff, answers) => {
|
|
|
1859
2174
|
nested: questions({ changes: renames })
|
|
1860
2175
|
};
|
|
1861
2176
|
};
|
|
2177
|
+
const pairColumns = (diff, resolutionFor) => ({ changes: diff.changes.map((change) => {
|
|
2178
|
+
if (!("changes" in change)) return change;
|
|
2179
|
+
const renamedSources = /* @__PURE__ */ new Set();
|
|
2180
|
+
const renamedTargets = /* @__PURE__ */ new Set();
|
|
2181
|
+
const renames = [];
|
|
2182
|
+
for (const item of change.changes) {
|
|
2183
|
+
if (item._tag !== "ColumnRemoved") continue;
|
|
2184
|
+
const resolution = resolutionFor(columnId(change, item.column.key));
|
|
2185
|
+
if (!record(resolution) || resolution.action !== "rename") continue;
|
|
2186
|
+
const target = change.changes.find((candidate) => candidate._tag === "ColumnAdded" && candidate.column.key === resolution.target);
|
|
2187
|
+
if (target?._tag !== "ColumnAdded") continue;
|
|
2188
|
+
renamedSources.add(item.column.key);
|
|
2189
|
+
renamedTargets.add(target.column.key);
|
|
2190
|
+
const fields = [];
|
|
2191
|
+
if (!equal(item.column.dataType, target.column.dataType)) fields.push("dataType");
|
|
2192
|
+
if (!constraintsEqual(item.column.constraints, target.column.constraints)) fields.push("constraints");
|
|
2193
|
+
renames.push({
|
|
2194
|
+
_tag: "ColumnRenamed",
|
|
2195
|
+
after: target.column,
|
|
2196
|
+
before: item.column,
|
|
2197
|
+
fields
|
|
2198
|
+
});
|
|
2199
|
+
}
|
|
2200
|
+
return {
|
|
2201
|
+
...change,
|
|
2202
|
+
changes: [...renames, ...change.changes.filter((item) => !(item._tag === "ColumnAdded" && renamedTargets.has(item.column.key)) && !(item._tag === "ColumnRemoved" && renamedSources.has(item.column.key)))]
|
|
2203
|
+
};
|
|
2204
|
+
}) });
|
|
2205
|
+
const issuedForTypeChangePairing = (pending, input, deferred, typeChange) => {
|
|
2206
|
+
const pair = typeof input.id === "string" && typeChangePair(pending, input.id, input.kind, typeChange);
|
|
2207
|
+
if (!pair || !equal(input.table, pair.before.table.identity) || input.column !== pair.before.column.key || equal(pair.before.column.dataType, pair.after.column.dataType) && typeChange.classify(pair) === void 0) return false;
|
|
2208
|
+
const { after, before } = pair;
|
|
2209
|
+
const renamedTable = tableKey(before.table.identity) !== tableKey(after.table.identity);
|
|
2210
|
+
if (renamedTable && !pending.decisions.some((parent) => !("enum" in parent) && !("kind" in parent) && parent.column === void 0 && equal(parent.table, before.table.identity) && parent.candidates.some((name) => equal({
|
|
2211
|
+
...parent.table,
|
|
2212
|
+
name
|
|
2213
|
+
}, after.table.identity)))) return false;
|
|
2214
|
+
return before.column.key === after.column.key ? renamedTable || deferred : !before.table.columns.some((column) => column.key === after.column.key) && !after.table.columns.some((column) => column.key === before.column.key);
|
|
2215
|
+
};
|
|
1862
2216
|
const issuedForPairing = (pending, input) => pending.decisions.some((parent) => {
|
|
2217
|
+
if ("kind" in parent) return false;
|
|
1863
2218
|
if ("enum" in parent) {
|
|
1864
2219
|
const value = input.value;
|
|
1865
2220
|
return parent.value === void 0 && typeof value === "string" && equal(parent.enum, input.enum) && parent.candidates.some((name) => input.id === valueId(parent.enum, {
|
|
@@ -1873,29 +2228,55 @@ const issuedForPairing = (pending, input) => pending.decisions.some((parent) =>
|
|
|
1873
2228
|
name
|
|
1874
2229
|
}, column));
|
|
1875
2230
|
});
|
|
1876
|
-
const
|
|
2231
|
+
const readEnumAnswers = (document, questions, inputs) => {
|
|
2232
|
+
const enums = questions.filter((item) => "enum" in item);
|
|
2233
|
+
const expected = new Set(enums.map((item) => item.id));
|
|
2234
|
+
const supplied = inputs.filter((input) => record(input) && typeof input.id === "string" && expected.has(input.id));
|
|
2235
|
+
const ids = new Set(supplied.map((input) => input.id));
|
|
2236
|
+
return readAnswers({
|
|
2237
|
+
...document,
|
|
2238
|
+
decisions: enums
|
|
2239
|
+
}, [...supplied, ...enums.filter((item) => !ids.has(item.id))], true);
|
|
2240
|
+
};
|
|
2241
|
+
const enumsAnswered = (decisions) => !Array.from(decisions).some((item) => "enum" in item && item.resolution.action === "pending");
|
|
2242
|
+
const reviewAnswers = (pending, diff, inputs, merging, typeChange) => effect.Effect.gen(function* () {
|
|
1877
2243
|
const rootIds = new Set(pending.decisions.map((item) => item.id));
|
|
1878
2244
|
const roots = inputs.filter((input) => record(input) && typeof input.id === "string" && rootIds.has(input.id));
|
|
1879
2245
|
const rootAnswers = yield* readAnswers(pending, roots, merging);
|
|
1880
2246
|
const expanded = expandRenames(diff, rootAnswers);
|
|
2247
|
+
const resolutions = new Map(inputs.flatMap((input) => record(input) && typeof input.id === "string" ? [[input.id, input.resolution]] : []));
|
|
2248
|
+
const paired = pairColumns(expanded.diff, (id) => resolutions.get(id));
|
|
2249
|
+
const deferred = defersTypeChanges(pending.decisions);
|
|
2250
|
+
let renames;
|
|
2251
|
+
let nestedTypeChanges = [];
|
|
2252
|
+
if (typeChange && deferred && enumsAnswered(rootAnswers.values())) {
|
|
2253
|
+
const labels = yield* effect.Effect.result(readEnumAnswers(pending, expanded.nested, inputs));
|
|
2254
|
+
if (effect.Result.isSuccess(labels) && enumsAnswered(labels.success.values())) {
|
|
2255
|
+
renames = acceptedEnumRenames(expanded.diff, new Map([...rootAnswers, ...labels.success]));
|
|
2256
|
+
nestedTypeChanges = typeChangeReviews(paired, pending, typeChange, "all", renames);
|
|
2257
|
+
}
|
|
2258
|
+
} else if (!deferred) nestedTypeChanges = typeChangeReviews(paired, pending, typeChange, "nested");
|
|
2259
|
+
const nested = [...expanded.nested, ...nestedTypeChanges.flatMap((item) => item.questions)];
|
|
1881
2260
|
const review = {
|
|
1882
2261
|
...pending,
|
|
1883
|
-
decisions: [...rootAnswers.values(), ...
|
|
2262
|
+
decisions: [...rootAnswers.values(), ...nested]
|
|
1884
2263
|
};
|
|
1885
2264
|
const expected = new Set(review.decisions.map((item) => item.id));
|
|
1886
|
-
const current = inputs.filter((input) => !merging || !record(input) || typeof input.id !== "string" || expected.has(input.id) || !issuedForPairing(pending, input));
|
|
2265
|
+
const current = inputs.filter((input) => !merging || !record(input) || typeof input.id !== "string" || expected.has(input.id) || !(issuedForPairing(pending, input) || typeChange && issuedForTypeChangePairing(pending, input, deferred, typeChange)));
|
|
1887
2266
|
const ids = new Set(current.flatMap((input) => record(input) ? [input.id] : []));
|
|
1888
|
-
const missing =
|
|
2267
|
+
const missing = nested.filter((item) => !ids.has(item.id));
|
|
1889
2268
|
if (!merging && missing.length) {
|
|
1890
|
-
const required = missing.some((item) => "enum" in item) ? "Enum rename requires value decisions" : "Table rename requires column decisions";
|
|
2269
|
+
const required = missing.some((item) => "enum" in item) ? "Enum rename requires value decisions" : missing.some((item) => !("kind" in item)) ? "Table rename requires column decisions" : "Rename requires type-change decisions";
|
|
1891
2270
|
return yield* effect.Effect.fail(new MigrationDecisionError({
|
|
1892
2271
|
message: `${required}. Run --merge-decisions to review them.`,
|
|
1893
2272
|
pending: review
|
|
1894
2273
|
}));
|
|
1895
2274
|
}
|
|
1896
2275
|
return {
|
|
1897
|
-
answers: yield* readAnswers(review, [...current, ...missing], merging),
|
|
1898
|
-
diff: expanded.diff
|
|
2276
|
+
answers: yield* readAnswers(review, [...current, ...missing], merging, typeChange, renames),
|
|
2277
|
+
diff: expanded.diff,
|
|
2278
|
+
nestedTypeChanges,
|
|
2279
|
+
paired
|
|
1899
2280
|
};
|
|
1900
2281
|
});
|
|
1901
2282
|
/** The enum and value renames that the reviewer accepted. */
|
|
@@ -1907,63 +2288,113 @@ const enumRenames = (diff, answers) => {
|
|
|
1907
2288
|
const { after, before } = change;
|
|
1908
2289
|
const key = tableKey(before.identity);
|
|
1909
2290
|
if (change._tag === "EnumRenamed") enums.set(key, after.identity);
|
|
1910
|
-
|
|
2291
|
+
const labels = new Map(before.values.flatMap((value) => {
|
|
1911
2292
|
const choice = answers.get(valueId(before.identity, after.identity, value))?.resolution;
|
|
1912
2293
|
return choice?.action === "rename" ? [[value, choice.target]] : [];
|
|
1913
|
-
}))
|
|
2294
|
+
}));
|
|
2295
|
+
if (labels.size) values.set(key, labels);
|
|
1914
2296
|
}
|
|
1915
2297
|
return {
|
|
1916
2298
|
enums,
|
|
1917
2299
|
values
|
|
1918
2300
|
};
|
|
1919
2301
|
};
|
|
1920
|
-
|
|
1921
|
-
const
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
2302
|
+
const acceptedEnumRenames = (diff, answers) => {
|
|
2303
|
+
const renames = enumRenames(diff, answers);
|
|
2304
|
+
return renames.enums.size || renames.values.size ? renames : void 0;
|
|
2305
|
+
};
|
|
2306
|
+
/** Rebuild the column pair and accepted enum renames to validate a proposed fallback. */
|
|
2307
|
+
const validateTypeChangeFallback = (document, decision, literal, typeChange) => {
|
|
2308
|
+
let renames;
|
|
2309
|
+
if (defersTypeChanges(document.decisions)) {
|
|
2310
|
+
const diff = { changes: diffEnums(document.before, document.after) };
|
|
2311
|
+
const answers = effect.Effect.runSync(effect.Effect.result(effect.Effect.gen(function* () {
|
|
2312
|
+
const roots = yield* readEnumAnswers(document, questions(diff), document.decisions);
|
|
2313
|
+
const expanded = expandRenames(diff, roots);
|
|
2314
|
+
const labels = yield* readEnumAnswers(document, expanded.nested, document.decisions);
|
|
2315
|
+
return acceptedEnumRenames(expanded.diff, new Map([...roots, ...labels]));
|
|
2316
|
+
})));
|
|
2317
|
+
if (effect.Result.isFailure(answers)) return answers.failure.message;
|
|
2318
|
+
renames = answers.success;
|
|
2319
|
+
}
|
|
2320
|
+
return validateFallback(document, decision, literal, typeChange, renames);
|
|
2321
|
+
};
|
|
2322
|
+
function mergeMigrationDecisions(fresh, previous, typeChange) {
|
|
2323
|
+
return effect.Effect.gen(function* () {
|
|
2324
|
+
if (!record(previous) || previous.version !== 1 || !Array.isArray(previous.decisions)) return yield* effect.Effect.fail(new MigrationDecisionError({
|
|
2325
|
+
message: "Expected a version 1 migration decision document.",
|
|
2326
|
+
pending: fresh
|
|
2327
|
+
}));
|
|
2328
|
+
const before = yield* decodeDatabaseSchema(previous.before);
|
|
2329
|
+
const after = yield* decodeDatabaseSchema(previous.after);
|
|
2330
|
+
const originalDiff = yield* diffDatabases(before, after);
|
|
2331
|
+
const original = rootReview(originalDiff, before, after, typeChange).document;
|
|
2332
|
+
const { answers } = yield* reviewAnswers(original, originalDiff, previous.decisions, true, typeChange);
|
|
2333
|
+
const carry = (question, renames) => {
|
|
2334
|
+
const answer = answers.get(question.id);
|
|
2335
|
+
if (!answer) return question;
|
|
2336
|
+
if ("kind" in question) {
|
|
2337
|
+
if (!("kind" in answer)) return question;
|
|
2338
|
+
const resolution = answer.resolution;
|
|
2339
|
+
if ("fallback" in resolution && typeChange && validateFallback(fresh, question, resolution.fallback.literal, typeChange, renames) !== void 0) return question;
|
|
2340
|
+
return {
|
|
2341
|
+
...question,
|
|
2342
|
+
resolution
|
|
2343
|
+
};
|
|
2344
|
+
}
|
|
2345
|
+
if ("kind" in answer) return question;
|
|
2346
|
+
const resolution = answer.resolution;
|
|
2347
|
+
if (resolution.action === "rename" && !question.candidates.includes(resolution.target)) return question;
|
|
2348
|
+
return {
|
|
2349
|
+
...question,
|
|
2350
|
+
resolution
|
|
2351
|
+
};
|
|
2352
|
+
};
|
|
2353
|
+
const freshDiff = yield* diffDatabases(fresh.before, fresh.after);
|
|
2354
|
+
const roots = rootReview(freshDiff, fresh.before, fresh.after, typeChange).document.decisions.map((question) => carry(question));
|
|
2355
|
+
const expanded = expandRenames(freshDiff, new Map(roots.map((item) => [item.id, item])));
|
|
2356
|
+
const decisions = [...roots, ...expanded.nested.map((question) => carry(question))];
|
|
2357
|
+
const deferred = defersTypeChanges(roots);
|
|
2358
|
+
if (typeChange && (!deferred || enumsAnswered(decisions))) {
|
|
2359
|
+
const carried = new Map(decisions.map((item) => [item.id, item]));
|
|
2360
|
+
const paired = pairColumns(expanded.diff, (id) => carried.get(id)?.resolution);
|
|
2361
|
+
const renames = deferred ? acceptedEnumRenames(expanded.diff, carried) : void 0;
|
|
2362
|
+
const reviews = typeChangeReviews(paired, fresh, typeChange, deferred ? "all" : "nested", renames);
|
|
2363
|
+
for (const review of reviews) decisions.push(...review.questions.map((question) => carry(question, renames)));
|
|
2364
|
+
}
|
|
1939
2365
|
return {
|
|
1940
|
-
...
|
|
1941
|
-
|
|
2366
|
+
...fresh,
|
|
2367
|
+
decisions
|
|
1942
2368
|
};
|
|
1943
|
-
};
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
2369
|
+
});
|
|
2370
|
+
}
|
|
2371
|
+
const resolvedTypeChanges = (reviews, answers) => reviews.map(({ change: { before }, classification, questions }) => {
|
|
2372
|
+
let loss;
|
|
2373
|
+
let failure;
|
|
2374
|
+
for (const question of questions) {
|
|
2375
|
+
const resolution = answers.get(question.id)?.resolution;
|
|
2376
|
+
if (resolution?.action === "accept_loss" || resolution?.action === "guard") loss = resolution.action;
|
|
2377
|
+
else if (resolution?.action === "accept_failure") failure = { action: "accept_failure" };
|
|
2378
|
+
else if (resolution && "fallback" in resolution) failure = { fallback: resolution.fallback };
|
|
2379
|
+
}
|
|
1947
2380
|
return {
|
|
1948
|
-
|
|
1949
|
-
|
|
2381
|
+
classification,
|
|
2382
|
+
column: before.column.name,
|
|
2383
|
+
table: before.table.identity,
|
|
2384
|
+
...loss ? { loss } : {},
|
|
2385
|
+
...failure ? { failure } : {}
|
|
1950
2386
|
};
|
|
1951
2387
|
});
|
|
1952
2388
|
/** Validate untrusted JSON and resolve intent against fresh comparison results. */
|
|
1953
|
-
const resolveMigrationDecisions = (before, after, supplied) => effect.Effect.gen(function* () {
|
|
2389
|
+
const resolveMigrationDecisions = (before, after, supplied, typeChange) => effect.Effect.gen(function* () {
|
|
1954
2390
|
const diff = yield* diffDatabases(before, after);
|
|
1955
|
-
const pending =
|
|
1956
|
-
after,
|
|
1957
|
-
before,
|
|
1958
|
-
decisions: questions(diff),
|
|
1959
|
-
version: 1
|
|
1960
|
-
};
|
|
2391
|
+
const { document: pending, typeChanges: roots } = rootReview(diff, before, after, typeChange);
|
|
1961
2392
|
const fail = (message) => effect.Effect.fail(new MigrationDecisionError({
|
|
1962
2393
|
message,
|
|
1963
2394
|
pending
|
|
1964
2395
|
}));
|
|
1965
2396
|
if (supplied === void 0) {
|
|
1966
|
-
if (pending.decisions.length) return yield* fail("Unresolved migration decisions. Prepare a decision file and resolve each removal before planning.");
|
|
2397
|
+
if (pending.decisions.length) return yield* fail(roots.length ? "Unresolved migration decisions. Prepare a decision file and resolve each question before planning." : "Unresolved migration decisions. Prepare a decision file and resolve each removal before planning.");
|
|
1967
2398
|
return {
|
|
1968
2399
|
decisions: pending,
|
|
1969
2400
|
diff
|
|
@@ -1971,42 +2402,19 @@ const resolveMigrationDecisions = (before, after, supplied) => effect.Effect.gen
|
|
|
1971
2402
|
}
|
|
1972
2403
|
if (!record(supplied) || supplied.version !== 1 || !Array.isArray(supplied.decisions)) return yield* fail("Expected a version 1 migration decision document.");
|
|
1973
2404
|
if (!equal(supplied.before, before) || !equal(supplied.after, after)) return yield* fail("Stale migration decisions: source or target schema changed. Prepare a new decision file.");
|
|
1974
|
-
const reviewed = yield* reviewAnswers(pending, diff, supplied.decisions, false);
|
|
2405
|
+
const reviewed = yield* reviewAnswers(pending, diff, supplied.decisions, false, typeChange);
|
|
1975
2406
|
const answers = reviewed.answers;
|
|
2407
|
+
const typeChanges = resolvedTypeChanges([...roots, ...reviewed.nestedTypeChanges], answers);
|
|
1976
2408
|
const dropGuards = [];
|
|
1977
|
-
const
|
|
1978
|
-
if (
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
const answer = answers.get(columnId(change, item.column.key));
|
|
1987
|
-
if (answer.resolution.action === "drop_if_empty") dropGuards.push({
|
|
1988
|
-
column: item.column.name,
|
|
1989
|
-
table: change.table
|
|
1990
|
-
});
|
|
1991
|
-
if (answer.resolution.action !== "rename") continue;
|
|
1992
|
-
const targetKey = answer.resolution.target;
|
|
1993
|
-
const afterColumn = change.after.columns.find((column) => column.key === targetKey);
|
|
1994
|
-
renamedTargets.add(afterColumn.key);
|
|
1995
|
-
const fields = [];
|
|
1996
|
-
if (!equal(item.column.dataType, afterColumn.dataType)) fields.push("dataType");
|
|
1997
|
-
if (!constraintsEqual(item.column.constraints, afterColumn.constraints)) fields.push("constraints");
|
|
1998
|
-
changes.push({
|
|
1999
|
-
_tag: "ColumnRenamed",
|
|
2000
|
-
after: afterColumn,
|
|
2001
|
-
before: item.column,
|
|
2002
|
-
fields
|
|
2003
|
-
});
|
|
2004
|
-
}
|
|
2005
|
-
return {
|
|
2006
|
-
...change,
|
|
2007
|
-
changes: [...changes, ...change.changes.filter((item) => !(item._tag === "ColumnAdded" && renamedTargets.has(item.column.key)) && !(item._tag === "ColumnRemoved" && answers.get(columnId(change, item.column.key))?.resolution.action === "rename"))]
|
|
2008
|
-
};
|
|
2009
|
-
});
|
|
2409
|
+
for (const change of reviewed.diff.changes) {
|
|
2410
|
+
if (change._tag === "TableRemoved" && answers.get(idFor(change.table))?.resolution.action === "drop_if_empty") dropGuards.push({ table: change.table });
|
|
2411
|
+
if (!("changes" in change)) continue;
|
|
2412
|
+
for (const item of change.changes) if (item._tag === "ColumnRemoved" && answers.get(columnId(change, item.column.key))?.resolution.action === "drop_if_empty") dropGuards.push({
|
|
2413
|
+
column: item.column.name,
|
|
2414
|
+
table: change.table
|
|
2415
|
+
});
|
|
2416
|
+
}
|
|
2417
|
+
const { changes } = reviewed.paired;
|
|
2010
2418
|
const renames = sequenceRenames(changes);
|
|
2011
2419
|
const kept = changes.filter((change) => !/^(Comment|Enum|Sequence|Trigger)/.test(change._tag));
|
|
2012
2420
|
const leading = (change) => /^(Namespace|Extension)/.test(change._tag);
|
|
@@ -2019,7 +2427,8 @@ const resolveMigrationDecisions = (before, after, supplied) => effect.Effect.gen
|
|
|
2019
2427
|
];
|
|
2020
2428
|
const resolved = {
|
|
2021
2429
|
changes: [...withRenames, ...diffComments(before, after, withRenames)],
|
|
2022
|
-
...dropGuards.length ? { dropGuards } : {}
|
|
2430
|
+
...dropGuards.length ? { dropGuards } : {},
|
|
2431
|
+
...typeChanges.length ? { typeChanges } : {}
|
|
2023
2432
|
};
|
|
2024
2433
|
return {
|
|
2025
2434
|
decisions: {
|
|
@@ -2155,6 +2564,17 @@ const mappingResult = effect.Schema.Struct({
|
|
|
2155
2564
|
diagnostics,
|
|
2156
2565
|
schema: effect.Schema.Unknown
|
|
2157
2566
|
});
|
|
2567
|
+
const stringList = effect.Schema.Array(effect.Schema.String);
|
|
2568
|
+
const mapperSubjectDescriptionSchema = effect.Schema.Struct({
|
|
2569
|
+
definitions: effect.Schema.optionalKey(stringList),
|
|
2570
|
+
language: effect.Schema.NonEmptyString,
|
|
2571
|
+
notes: effect.Schema.optionalKey(stringList),
|
|
2572
|
+
types: effect.Schema.Array(effect.Schema.Struct({
|
|
2573
|
+
label: effect.Schema.NonEmptyString,
|
|
2574
|
+
text: effect.Schema.String
|
|
2575
|
+
}))
|
|
2576
|
+
});
|
|
2577
|
+
const subjectDescription = effect.Schema.UndefinedOr(mapperSubjectDescriptionSchema);
|
|
2158
2578
|
const wrapError = (cause, code, path, fallback) => {
|
|
2159
2579
|
if (cause instanceof CodegenError) return cause;
|
|
2160
2580
|
const details = effect.Schema.decodeUnknownOption(taggedCodegenError)(cause);
|
|
@@ -2211,6 +2631,39 @@ const invoke = (callback, code, path) => effect.Effect.flatMap(validate(() => {
|
|
|
2211
2631
|
});
|
|
2212
2632
|
return result;
|
|
2213
2633
|
}, "invalid-plugin", path), (effect$1) => effect.Effect.mapError(effect$1, (cause) => wrapError(cause, code, path, `Plugin ${path}() failed`)));
|
|
2634
|
+
/** Optional capability: absent gives no describer; a present value must be a function. */
|
|
2635
|
+
const optionalDescribe = (plugin) => {
|
|
2636
|
+
if (!("describe" in plugin)) return;
|
|
2637
|
+
const callback = plugin.describe;
|
|
2638
|
+
if (typeof callback !== "function") throw new CodegenError({
|
|
2639
|
+
code: "invalid-plugin",
|
|
2640
|
+
message: "Plugin describe() must be a function",
|
|
2641
|
+
path: "describe"
|
|
2642
|
+
});
|
|
2643
|
+
return (schema, subject) => {
|
|
2644
|
+
let result;
|
|
2645
|
+
try {
|
|
2646
|
+
result = Reflect.apply(callback, plugin, [schema, subject]);
|
|
2647
|
+
} catch (cause) {
|
|
2648
|
+
throw new CodegenError({
|
|
2649
|
+
cause,
|
|
2650
|
+
code: "description-failed",
|
|
2651
|
+
message: cause instanceof Error ? cause.message : "Plugin describe() failed",
|
|
2652
|
+
path: "describe"
|
|
2653
|
+
});
|
|
2654
|
+
}
|
|
2655
|
+
try {
|
|
2656
|
+
return effect.Schema.decodeUnknownSync(subjectDescription)(result);
|
|
2657
|
+
} catch (cause) {
|
|
2658
|
+
throw new CodegenError({
|
|
2659
|
+
cause,
|
|
2660
|
+
code: "invalid-description",
|
|
2661
|
+
message: "Plugin describe() returned an invalid subject description",
|
|
2662
|
+
path: "describe"
|
|
2663
|
+
});
|
|
2664
|
+
}
|
|
2665
|
+
};
|
|
2666
|
+
};
|
|
2214
2667
|
/** Validate the mapper value selected from a module by the consumer. */
|
|
2215
2668
|
const decodeMapperPlugin = (value) => validate(() => {
|
|
2216
2669
|
const plugin = readExport(value, "mapper");
|
|
@@ -2218,8 +2671,10 @@ const decodeMapperPlugin = (value) => validate(() => {
|
|
|
2218
2671
|
const decodeInput = method(plugin, "decodeInput");
|
|
2219
2672
|
const decodeOptions = method(plugin, "decodeOptions");
|
|
2220
2673
|
const map = method(plugin, "map");
|
|
2674
|
+
const describeSubject = optionalDescribe(plugin);
|
|
2221
2675
|
return {
|
|
2222
2676
|
descriptor,
|
|
2677
|
+
...describeSubject === void 0 ? {} : { describe: describeSubject },
|
|
2223
2678
|
map: (schema, options) => effect.Effect.gen(function* () {
|
|
2224
2679
|
const input = yield* invoke(() => decodeInput(schema), "invalid-input", "decodeInput");
|
|
2225
2680
|
const settings = yield* invoke(() => decodeOptions(options), "invalid-options", "decodeOptions");
|
|
@@ -2246,6 +2701,38 @@ const decodeGeneratorPlugin = (value) => validate(() => {
|
|
|
2246
2701
|
})
|
|
2247
2702
|
};
|
|
2248
2703
|
}, "invalid-plugin", "generator");
|
|
2704
|
+
// src/codegen-pipeline.ts
|
|
2705
|
+
/** Map every profile in order, retaining errors and diagnostics for the caller. */
|
|
2706
|
+
const mapProfiles = (schema, profiles) => effect.Effect.forEach(profiles, ({ mapper, name, options }) => mapper.map(schema, options).pipe(effect.Effect.match({
|
|
2707
|
+
onFailure: (error) => ({
|
|
2708
|
+
mapper,
|
|
2709
|
+
name,
|
|
2710
|
+
result: { error }
|
|
2711
|
+
}),
|
|
2712
|
+
onSuccess: (result) => ({
|
|
2713
|
+
mapper,
|
|
2714
|
+
name,
|
|
2715
|
+
result
|
|
2716
|
+
})
|
|
2717
|
+
})));
|
|
2718
|
+
var GenerationFailure = class extends effect.Data.TaggedError("GenerationFailure") {};
|
|
2719
|
+
/** Check the entire batch's contracts before invoking any generator. */
|
|
2720
|
+
const generate = (mapped, generators) => effect.Effect.gen(function* () {
|
|
2721
|
+
const output = mapped.mapper.descriptor.output;
|
|
2722
|
+
yield* effect.Effect.forEach(generators, ({ generator, name }) => validateApplicationCompatibility(output, generator.descriptor.input).pipe(effect.Effect.mapError((error) => new GenerationFailure({
|
|
2723
|
+
error,
|
|
2724
|
+
generator: name,
|
|
2725
|
+
stage: "compatibility"
|
|
2726
|
+
}))), { discard: true });
|
|
2727
|
+
return yield* effect.Effect.forEach(generators, ({ generator, name, options }) => generator.generate(output, mapped.result.schema, options).pipe(effect.Effect.mapError((error) => new GenerationFailure({
|
|
2728
|
+
error,
|
|
2729
|
+
generator: name,
|
|
2730
|
+
stage: "generation"
|
|
2731
|
+
})), effect.Effect.map((result) => ({
|
|
2732
|
+
name,
|
|
2733
|
+
result
|
|
2734
|
+
}))));
|
|
2735
|
+
});
|
|
2249
2736
|
exports.CodegenError = CodegenError;
|
|
2250
2737
|
exports.MigrationDecisionError = MigrationDecisionError;
|
|
2251
2738
|
exports.SchemaDiffError = SchemaDiffError;
|
|
@@ -2279,13 +2766,17 @@ exports.externalObjects = externalObjects;
|
|
|
2279
2766
|
exports.formatExtractOmissions = formatExtractOmissions;
|
|
2280
2767
|
exports.functionKey = functionKey;
|
|
2281
2768
|
exports.functionLabel = functionLabel;
|
|
2769
|
+
exports.generate = generate;
|
|
2282
2770
|
exports.generatedFileHeader = generatedFileHeader;
|
|
2771
|
+
exports.hashMigrationPair = hashMigrationPair;
|
|
2772
|
+
exports.mapProfiles = mapProfiles;
|
|
2283
2773
|
exports.mergeMigrationDecisions = mergeMigrationDecisions;
|
|
2284
2774
|
exports.migrationRun = migrationRun;
|
|
2285
2775
|
exports.nameSpan = nameSpan;
|
|
2286
2776
|
exports.namespaceKey = namespaceKey;
|
|
2287
2777
|
exports.namespaceOf = namespaceOf;
|
|
2288
2778
|
exports.nextMigration = nextMigration;
|
|
2779
|
+
exports.pairedColumns = pairedColumns;
|
|
2289
2780
|
exports.planDatabaseMigration = planDatabaseMigration;
|
|
2290
2781
|
exports.planDownMigration = planDownMigration;
|
|
2291
2782
|
exports.prepareMigrationDecisions = prepareMigrationDecisions;
|
|
@@ -2301,3 +2792,4 @@ exports.tableKey = tableKey;
|
|
|
2301
2792
|
exports.triggerKey = triggerKey;
|
|
2302
2793
|
exports.validateApplicationCompatibility = validateApplicationCompatibility;
|
|
2303
2794
|
exports.validateDatabase = validateDatabase;
|
|
2795
|
+
exports.validateTypeChangeFallback = validateTypeChangeFallback;
|