@uncutsql/core 0.7.0 → 0.8.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/dist/index.cjs CHANGED
@@ -1322,6 +1322,157 @@ function swap(value) {
1322
1322
  before: value.after
1323
1323
  };
1324
1324
  }
1325
+ // src/migration-checksum.ts
1326
+ const encoder = new TextEncoder();
1327
+ const domain = encoder.encode("uncutsql-migration-pair-v1\0");
1328
+ const blockSize = 64;
1329
+ const digestSize = 32;
1330
+ const initialState = [
1331
+ 1779033703,
1332
+ 3144134277,
1333
+ 1013904242,
1334
+ 2773480762,
1335
+ 1359893119,
1336
+ 2600822924,
1337
+ 528734635,
1338
+ 1541459225
1339
+ ];
1340
+ const roundConstants = [
1341
+ 1116352408,
1342
+ 1899447441,
1343
+ 3049323471,
1344
+ 3921009573,
1345
+ 961987163,
1346
+ 1508970993,
1347
+ 2453635748,
1348
+ 2870763221,
1349
+ 3624381080,
1350
+ 310598401,
1351
+ 607225278,
1352
+ 1426881987,
1353
+ 1925078388,
1354
+ 2162078206,
1355
+ 2614888103,
1356
+ 3248222580,
1357
+ 3835390401,
1358
+ 4022224774,
1359
+ 264347078,
1360
+ 604807628,
1361
+ 770255983,
1362
+ 1249150122,
1363
+ 1555081692,
1364
+ 1996064986,
1365
+ 2554220882,
1366
+ 2821834349,
1367
+ 2952996808,
1368
+ 3210313671,
1369
+ 3336571891,
1370
+ 3584528711,
1371
+ 113926993,
1372
+ 338241895,
1373
+ 666307205,
1374
+ 773529912,
1375
+ 1294757372,
1376
+ 1396182291,
1377
+ 1695183700,
1378
+ 1986661051,
1379
+ 2177026350,
1380
+ 2456956037,
1381
+ 2730485921,
1382
+ 2820302411,
1383
+ 3259730800,
1384
+ 3345764771,
1385
+ 3516065817,
1386
+ 3600352804,
1387
+ 4094571909,
1388
+ 275423344,
1389
+ 430227734,
1390
+ 506948616,
1391
+ 659060556,
1392
+ 883997877,
1393
+ 958139571,
1394
+ 1322822218,
1395
+ 1537002063,
1396
+ 1747873779,
1397
+ 1955562222,
1398
+ 2024104815,
1399
+ 2227730452,
1400
+ 2361852424,
1401
+ 2428436474,
1402
+ 2756734187,
1403
+ 3204031479,
1404
+ 3329325298
1405
+ ];
1406
+ function rotateRight(word, bits) {
1407
+ return word >>> bits | word << 32 - bits;
1408
+ }
1409
+ function compress(state, schedule, bytes, offset) {
1410
+ for (let i = 0; i < 16; i++) schedule[i] = bytes.getUint32(offset + i * 4);
1411
+ for (let i = 16; i < schedule.length; i++) {
1412
+ const word15 = schedule[i - 15];
1413
+ const word2 = schedule[i - 2];
1414
+ const sigma0 = rotateRight(word15, 7) ^ rotateRight(word15, 18) ^ word15 >>> 3;
1415
+ const sigma1 = rotateRight(word2, 17) ^ rotateRight(word2, 19) ^ word2 >>> 10;
1416
+ schedule[i] = schedule[i - 16] + sigma0 + schedule[i - 7] + sigma1;
1417
+ }
1418
+ let a = state[0];
1419
+ let b = state[1];
1420
+ let c = state[2];
1421
+ let d = state[3];
1422
+ let e = state[4];
1423
+ let f = state[5];
1424
+ let g = state[6];
1425
+ let h = state[7];
1426
+ for (let i = 0; i < schedule.length; i++) {
1427
+ const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
1428
+ const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
1429
+ const choose = e & f ^ ~e & g;
1430
+ const majority = a & b ^ a & c ^ b & c;
1431
+ const temp1 = h + sum1 + choose + roundConstants[i] + schedule[i];
1432
+ const temp2 = sum0 + majority;
1433
+ h = g;
1434
+ g = f;
1435
+ f = e;
1436
+ e = d + temp1 >>> 0;
1437
+ d = c;
1438
+ c = b;
1439
+ b = a;
1440
+ a = temp1 + temp2 >>> 0;
1441
+ }
1442
+ state[0] = state[0] + a;
1443
+ state[1] = state[1] + b;
1444
+ state[2] = state[2] + c;
1445
+ state[3] = state[3] + d;
1446
+ state[4] = state[4] + e;
1447
+ state[5] = state[5] + f;
1448
+ state[6] = state[6] + g;
1449
+ state[7] = state[7] + h;
1450
+ }
1451
+ function sha256(bytes) {
1452
+ const state = new Uint32Array(initialState);
1453
+ const schedule = new Uint32Array(roundConstants.length);
1454
+ const input = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1455
+ const remainder = bytes.byteLength % blockSize;
1456
+ const tailOffset = bytes.byteLength - remainder;
1457
+ for (let offset = 0; offset < tailOffset; offset += blockSize) compress(state, schedule, input, offset);
1458
+ const tail = new Uint8Array(remainder + 9 <= blockSize ? blockSize : 128);
1459
+ tail.set(bytes.subarray(tailOffset));
1460
+ tail[remainder] = 128;
1461
+ const padded = new DataView(tail.buffer);
1462
+ padded.setBigUint64(tail.byteLength - 8, BigInt(bytes.byteLength) * 8n);
1463
+ for (let offset = 0; offset < tail.byteLength; offset += blockSize) compress(state, schedule, padded, offset);
1464
+ const digest = new Uint8Array(digestSize);
1465
+ const output = new DataView(digest.buffer);
1466
+ for (let i = 0; i < state.length; i++) output.setUint32(i * 4, state[i]);
1467
+ return digest;
1468
+ }
1469
+ function hashMigrationPair(up, down) {
1470
+ const pair = new Uint8Array(domain.length + 64);
1471
+ pair.set(domain);
1472
+ pair.set(sha256(typeof up === "string" ? encoder.encode(up) : up), domain.length);
1473
+ pair.set(sha256(typeof down === "string" ? encoder.encode(down) : down), domain.length + digestSize);
1474
+ return Array.from(sha256(pair), (byte) => byte.toString(16).padStart(2, "0")).join("");
1475
+ }
1325
1476
  // src/dialect.ts
