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