1326
1477
  var StatementEditError = class extends effect.Data.TaggedError("StatementEditError") {};
1327
1478
  // src/structure.ts
@@ -1671,13 +1822,17 @@ const formatExtractOmissions = (omitted) => [...omitted].sort((left, right) => o
1671
1822
  });
1672
1823
  const checksumPattern = /^[a-f0-9]{64}$/;
1673
1824
  /** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
1674
- const migrationRun = (direction, migrations, checkpoint, sha256) => {
1825
+ const migrationRun = (direction, migrations, checkpoint) => {
1675
1826
  const down = direction === "down";
1676
1827
  const baseline = migrations[0]?.baselineCheckpoint !== void 0 ? migrations[0] : void 0;
1677
1828
  const checkpointId = checkpoint?.id ?? 0;
1678
1829
  const firstUpId = baseline?.id ?? checkpointId + 1;
1679
1830
  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) => index > 0 && migration.baselineCheckpoint !== void 0 || migration.id !== offset + index + 1 || !migration.sql.trim() || migration.sql.includes("\0") || sha256(migration.sql) !== migration.sha256)) return;
1831
+ 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) => {
1832
+ const { down, sql } = migration;
1833
+ const downText = "sql" in down ? down.sql : down.text;
1834
+ 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;
1835
+ })) return;
1681
1836
  return {
1682
1837
  ...baseline ? { baseline } : {},
1683
1838
  direction,
@@ -2280,6 +2435,7 @@ exports.formatExtractOmissions = formatExtractOmissions;
2280
2435
  exports.functionKey = functionKey;
2281
2436
  exports.functionLabel = functionLabel;
2282
2437
  exports.generatedFileHeader = generatedFileHeader;
2438
+ exports.hashMigrationPair = hashMigrationPair;
2283
2439
  exports.mergeMigrationDecisions = mergeMigrationDecisions;
2284
2440
  exports.migrationRun = migrationRun;
2285
2441
  exports.nameSpan = nameSpan;
package/dist/index.d.cts CHANGED
@@ -586,6 +586,7 @@ export interface DatabaseMigration {
586
586
  export declare const planDatabaseMigration: (diff: DatabaseDiff) => DatabaseMigration;
587
587
  /** Reverse reviewed intent, retaining rename pairs and guarding newly added data. */
588
588
  export declare const planDownMigration: (up: DatabaseMigration) => DatabaseMigration | string;
589
+ export declare function hashMigrationPair(up: string | Uint8Array, down: string | Uint8Array): string;
589
590
  /** A normalized column key scoped to its qualified relation. */
590
591
  interface ColumnIdentity {
591
592
  readonly key: DatabaseColumn["key"];
@@ -888,18 +889,21 @@ export declare const decodeDatabaseSchema: (value: unknown) => Effect.Effect<Dat
888
889
  interface MigrationSource {
889
890
  /** A root baseline records the original prefix tip as the logical checkpoint. */
890
891
  readonly baselineCheckpoint?: string;
891
- readonly down?: {
892
+ readonly down: {
892
893
  /** What the down SQL relies on (ADR 0038 Decision 2). */
893
894
  readonly externals?: DatabaseExternals;
895
+ /** The complete down file text. */
894
896
  readonly sql: string;
895
897
  } | {
896
898
  readonly reason: string;
899
+ readonly text: string;
897
900
  };
898
901
  /** What the up SQL relies on (ADR 0038 Decision 2). */
899
902
  readonly externals?: DatabaseExternals;
900
903
  readonly id: number;
904
+ /** The SHA-256 digest of the migration pair. */
901
905
  readonly sha256: string;
902
- /** The whole migration file, including the CLI's `-- ` header lines. */
906
+ /** The complete up file text. */
903
907
  readonly sql: string;
904
908
  }
905
909
  interface MigrationCheckpoint {
@@ -962,7 +966,11 @@ interface ExecutorCapabilities<E extends Error = Error, R = never> {
962
966
  * Recheck the database position under the write transaction before executing. */
963
967
  checkpoint?: MigrationCheckpoint, options?: ExecutionOptions) => Effect.Effect<ExecutionResult, E | V, R | RV>;
964
968
  readonly checkpoint: (target: string, options?: ExecutionOptions) => Effect.Effect<MigrationCheckpoint, E, R>;
965
- /** Replay a verified prefix on an empty database and render its state as one SQL migration. */
969
+ /**
970
+ * Replay a verified prefix on an empty database and render its state as one SQL migration.
971
+ * Returns the baseline up SQL only, without a chain header or down record; the caller writes
972
+ * the down file.
973
+ */
966
974
  readonly compact?: (migrations: readonly MigrationSource[], options?: ReplayOptions) => Effect.Effect<string, E, R>;
967
975
  /** Equal to the `Dialect.name` that produced the lock. Core and the CLI compare it only for equality. */
968
976
  readonly dialect: string;
@@ -996,7 +1004,7 @@ interface MigrationRun {
996
1004
  readonly offset: number;
997
1005
  }
998
1006
  /** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
999
- export declare const migrationRun: (direction: "up" | "down", migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined, sha256: (sql: string) => string) => MigrationRun | undefined;
1007
+ export declare const migrationRun: (direction: "up" | "down", migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined) => MigrationRun | undefined;
1000
1008
  /** Whether the history tip read under the write transaction is a matching prefix of the run. */
1001
1009
  export declare const continuesRun: (run: MigrationRun, migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined, tip: MigrationCheckpoint) => boolean;
1002
1010
  /** The file to execute after the tip, or `undefined` when the run is complete. */
@@ -1052,7 +1060,7 @@ export declare const mergeMigrationDecisions: (fresh: MigrationDecisions, previo
1052
1060
  after: DatabaseSchema;
1053
1061
  before: DatabaseSchema;
1054
1062
  version: 1;
1055
- }, SchemaDiffError | SchemaModelError | MigrationDecisionError, never>;
1063
+ }, SchemaModelError | SchemaDiffError | MigrationDecisionError, never>;
1056
1064
  /** Validate untrusted JSON and resolve intent against fresh comparison results. */
1057
1065
  export declare const resolveMigrationDecisions: (before: DatabaseSchema, after: DatabaseSchema, supplied?: unknown) => Effect.Effect<{
1058
1066
  decisions: MigrationDecisions;
package/dist/index.d.ts CHANGED
@@ -586,6 +586,7 @@ export interface DatabaseMigration {
586
586
  export declare const planDatabaseMigration: (diff: DatabaseDiff) => DatabaseMigration;
587
587
  /** Reverse reviewed intent, retaining rename pairs and guarding newly added data. */
588
588
  export declare const planDownMigration: (up: DatabaseMigration) => DatabaseMigration | string;
589
+ export declare function hashMigrationPair(up: string | Uint8Array, down: string | Uint8Array): string;
589
590
  /** A normalized column key scoped to its qualified relation. */
590
591
  interface ColumnIdentity {
591
592
  readonly key: DatabaseColumn["key"];
@@ -888,18 +889,21 @@ export declare const decodeDatabaseSchema: (value: unknown) => Effect.Effect<Dat
888
889
  interface MigrationSource {
889
890
  /** A root baseline records the original prefix tip as the logical checkpoint. */
890
891
  readonly baselineCheckpoint?: string;
891
- readonly down?: {
892
+ readonly down: {
892
893
  /** What the down SQL relies on (ADR 0038 Decision 2). */
893
894
  readonly externals?: DatabaseExternals;
895
+ /** The complete down file text. */
894
896
  readonly sql: string;
895
897
  } | {
896
898
  readonly reason: string;
899
+ readonly text: string;
897
900
  };
898
901
  /** What the up SQL relies on (ADR 0038 Decision 2). */
899
902
  readonly externals?: DatabaseExternals;
900
903
  readonly id: number;
904
+ /** The SHA-256 digest of the migration pair. */
901
905
  readonly sha256: string;
902
- /** The whole migration file, including the CLI's `-- ` header lines. */
906
+ /** The complete up file text. */
903
907
  readonly sql: string;
904
908
  }
905
909
  interface MigrationCheckpoint {
@@ -962,7 +966,11 @@ interface ExecutorCapabilities<E extends Error = Error, R = never> {
962
966
  * Recheck the database position under the write transaction before executing. */
963
967
  checkpoint?: MigrationCheckpoint, options?: ExecutionOptions) => Effect.Effect<ExecutionResult, E | V, R | RV>;
964
968
  readonly checkpoint: (target: string, options?: ExecutionOptions) => Effect.Effect<MigrationCheckpoint, E, R>;
965
- /** Replay a verified prefix on an empty database and render its state as one SQL migration. */
969
+ /**
970
+ * Replay a verified prefix on an empty database and render its state as one SQL migration.
971
+ * Returns the baseline up SQL only, without a chain header or down record; the caller writes
972
+ * the down file.
973
+ */
966
974
  readonly compact?: (migrations: readonly MigrationSource[], options?: ReplayOptions) => Effect.Effect<string, E, R>;
967
975
  /** Equal to the `Dialect.name` that produced the lock. Core and the CLI compare it only for equality. */
968
976
  readonly dialect: string;
@@ -996,7 +1004,7 @@ interface MigrationRun {
996
1004
  readonly offset: number;
997
1005
  }
998
1006
  /** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
999
- export declare const migrationRun: (direction: "up" | "down", migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined, sha256: (sql: string) => string) => MigrationRun | undefined;
1007
+ export declare const migrationRun: (direction: "up" | "down", migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined) => MigrationRun | undefined;
1000
1008
  /** Whether the history tip read under the write transaction is a matching prefix of the run. */
1001
1009
  export declare const continuesRun: (run: MigrationRun, migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined, tip: MigrationCheckpoint) => boolean;
1002
1010
  /** The file to execute after the tip, or `undefined` when the run is complete. */
@@ -1052,7 +1060,7 @@ export declare const mergeMigrationDecisions: (fresh: MigrationDecisions, previo
1052
1060
  after: DatabaseSchema;
1053
1061
  before: DatabaseSchema;
1054
1062
  version: 1;
1055
- }, SchemaDiffError | SchemaModelError | MigrationDecisionError, never>;
1063
+ }, SchemaModelError | SchemaDiffError | MigrationDecisionError, never>;
1056
1064
  /** Validate untrusted JSON and resolve intent against fresh comparison results. */
1057
1065
  export declare const resolveMigrationDecisions: (before: DatabaseSchema, after: DatabaseSchema, supplied?: unknown) => Effect.Effect<{
1058
1066
  decisions: MigrationDecisions;
package/dist/index.js CHANGED
@@ -1321,6 +1321,157 @@ function swap(value) {
1321
1321
  before: value.after
1322
1322
  };
1323
1323
  }
1324
+ // src/migration-checksum.ts
1325
+ const encoder = new TextEncoder();
1326
+ const domain = encoder.encode("uncutsql-migration-pair-v1\0");
1327
+ const blockSize = 64;
1328
+ const digestSize = 32;
1329
+ const initialState = [
1330
+ 1779033703,
1331
+ 3144134277,
1332
+ 1013904242,
1333
+ 2773480762,
1334
+ 1359893119,
1335
+ 2600822924,
1336
+ 528734635,
1337
+ 1541459225
1338
+ ];
1339
+ const roundConstants = [
1340
+ 1116352408,
1341
+ 1899447441,
1342
+ 3049323471,
1343
+ 3921009573,
1344
+ 961987163,
1345
+ 1508970993,
1346
+ 2453635748,
1347
+ 2870763221,
1348
+ 3624381080,
1349
+ 310598401,
1350
+ 607225278,
1351
+ 1426881987,
1352
+ 1925078388,
1353
+ 2162078206,
1354
+ 2614888103,
1355
+ 3248222580,
1356
+ 3835390401,
1357
+ 4022224774,
1358
+ 264347078,
1359
+ 604807628,
1360
+ 770255983,
1361
+ 1249150122,
1362
+ 1555081692,
1363
+ 1996064986,
1364
+ 2554220882,
1365
+ 2821834349,
1366
+ 2952996808,
1367
+ 3210313671,
1368
+ 3336571891,
1369
+ 3584528711,
1370
+ 113926993,
1371
+ 338241895,
1372
+ 666307205,
1373
+ 773529912,
1374
+ 1294757372,
1375
+ 1396182291,
1376
+ 1695183700,
1377
+ 1986661051,
1378
+ 2177026350,
1379
+ 2456956037,
1380
+ 2730485921,
1381
+ 2820302411,
1382
+ 3259730800,
1383
+ 3345764771,
1384
+ 3516065817,
1385
+ 3600352804,
1386
+ 4094571909,
1387
+ 275423344,
1388
+ 430227734,
1389
+ 506948616,
1390
+ 659060556,
1391
+ 883997877,
1392
+ 958139571,
1393
+ 1322822218,
1394
+ 1537002063,
1395
+ 1747873779,
1396
+ 1955562222,
1397
+ 2024104815,
1398
+ 2227730452,
1399
+ 2361852424,
1400
+ 2428436474,
1401
+ 2756734187,
1402
+ 3204031479,
1403
+ 3329325298
1404
+ ];
1405
+ function rotateRight(word, bits) {
1406
+ return word >>> bits | word << 32 - bits;
1407
+ }
1408
+ function compress(state, schedule, bytes, offset) {
1409
+ for (let i = 0; i < 16; i++) schedule[i] = bytes.getUint32(offset + i * 4);
1410
+ for (let i = 16; i < schedule.length; i++) {
1411
+ const word15 = schedule[i - 15];
1412
+ const word2 = schedule[i - 2];
1413
+ const sigma0 = rotateRight(word15, 7) ^ rotateRight(word15, 18) ^ word15 >>> 3;
1414
+ const sigma1 = rotateRight(word2, 17) ^ rotateRight(word2, 19) ^ word2 >>> 10;
1415
+ schedule[i] = schedule[i - 16] + sigma0 + schedule[i - 7] + sigma1;
1416
+ }
1417
+ let a = state[0];
1418
+ let b = state[1];
1419
+ let c = state[2];
1420
+ let d = state[3];
1421
+ let e = state[4];
1422
+ let f = state[5];
1423
+ let g = state[6];
1424
+ let h = state[7];
1425
+ for (let i = 0; i < schedule.length; i++) {
1426
+ const sum0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
1427
+ const sum1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
1428
+ const choose = e & f ^ ~e & g;
1429
+ const majority = a & b ^ a & c ^ b & c;
1430
+ const temp1 = h + sum1 + choose + roundConstants[i] + schedule[i];
1431
+ const temp2 = sum0 + majority;
1432
+ h = g;
1433
+ g = f;
1434
+ f = e;
1435
+ e = d + temp1 >>> 0;
1436
+ d = c;
1437
+ c = b;
1438
+ b = a;
1439
+ a = temp1 + temp2 >>> 0;
1440
+ }
1441
+ state[0] = state[0] + a;
1442
+ state[1] = state[1] + b;
1443
+ state[2] = state[2] + c;
1444
+ state[3] = state[3] + d;
1445
+ state[4] = state[4] + e;
1446
+ state[5] = state[5] + f;
1447
+ state[6] = state[6] + g;
1448
+ state[7] = state[7] + h;
1449
+ }
1450
+ function sha256(bytes) {
1451
+ const state = new Uint32Array(initialState);
1452
+ const schedule = new Uint32Array(roundConstants.length);
1453
+ const input = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1454
+ const remainder = bytes.byteLength % blockSize;
1455
+ const tailOffset = bytes.byteLength - remainder;
1456
+ for (let offset = 0; offset < tailOffset; offset += blockSize) compress(state, schedule, input, offset);
1457
+ const tail = new Uint8Array(remainder + 9 <= blockSize ? blockSize : 128);
1458
+ tail.set(bytes.subarray(tailOffset));
1459
+ tail[remainder] = 128;
1460
+ const padded = new DataView(tail.buffer);
1461
+ padded.setBigUint64(tail.byteLength - 8, BigInt(bytes.byteLength) * 8n);
1462
+ for (let offset = 0; offset < tail.byteLength; offset += blockSize) compress(state, schedule, padded, offset);
1463
+ const digest = new Uint8Array(digestSize);
1464
+ const output = new DataView(digest.buffer);
1465
+ for (let i = 0; i < state.length; i++) output.setUint32(i * 4, state[i]);
1466
+ return digest;
1467
+ }
1468
+ function hashMigrationPair(up, down) {
1469
+ const pair = new Uint8Array(domain.length + 64);
1470
+ pair.set(domain);
1471
+ pair.set(sha256(typeof up === "string" ? encoder.encode(up) : up), domain.length);
1472
+ pair.set(sha256(typeof down === "string" ? encoder.encode(down) : down), domain.length + digestSize);
1473
+ return Array.from(sha256(pair), (byte) => byte.toString(16).padStart(2, "0")).join("");
1474
+ }
1324
1475
  // src/dialect.ts
1325
1476
  var StatementEditError = class extends Data.TaggedError("StatementEditError") {};
1326
1477
  // src/structure.ts
@@ -1670,13 +1821,17 @@ const formatExtractOmissions = (omitted) => [...omitted].sort((left, right) => o
1670
1821
  });
1671
1822
  const checksumPattern = /^[a-f0-9]{64}$/;
1672
1823
  /** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
1673
- const migrationRun = (direction, migrations, checkpoint, sha256) => {
1824
+ const migrationRun = (direction, migrations, checkpoint) => {
1674
1825
  const down = direction === "down";
1675
1826
  const baseline = migrations[0]?.baselineCheckpoint !== void 0 ? migrations[0] : void 0;
1676
1827
  const checkpointId = checkpoint?.id ?? 0;
1677
1828
  const firstUpId = baseline?.id ?? checkpointId + 1;
1678
1829
  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) => index > 0 && migration.baselineCheckpoint !== void 0 || migration.id !== offset + index + 1 || !migration.sql.trim() || migration.sql.includes("\0") || sha256(migration.sql) !== migration.sha256)) return;
1830
+ 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) => {
1831
+ const { down, sql } = migration;
1832
+ const downText = "sql" in down ? down.sql : down.text;
1833
+ 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;
1834
+ })) return;
1680
1835
  return {
1681
1836
  ...baseline ? { baseline } : {},
1682
1837
  direction,
@@ -2245,4 +2400,4 @@ const decodeGeneratorPlugin = (value) => validate(() => {
2245
2400
  })
2246
2401
  };
2247
2402
  }, "invalid-plugin", "generator");
2248
- 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, generatedFileHeader, mergeMigrationDecisions, migrationRun, nameSpan, namespaceKey, namespaceOf, nextMigration, planDatabaseMigration, planDownMigration, prepareMigrationDecisions, referenceAt, referencesTo, relationIdentitySchema, renameSiteAt, renameSpans, resolveMigrationDecisions, schemaValueEqual, sequenceRenames, tableKey, triggerKey, validateApplicationCompatibility, validateDatabase };
2403
+ 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, generatedFileHeader, hashMigrationPair, mergeMigrationDecisions, migrationRun, nameSpan, namespaceKey, namespaceOf, nextMigration, planDatabaseMigration, planDownMigration, prepareMigrationDecisions, referenceAt, referencesTo, relationIdentitySchema, renameSiteAt, renameSpans, resolveMigrationDecisions, schemaValueEqual, sequenceRenames, tableKey, triggerKey, validateApplicationCompatibility, validateDatabase };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uncutsql/core",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",