@prisma-next/family-sql 0.3.0-dev.4 → 0.3.0-dev.40

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.
Files changed (74) hide show
  1. package/README.md +11 -6
  2. package/dist/assembly-BVS641kd.mjs +106 -0
  3. package/dist/assembly-BVS641kd.mjs.map +1 -0
  4. package/dist/control-adapter.d.mts +60 -0
  5. package/dist/control-adapter.d.mts.map +1 -0
  6. package/dist/control-adapter.mjs +1 -0
  7. package/dist/control-instance-Cvmn5zpn.d.mts +292 -0
  8. package/dist/control-instance-Cvmn5zpn.d.mts.map +1 -0
  9. package/dist/control.d.mts +64 -0
  10. package/dist/control.d.mts.map +1 -0
  11. package/dist/control.mjs +534 -0
  12. package/dist/control.mjs.map +1 -0
  13. package/dist/runtime.d.mts +27 -0
  14. package/dist/runtime.d.mts.map +1 -0
  15. package/dist/runtime.mjs +38 -0
  16. package/dist/runtime.mjs.map +1 -0
  17. package/dist/schema-verify.d.mts +48 -0
  18. package/dist/schema-verify.d.mts.map +1 -0
  19. package/dist/schema-verify.mjs +4 -0
  20. package/dist/test-utils.d.mts +2 -0
  21. package/dist/test-utils.mjs +3 -0
  22. package/dist/verify-BfMETJcM.mjs +108 -0
  23. package/dist/verify-BfMETJcM.mjs.map +1 -0
  24. package/dist/verify-sql-schema-B4T5MEuz.mjs +934 -0
  25. package/dist/verify-sql-schema-B4T5MEuz.mjs.map +1 -0
  26. package/dist/verify-sql-schema-CvQoGm2Q.d.mts +67 -0
  27. package/dist/verify-sql-schema-CvQoGm2Q.d.mts.map +1 -0
  28. package/dist/{exports/verify.d.ts → verify.d.mts} +8 -5
  29. package/dist/verify.d.mts.map +1 -0
  30. package/dist/verify.mjs +3 -0
  31. package/package.json +38 -48
  32. package/src/core/assembly.ts +216 -0
  33. package/src/core/control-adapter.ts +67 -0
  34. package/src/core/control-descriptor.ts +37 -0
  35. package/src/core/control-instance.ts +750 -0
  36. package/src/core/migrations/plan-helpers.ts +164 -0
  37. package/src/core/migrations/policies.ts +8 -0
  38. package/src/core/migrations/types.ts +279 -0
  39. package/src/core/runtime-descriptor.ts +23 -0
  40. package/src/core/runtime-instance.ts +22 -0
  41. package/src/core/schema-verify/verify-helpers.ts +532 -0
  42. package/src/core/schema-verify/verify-sql-schema.ts +1011 -0
  43. package/src/core/verify.ts +168 -0
  44. package/src/exports/control-adapter.ts +1 -0
  45. package/src/exports/control.ts +59 -0
  46. package/src/exports/runtime.ts +3 -0
  47. package/src/exports/schema-verify.ts +19 -0
  48. package/src/exports/test-utils.ts +10 -0
  49. package/src/exports/verify.ts +1 -0
  50. package/dist/exports/chunk-6P44BVZ4.js +0 -580
  51. package/dist/exports/chunk-6P44BVZ4.js.map +0 -1
  52. package/dist/exports/chunk-C3GKWCKA.js +0 -96
  53. package/dist/exports/chunk-C3GKWCKA.js.map +0 -1
  54. package/dist/exports/chunk-F252JMEU.js +0 -772
  55. package/dist/exports/chunk-F252JMEU.js.map +0 -1
  56. package/dist/exports/control-adapter.d.ts +0 -44
  57. package/dist/exports/control-adapter.js +0 -1
  58. package/dist/exports/control-adapter.js.map +0 -1
  59. package/dist/exports/control.d.ts +0 -75
  60. package/dist/exports/control.js +0 -149
  61. package/dist/exports/control.js.map +0 -1
  62. package/dist/exports/instance-DiZi2k_2.d.ts +0 -127
  63. package/dist/exports/runtime.d.ts +0 -66
  64. package/dist/exports/runtime.js +0 -64
  65. package/dist/exports/runtime.js.map +0 -1
  66. package/dist/exports/schema-verify.d.ts +0 -75
  67. package/dist/exports/schema-verify.js +0 -11
  68. package/dist/exports/schema-verify.js.map +0 -1
  69. package/dist/exports/test-utils.d.ts +0 -33
  70. package/dist/exports/test-utils.js +0 -17
  71. package/dist/exports/test-utils.js.map +0 -1
  72. package/dist/exports/types-Bh7ftf0Q.d.ts +0 -275
  73. package/dist/exports/verify.js +0 -11
  74. package/dist/exports/verify.js.map +0 -1
@@ -0,0 +1,934 @@
1
+ import { n as extractCodecControlHooks } from "./assembly-BVS641kd.mjs";
2
+ import { ifDefined } from "@prisma-next/utils/defined";
3
+
4
+ //#region src/core/schema-verify/verify-helpers.ts
5
+ /**
6
+ * Compares two arrays of strings for equality (order-sensitive).
7
+ */
8
+ function arraysEqual(a, b) {
9
+ if (a.length !== b.length) return false;
10
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
11
+ return true;
12
+ }
13
+ /**
14
+ * Checks if a unique constraint requirement is satisfied by the given columns.
15
+ *
16
+ * Semantic satisfaction: a unique constraint requirement can be satisfied by:
17
+ * - A unique constraint with the same columns, OR
18
+ * - A unique index with the same columns
19
+ *
20
+ * @param uniques - The unique constraints in the schema table
21
+ * @param indexes - The indexes in the schema table
22
+ * @param columns - The columns required by the unique constraint
23
+ * @returns true if the requirement is satisfied
24
+ */
25
+ function isUniqueConstraintSatisfied(uniques, indexes, columns) {
26
+ if (uniques.some((unique) => arraysEqual(unique.columns, columns))) return true;
27
+ return indexes.some((index) => index.unique && arraysEqual(index.columns, columns));
28
+ }
29
+ /**
30
+ * Checks if an index requirement is satisfied by the given columns.
31
+ *
32
+ * Semantic satisfaction: a non-unique index requirement can be satisfied by:
33
+ * - Any index (unique or non-unique) with the same columns, OR
34
+ * - A unique constraint with the same columns (stronger satisfies weaker)
35
+ *
36
+ * @param indexes - The indexes in the schema table
37
+ * @param uniques - The unique constraints in the schema table
38
+ * @param columns - The columns required by the index
39
+ * @returns true if the requirement is satisfied
40
+ */
41
+ function isIndexSatisfied(indexes, uniques, columns) {
42
+ if (indexes.some((index) => arraysEqual(index.columns, columns))) return true;
43
+ return uniques.some((unique) => arraysEqual(unique.columns, columns));
44
+ }
45
+ /**
46
+ * Verifies primary key matches between contract and schema.
47
+ * Returns 'pass' or 'fail'.
48
+ *
49
+ * Uses semantic satisfaction: identity is based on (table + kind + columns).
50
+ * Name differences are ignored by default (names are for DDL/diagnostics, not identity).
51
+ */
52
+ function verifyPrimaryKey(contractPK, schemaPK, tableName, issues) {
53
+ if (!schemaPK) {
54
+ issues.push({
55
+ kind: "primary_key_mismatch",
56
+ table: tableName,
57
+ expected: contractPK.columns.join(", "),
58
+ message: `Table "${tableName}" is missing primary key`
59
+ });
60
+ return "fail";
61
+ }
62
+ if (!arraysEqual(contractPK.columns, schemaPK.columns)) {
63
+ issues.push({
64
+ kind: "primary_key_mismatch",
65
+ table: tableName,
66
+ expected: contractPK.columns.join(", "),
67
+ actual: schemaPK.columns.join(", "),
68
+ message: `Table "${tableName}" has primary key mismatch: expected columns [${contractPK.columns.join(", ")}], got [${schemaPK.columns.join(", ")}]`
69
+ });
70
+ return "fail";
71
+ }
72
+ return "pass";
73
+ }
74
+ /**
75
+ * Verifies foreign keys match between contract and schema.
76
+ * Returns verification nodes for the tree.
77
+ *
78
+ * Uses semantic satisfaction: identity is based on (table + columns + referenced table + referenced columns).
79
+ * Name differences are ignored by default (names are for DDL/diagnostics, not identity).
80
+ */
81
+ function verifyForeignKeys(contractFKs, schemaFKs, tableName, tablePath, issues, strict) {
82
+ const nodes = [];
83
+ for (const contractFK of contractFKs) {
84
+ const fkPath = `${tablePath}.foreignKeys[${contractFK.columns.join(",")}]`;
85
+ if (!schemaFKs.find((fk) => {
86
+ return arraysEqual(fk.columns, contractFK.columns) && fk.referencedTable === contractFK.references.table && arraysEqual(fk.referencedColumns, contractFK.references.columns);
87
+ })) {
88
+ issues.push({
89
+ kind: "foreign_key_mismatch",
90
+ table: tableName,
91
+ expected: `${contractFK.columns.join(", ")} -> ${contractFK.references.table}(${contractFK.references.columns.join(", ")})`,
92
+ message: `Table "${tableName}" is missing foreign key: ${contractFK.columns.join(", ")} -> ${contractFK.references.table}(${contractFK.references.columns.join(", ")})`
93
+ });
94
+ nodes.push({
95
+ status: "fail",
96
+ kind: "foreignKey",
97
+ name: `foreignKey(${contractFK.columns.join(", ")})`,
98
+ contractPath: fkPath,
99
+ code: "foreign_key_mismatch",
100
+ message: "Foreign key missing",
101
+ expected: contractFK,
102
+ actual: void 0,
103
+ children: []
104
+ });
105
+ } else nodes.push({
106
+ status: "pass",
107
+ kind: "foreignKey",
108
+ name: `foreignKey(${contractFK.columns.join(", ")})`,
109
+ contractPath: fkPath,
110
+ code: "",
111
+ message: "",
112
+ expected: void 0,
113
+ actual: void 0,
114
+ children: []
115
+ });
116
+ }
117
+ if (strict) {
118
+ for (const schemaFK of schemaFKs) if (!contractFKs.find((fk) => {
119
+ return arraysEqual(fk.columns, schemaFK.columns) && fk.references.table === schemaFK.referencedTable && arraysEqual(fk.references.columns, schemaFK.referencedColumns);
120
+ })) {
121
+ issues.push({
122
+ kind: "extra_foreign_key",
123
+ table: tableName,
124
+ message: `Extra foreign key found in database (not in contract): ${schemaFK.columns.join(", ")} -> ${schemaFK.referencedTable}(${schemaFK.referencedColumns.join(", ")})`
125
+ });
126
+ nodes.push({
127
+ status: "fail",
128
+ kind: "foreignKey",
129
+ name: `foreignKey(${schemaFK.columns.join(", ")})`,
130
+ contractPath: `${tablePath}.foreignKeys[${schemaFK.columns.join(",")}]`,
131
+ code: "extra_foreign_key",
132
+ message: "Extra foreign key found",
133
+ expected: void 0,
134
+ actual: schemaFK,
135
+ children: []
136
+ });
137
+ }
138
+ }
139
+ return nodes;
140
+ }
141
+ /**
142
+ * Verifies unique constraints match between contract and schema.
143
+ * Returns verification nodes for the tree.
144
+ *
145
+ * Uses semantic satisfaction: identity is based on (table + kind + columns).
146
+ * A unique constraint requirement can be satisfied by either:
147
+ * - A unique constraint with the same columns, or
148
+ * - A unique index with the same columns
149
+ *
150
+ * Name differences are ignored by default (names are for DDL/diagnostics, not identity).
151
+ */
152
+ function verifyUniqueConstraints(contractUniques, schemaUniques, schemaIndexes, tableName, tablePath, issues, strict) {
153
+ const nodes = [];
154
+ for (const contractUnique of contractUniques) {
155
+ const uniquePath = `${tablePath}.uniques[${contractUnique.columns.join(",")}]`;
156
+ const matchingUnique = schemaUniques.find((u) => arraysEqual(u.columns, contractUnique.columns));
157
+ const matchingUniqueIndex = !matchingUnique && schemaIndexes.find((idx) => idx.unique && arraysEqual(idx.columns, contractUnique.columns));
158
+ if (!matchingUnique && !matchingUniqueIndex) {
159
+ issues.push({
160
+ kind: "unique_constraint_mismatch",
161
+ table: tableName,
162
+ expected: contractUnique.columns.join(", "),
163
+ message: `Table "${tableName}" is missing unique constraint: ${contractUnique.columns.join(", ")}`
164
+ });
165
+ nodes.push({
166
+ status: "fail",
167
+ kind: "unique",
168
+ name: `unique(${contractUnique.columns.join(", ")})`,
169
+ contractPath: uniquePath,
170
+ code: "unique_constraint_mismatch",
171
+ message: "Unique constraint missing",
172
+ expected: contractUnique,
173
+ actual: void 0,
174
+ children: []
175
+ });
176
+ } else nodes.push({
177
+ status: "pass",
178
+ kind: "unique",
179
+ name: `unique(${contractUnique.columns.join(", ")})`,
180
+ contractPath: uniquePath,
181
+ code: "",
182
+ message: "",
183
+ expected: void 0,
184
+ actual: void 0,
185
+ children: []
186
+ });
187
+ }
188
+ if (strict) {
189
+ for (const schemaUnique of schemaUniques) if (!contractUniques.find((u) => arraysEqual(u.columns, schemaUnique.columns))) {
190
+ issues.push({
191
+ kind: "extra_unique_constraint",
192
+ table: tableName,
193
+ message: `Extra unique constraint found in database (not in contract): ${schemaUnique.columns.join(", ")}`
194
+ });
195
+ nodes.push({
196
+ status: "fail",
197
+ kind: "unique",
198
+ name: `unique(${schemaUnique.columns.join(", ")})`,
199
+ contractPath: `${tablePath}.uniques[${schemaUnique.columns.join(",")}]`,
200
+ code: "extra_unique_constraint",
201
+ message: "Extra unique constraint found",
202
+ expected: void 0,
203
+ actual: schemaUnique,
204
+ children: []
205
+ });
206
+ }
207
+ }
208
+ return nodes;
209
+ }
210
+ /**
211
+ * Verifies indexes match between contract and schema.
212
+ * Returns verification nodes for the tree.
213
+ *
214
+ * Uses semantic satisfaction: identity is based on (table + kind + columns).
215
+ * A non-unique index requirement can be satisfied by either:
216
+ * - A non-unique index with the same columns, or
217
+ * - A unique index with the same columns (stronger satisfies weaker)
218
+ *
219
+ * Name differences are ignored by default (names are for DDL/diagnostics, not identity).
220
+ */
221
+ function verifyIndexes(contractIndexes, schemaIndexes, schemaUniques, tableName, tablePath, issues, strict) {
222
+ const nodes = [];
223
+ for (const contractIndex of contractIndexes) {
224
+ const indexPath = `${tablePath}.indexes[${contractIndex.columns.join(",")}]`;
225
+ const matchingIndex = schemaIndexes.find((idx) => arraysEqual(idx.columns, contractIndex.columns));
226
+ const matchingUniqueConstraint = !matchingIndex && schemaUniques.find((u) => arraysEqual(u.columns, contractIndex.columns));
227
+ if (!matchingIndex && !matchingUniqueConstraint) {
228
+ issues.push({
229
+ kind: "index_mismatch",
230
+ table: tableName,
231
+ expected: contractIndex.columns.join(", "),
232
+ message: `Table "${tableName}" is missing index: ${contractIndex.columns.join(", ")}`
233
+ });
234
+ nodes.push({
235
+ status: "fail",
236
+ kind: "index",
237
+ name: `index(${contractIndex.columns.join(", ")})`,
238
+ contractPath: indexPath,
239
+ code: "index_mismatch",
240
+ message: "Index missing",
241
+ expected: contractIndex,
242
+ actual: void 0,
243
+ children: []
244
+ });
245
+ } else nodes.push({
246
+ status: "pass",
247
+ kind: "index",
248
+ name: `index(${contractIndex.columns.join(", ")})`,
249
+ contractPath: indexPath,
250
+ code: "",
251
+ message: "",
252
+ expected: void 0,
253
+ actual: void 0,
254
+ children: []
255
+ });
256
+ }
257
+ if (strict) for (const schemaIndex of schemaIndexes) {
258
+ if (schemaIndex.unique) continue;
259
+ if (!contractIndexes.find((idx) => arraysEqual(idx.columns, schemaIndex.columns))) {
260
+ issues.push({
261
+ kind: "extra_index",
262
+ table: tableName,
263
+ message: `Extra index found in database (not in contract): ${schemaIndex.columns.join(", ")}`
264
+ });
265
+ nodes.push({
266
+ status: "fail",
267
+ kind: "index",
268
+ name: `index(${schemaIndex.columns.join(", ")})`,
269
+ contractPath: `${tablePath}.indexes[${schemaIndex.columns.join(",")}]`,
270
+ code: "extra_index",
271
+ message: "Extra index found",
272
+ expected: void 0,
273
+ actual: schemaIndex,
274
+ children: []
275
+ });
276
+ }
277
+ }
278
+ return nodes;
279
+ }
280
+ /**
281
+ * Verifies database dependencies are installed using component-owned verification hooks.
282
+ * Each dependency provides a pure verifyDatabaseDependencyInstalled function that checks
283
+ * whether the dependency is satisfied based on the in-memory schema IR (no DB I/O).
284
+ *
285
+ * Returns verification nodes for the tree.
286
+ */
287
+ function verifyDatabaseDependencies(dependencies, schema, issues) {
288
+ const nodes = [];
289
+ for (const dependency of dependencies) {
290
+ const depIssues = dependency.verifyDatabaseDependencyInstalled(schema);
291
+ const depPath = `dependencies.${dependency.id}`;
292
+ if (depIssues.length > 0) {
293
+ issues.push(...depIssues);
294
+ const issuesMessage = depIssues.map((i) => i.message).join("; ");
295
+ const nodeMessage = issuesMessage ? `${dependency.id}: ${issuesMessage}` : dependency.id;
296
+ nodes.push({
297
+ status: "fail",
298
+ kind: "databaseDependency",
299
+ name: dependency.label,
300
+ contractPath: depPath,
301
+ code: "dependency_missing",
302
+ message: nodeMessage,
303
+ expected: void 0,
304
+ actual: void 0,
305
+ children: []
306
+ });
307
+ } else nodes.push({
308
+ status: "pass",
309
+ kind: "databaseDependency",
310
+ name: dependency.label,
311
+ contractPath: depPath,
312
+ code: "",
313
+ message: "",
314
+ expected: void 0,
315
+ actual: void 0,
316
+ children: []
317
+ });
318
+ }
319
+ return nodes;
320
+ }
321
+ /**
322
+ * Computes counts of pass/warn/fail nodes by traversing the tree.
323
+ */
324
+ function computeCounts(node) {
325
+ let pass = 0;
326
+ let warn = 0;
327
+ let fail = 0;
328
+ function traverse(n) {
329
+ if (n.status === "pass") pass++;
330
+ else if (n.status === "warn") warn++;
331
+ else if (n.status === "fail") fail++;
332
+ if (n.children) for (const child of n.children) traverse(child);
333
+ }
334
+ traverse(node);
335
+ return {
336
+ pass,
337
+ warn,
338
+ fail,
339
+ totalNodes: pass + warn + fail
340
+ };
341
+ }
342
+
343
+ //#endregion
344
+ //#region src/core/schema-verify/verify-sql-schema.ts
345
+ /**
346
+ * Verifies that a SqlSchemaIR matches a SqlContract.
347
+ *
348
+ * This is a pure function that does NOT perform any database I/O.
349
+ * It takes an already-introspected schema IR and compares it against
350
+ * the contract requirements.
351
+ *
352
+ * @param options - Verification options
353
+ * @returns VerifyDatabaseSchemaResult with verification tree and issues
354
+ */
355
+ function verifySqlSchema(options) {
356
+ const { contract, schema, strict, context, typeMetadataRegistry, normalizeDefault, normalizeNativeType } = options;
357
+ const startTime = Date.now();
358
+ const codecHooks = extractCodecControlHooks(options.frameworkComponents);
359
+ const { contractStorageHash, contractProfileHash, contractTarget } = extractContractMetadata(contract);
360
+ const { issues, rootChildren } = verifySchemaTables({
361
+ contract,
362
+ schema,
363
+ strict,
364
+ typeMetadataRegistry,
365
+ codecHooks,
366
+ ...ifDefined("normalizeDefault", normalizeDefault),
367
+ ...ifDefined("normalizeNativeType", normalizeNativeType)
368
+ });
369
+ validateFrameworkComponentsForExtensions(contract, options.frameworkComponents);
370
+ const storageTypes = contract.storage.types ?? {};
371
+ const storageTypeEntries = Object.entries(storageTypes);
372
+ if (storageTypeEntries.length > 0) {
373
+ const typeNodes = [];
374
+ for (const [typeName, typeInstance] of storageTypeEntries) {
375
+ const hook = codecHooks.get(typeInstance.codecId);
376
+ const typeIssues = hook?.verifyType ? hook.verifyType({
377
+ typeName,
378
+ typeInstance,
379
+ schema
380
+ }) : [];
381
+ if (typeIssues.length > 0) issues.push(...typeIssues);
382
+ const typeStatus = typeIssues.length > 0 ? "fail" : "pass";
383
+ const typeCode = typeIssues.length > 0 ? typeIssues[0]?.kind ?? "" : "";
384
+ typeNodes.push({
385
+ status: typeStatus,
386
+ kind: "storageType",
387
+ name: `type ${typeName}`,
388
+ contractPath: `storage.types.${typeName}`,
389
+ code: typeCode,
390
+ message: typeIssues.length > 0 ? `${typeIssues.length} issue${typeIssues.length === 1 ? "" : "s"}` : "",
391
+ expected: void 0,
392
+ actual: void 0,
393
+ children: []
394
+ });
395
+ }
396
+ const typesStatus = typeNodes.some((n) => n.status === "fail") ? "fail" : "pass";
397
+ rootChildren.push({
398
+ status: typesStatus,
399
+ kind: "storageTypes",
400
+ name: "types",
401
+ contractPath: "storage.types",
402
+ code: typesStatus === "fail" ? "type_mismatch" : "",
403
+ message: "",
404
+ expected: void 0,
405
+ actual: void 0,
406
+ children: typeNodes
407
+ });
408
+ }
409
+ const dependencyStatuses = verifyDatabaseDependencies(collectDependenciesFromFrameworkComponents(options.frameworkComponents), schema, issues);
410
+ rootChildren.push(...dependencyStatuses);
411
+ const root = buildRootNode(rootChildren);
412
+ const counts = computeCounts(root);
413
+ const ok = counts.fail === 0;
414
+ const code = ok ? void 0 : "PN-SCHEMA-0001";
415
+ const summary = ok ? "Database schema satisfies contract" : `Database schema does not satisfy contract (${counts.fail} failure${counts.fail === 1 ? "" : "s"})`;
416
+ const totalTime = Date.now() - startTime;
417
+ return {
418
+ ok,
419
+ ...ifDefined("code", code),
420
+ summary,
421
+ contract: {
422
+ storageHash: contractStorageHash,
423
+ ...ifDefined("profileHash", contractProfileHash)
424
+ },
425
+ target: {
426
+ expected: contractTarget,
427
+ actual: contractTarget
428
+ },
429
+ schema: {
430
+ issues,
431
+ root,
432
+ counts
433
+ },
434
+ meta: {
435
+ strict,
436
+ ...ifDefined("contractPath", context?.contractPath),
437
+ ...ifDefined("configPath", context?.configPath)
438
+ },
439
+ timings: { total: totalTime }
440
+ };
441
+ }
442
+ function extractContractMetadata(contract) {
443
+ return {
444
+ contractStorageHash: contract.storageHash,
445
+ contractProfileHash: "profileHash" in contract && typeof contract.profileHash === "string" ? contract.profileHash : void 0,
446
+ contractTarget: contract.target
447
+ };
448
+ }
449
+ function verifySchemaTables(options) {
450
+ const { contract, schema, strict, typeMetadataRegistry, codecHooks, normalizeDefault, normalizeNativeType } = options;
451
+ const issues = [];
452
+ const rootChildren = [];
453
+ const contractTables = contract.storage.tables;
454
+ const schemaTables = schema.tables;
455
+ for (const [tableName, contractTable] of Object.entries(contractTables)) {
456
+ const schemaTable = schemaTables[tableName];
457
+ const tablePath = `storage.tables.${tableName}`;
458
+ if (!schemaTable) {
459
+ issues.push({
460
+ kind: "missing_table",
461
+ table: tableName,
462
+ message: `Table "${tableName}" is missing from database`
463
+ });
464
+ rootChildren.push({
465
+ status: "fail",
466
+ kind: "table",
467
+ name: `table ${tableName}`,
468
+ contractPath: tablePath,
469
+ code: "missing_table",
470
+ message: `Table "${tableName}" is missing`,
471
+ expected: void 0,
472
+ actual: void 0,
473
+ children: []
474
+ });
475
+ continue;
476
+ }
477
+ const tableChildren = verifyTableChildren({
478
+ contractTable,
479
+ schemaTable,
480
+ tableName,
481
+ tablePath,
482
+ issues,
483
+ strict,
484
+ typeMetadataRegistry,
485
+ codecHooks,
486
+ ...ifDefined("normalizeDefault", normalizeDefault),
487
+ ...ifDefined("normalizeNativeType", normalizeNativeType)
488
+ });
489
+ rootChildren.push(buildTableNode(tableName, tablePath, tableChildren));
490
+ }
491
+ if (strict) {
492
+ for (const tableName of Object.keys(schemaTables)) if (!contractTables[tableName]) {
493
+ issues.push({
494
+ kind: "extra_table",
495
+ table: tableName,
496
+ message: `Extra table "${tableName}" found in database (not in contract)`
497
+ });
498
+ rootChildren.push({
499
+ status: "fail",
500
+ kind: "table",
501
+ name: `table ${tableName}`,
502
+ contractPath: `storage.tables.${tableName}`,
503
+ code: "extra_table",
504
+ message: `Extra table "${tableName}" found`,
505
+ expected: void 0,
506
+ actual: void 0,
507
+ children: []
508
+ });
509
+ }
510
+ }
511
+ return {
512
+ issues,
513
+ rootChildren
514
+ };
515
+ }
516
+ function verifyTableChildren(options) {
517
+ const { contractTable, schemaTable, tableName, tablePath, issues, strict, typeMetadataRegistry, codecHooks, normalizeDefault, normalizeNativeType } = options;
518
+ const tableChildren = [];
519
+ const columnNodes = collectContractColumnNodes({
520
+ contractTable,
521
+ schemaTable,
522
+ tableName,
523
+ tablePath,
524
+ issues,
525
+ typeMetadataRegistry,
526
+ codecHooks,
527
+ ...ifDefined("normalizeDefault", normalizeDefault),
528
+ ...ifDefined("normalizeNativeType", normalizeNativeType)
529
+ });
530
+ if (columnNodes.length > 0) tableChildren.push(buildColumnsNode(tablePath, columnNodes));
531
+ if (strict) appendExtraColumnNodes({
532
+ contractTable,
533
+ schemaTable,
534
+ tableName,
535
+ tablePath,
536
+ issues,
537
+ columnNodes
538
+ });
539
+ if (contractTable.primaryKey) if (verifyPrimaryKey(contractTable.primaryKey, schemaTable.primaryKey, tableName, issues) === "fail") tableChildren.push({
540
+ status: "fail",
541
+ kind: "primaryKey",
542
+ name: `primary key: ${contractTable.primaryKey.columns.join(", ")}`,
543
+ contractPath: `${tablePath}.primaryKey`,
544
+ code: "primary_key_mismatch",
545
+ message: "Primary key mismatch",
546
+ expected: contractTable.primaryKey,
547
+ actual: schemaTable.primaryKey,
548
+ children: []
549
+ });
550
+ else tableChildren.push({
551
+ status: "pass",
552
+ kind: "primaryKey",
553
+ name: `primary key: ${contractTable.primaryKey.columns.join(", ")}`,
554
+ contractPath: `${tablePath}.primaryKey`,
555
+ code: "",
556
+ message: "",
557
+ expected: void 0,
558
+ actual: void 0,
559
+ children: []
560
+ });
561
+ else if (schemaTable.primaryKey && strict) {
562
+ issues.push({
563
+ kind: "extra_primary_key",
564
+ table: tableName,
565
+ message: "Extra primary key found in database (not in contract)"
566
+ });
567
+ tableChildren.push({
568
+ status: "fail",
569
+ kind: "primaryKey",
570
+ name: `primary key: ${schemaTable.primaryKey.columns.join(", ")}`,
571
+ contractPath: `${tablePath}.primaryKey`,
572
+ code: "extra_primary_key",
573
+ message: "Extra primary key found",
574
+ expected: void 0,
575
+ actual: schemaTable.primaryKey,
576
+ children: []
577
+ });
578
+ }
579
+ const constraintFks = contractTable.foreignKeys.filter((fk) => fk.constraint === true);
580
+ if (constraintFks.length > 0) {
581
+ const fkStatuses = verifyForeignKeys(constraintFks, schemaTable.foreignKeys, tableName, tablePath, issues, strict);
582
+ tableChildren.push(...fkStatuses);
583
+ }
584
+ const uniqueStatuses = verifyUniqueConstraints(contractTable.uniques, schemaTable.uniques, schemaTable.indexes, tableName, tablePath, issues, strict);
585
+ tableChildren.push(...uniqueStatuses);
586
+ const fkBackingIndexes = contractTable.foreignKeys.filter((fk) => fk.index === true && !contractTable.indexes.some((idx) => arraysEqual(idx.columns, fk.columns))).map((fk) => ({ columns: fk.columns }));
587
+ const indexStatuses = verifyIndexes([...contractTable.indexes, ...fkBackingIndexes], schemaTable.indexes, schemaTable.uniques, tableName, tablePath, issues, strict);
588
+ tableChildren.push(...indexStatuses);
589
+ return tableChildren;
590
+ }
591
+ function collectContractColumnNodes(options) {
592
+ const { contractTable, schemaTable, tableName, tablePath, issues, typeMetadataRegistry, codecHooks, normalizeDefault, normalizeNativeType } = options;
593
+ const columnNodes = [];
594
+ for (const [columnName, contractColumn] of Object.entries(contractTable.columns)) {
595
+ const schemaColumn = schemaTable.columns[columnName];
596
+ const columnPath = `${tablePath}.columns.${columnName}`;
597
+ if (!schemaColumn) {
598
+ issues.push({
599
+ kind: "missing_column",
600
+ table: tableName,
601
+ column: columnName,
602
+ message: `Column "${tableName}"."${columnName}" is missing from database`
603
+ });
604
+ columnNodes.push({
605
+ status: "fail",
606
+ kind: "column",
607
+ name: `${columnName}: missing`,
608
+ contractPath: columnPath,
609
+ code: "missing_column",
610
+ message: `Column "${columnName}" is missing`,
611
+ expected: void 0,
612
+ actual: void 0,
613
+ children: []
614
+ });
615
+ continue;
616
+ }
617
+ columnNodes.push(verifyColumn({
618
+ tableName,
619
+ columnName,
620
+ contractColumn,
621
+ schemaColumn,
622
+ columnPath,
623
+ issues,
624
+ typeMetadataRegistry,
625
+ codecHooks,
626
+ ...ifDefined("normalizeDefault", normalizeDefault),
627
+ ...ifDefined("normalizeNativeType", normalizeNativeType)
628
+ }));
629
+ }
630
+ return columnNodes;
631
+ }
632
+ function appendExtraColumnNodes(options) {
633
+ const { contractTable, schemaTable, tableName, tablePath, issues, columnNodes } = options;
634
+ for (const [columnName, { nativeType }] of Object.entries(schemaTable.columns)) if (!contractTable.columns[columnName]) {
635
+ issues.push({
636
+ kind: "extra_column",
637
+ table: tableName,
638
+ column: columnName,
639
+ message: `Extra column "${tableName}"."${columnName}" found in database (not in contract)`
640
+ });
641
+ columnNodes.push({
642
+ status: "fail",
643
+ kind: "column",
644
+ name: `${columnName}: extra`,
645
+ contractPath: `${tablePath}.columns.${columnName}`,
646
+ code: "extra_column",
647
+ message: `Extra column "${columnName}" found`,
648
+ expected: void 0,
649
+ actual: nativeType,
650
+ children: []
651
+ });
652
+ }
653
+ }
654
+ function verifyColumn(options) {
655
+ const { tableName, columnName, contractColumn, schemaColumn, columnPath, issues, codecHooks, normalizeDefault, normalizeNativeType } = options;
656
+ const columnChildren = [];
657
+ let columnStatus = "pass";
658
+ const contractNativeType = renderExpectedNativeType(contractColumn, codecHooks);
659
+ const schemaNativeType = normalizeNativeType?.(schemaColumn.nativeType) ?? schemaColumn.nativeType;
660
+ if (contractNativeType !== schemaNativeType) {
661
+ issues.push({
662
+ kind: "type_mismatch",
663
+ table: tableName,
664
+ column: columnName,
665
+ expected: contractNativeType,
666
+ actual: schemaNativeType,
667
+ message: `Column "${tableName}"."${columnName}" has type mismatch: expected "${contractNativeType}", got "${schemaNativeType}"`
668
+ });
669
+ columnChildren.push({
670
+ status: "fail",
671
+ kind: "type",
672
+ name: "type",
673
+ contractPath: `${columnPath}.nativeType`,
674
+ code: "type_mismatch",
675
+ message: `Type mismatch: expected ${contractNativeType}, got ${schemaNativeType}`,
676
+ expected: contractNativeType,
677
+ actual: schemaNativeType,
678
+ children: []
679
+ });
680
+ columnStatus = "fail";
681
+ }
682
+ if (contractColumn.codecId) {
683
+ const typeMetadata = options.typeMetadataRegistry.get(contractColumn.codecId);
684
+ if (!typeMetadata) columnChildren.push({
685
+ status: "warn",
686
+ kind: "type",
687
+ name: "type_metadata_missing",
688
+ contractPath: `${columnPath}.codecId`,
689
+ code: "type_metadata_missing",
690
+ message: `codecId "${contractColumn.codecId}" not found in type metadata registry`,
691
+ expected: contractColumn.codecId,
692
+ actual: void 0,
693
+ children: []
694
+ });
695
+ else if (typeMetadata.nativeType && typeMetadata.nativeType !== contractColumn.nativeType) columnChildren.push({
696
+ status: "warn",
697
+ kind: "type",
698
+ name: "type_consistency",
699
+ contractPath: `${columnPath}.codecId`,
700
+ code: "type_consistency_warning",
701
+ message: `codecId "${contractColumn.codecId}" maps to nativeType "${typeMetadata.nativeType}" in registry, but contract has "${contractColumn.nativeType}"`,
702
+ expected: typeMetadata.nativeType,
703
+ actual: contractColumn.nativeType,
704
+ children: []
705
+ });
706
+ }
707
+ if (contractColumn.nullable !== schemaColumn.nullable) {
708
+ issues.push({
709
+ kind: "nullability_mismatch",
710
+ table: tableName,
711
+ column: columnName,
712
+ expected: String(contractColumn.nullable),
713
+ actual: String(schemaColumn.nullable),
714
+ message: `Column "${tableName}"."${columnName}" has nullability mismatch: expected ${contractColumn.nullable ? "nullable" : "not null"}, got ${schemaColumn.nullable ? "nullable" : "not null"}`
715
+ });
716
+ columnChildren.push({
717
+ status: "fail",
718
+ kind: "nullability",
719
+ name: "nullability",
720
+ contractPath: `${columnPath}.nullable`,
721
+ code: "nullability_mismatch",
722
+ message: `Nullability mismatch: expected ${contractColumn.nullable ? "nullable" : "not null"}, got ${schemaColumn.nullable ? "nullable" : "not null"}`,
723
+ expected: contractColumn.nullable,
724
+ actual: schemaColumn.nullable,
725
+ children: []
726
+ });
727
+ columnStatus = "fail";
728
+ }
729
+ if (contractColumn.default) {
730
+ if (!schemaColumn.default) {
731
+ const defaultDescription = describeColumnDefault(contractColumn.default);
732
+ issues.push({
733
+ kind: "default_missing",
734
+ table: tableName,
735
+ column: columnName,
736
+ expected: defaultDescription,
737
+ message: `Column "${tableName}"."${columnName}" should have default ${defaultDescription} but database has no default`
738
+ });
739
+ columnChildren.push({
740
+ status: "fail",
741
+ kind: "default",
742
+ name: "default",
743
+ contractPath: `${columnPath}.default`,
744
+ code: "default_missing",
745
+ message: `Default missing: expected ${defaultDescription}`,
746
+ expected: defaultDescription,
747
+ actual: void 0,
748
+ children: []
749
+ });
750
+ columnStatus = "fail";
751
+ } else if (!columnDefaultsEqual(contractColumn.default, schemaColumn.default, normalizeDefault, schemaNativeType)) {
752
+ const expectedDescription = describeColumnDefault(contractColumn.default);
753
+ const actualDescription = schemaColumn.default;
754
+ issues.push({
755
+ kind: "default_mismatch",
756
+ table: tableName,
757
+ column: columnName,
758
+ expected: expectedDescription,
759
+ actual: actualDescription,
760
+ message: `Column "${tableName}"."${columnName}" has default mismatch: expected ${expectedDescription}, got ${actualDescription}`
761
+ });
762
+ columnChildren.push({
763
+ status: "fail",
764
+ kind: "default",
765
+ name: "default",
766
+ contractPath: `${columnPath}.default`,
767
+ code: "default_mismatch",
768
+ message: `Default mismatch: expected ${expectedDescription}, got ${actualDescription}`,
769
+ expected: expectedDescription,
770
+ actual: actualDescription,
771
+ children: []
772
+ });
773
+ columnStatus = "fail";
774
+ }
775
+ }
776
+ const aggregated = aggregateChildState(columnChildren, columnStatus);
777
+ const nullableText = contractColumn.nullable ? "nullable" : "not nullable";
778
+ const columnTypeDisplay = contractColumn.codecId ? `${contractNativeType} (${contractColumn.codecId})` : contractNativeType;
779
+ const columnMessage = aggregated.failureMessages.join("; ");
780
+ return {
781
+ status: aggregated.status,
782
+ kind: "column",
783
+ name: `${columnName}: ${columnTypeDisplay} (${nullableText})`,
784
+ contractPath: columnPath,
785
+ code: aggregated.firstCode,
786
+ message: columnMessage,
787
+ expected: void 0,
788
+ actual: void 0,
789
+ children: columnChildren
790
+ };
791
+ }
792
+ function buildColumnsNode(tablePath, columnNodes) {
793
+ return {
794
+ status: aggregateChildState(columnNodes, "pass").status,
795
+ kind: "columns",
796
+ name: "columns",
797
+ contractPath: `${tablePath}.columns`,
798
+ code: "",
799
+ message: "",
800
+ expected: void 0,
801
+ actual: void 0,
802
+ children: columnNodes
803
+ };
804
+ }
805
+ function buildTableNode(tableName, tablePath, tableChildren) {
806
+ const tableStatus = aggregateChildState(tableChildren, "pass").status;
807
+ const tableFailureMessages = tableChildren.filter((child) => child.status === "fail" && child.message).map((child) => child.message).filter((msg) => typeof msg === "string" && msg.length > 0);
808
+ const tableMessage = tableStatus === "fail" && tableFailureMessages.length > 0 ? `${tableFailureMessages.length} issue${tableFailureMessages.length === 1 ? "" : "s"}` : "";
809
+ const tableCode = tableStatus === "fail" && tableChildren.length > 0 && tableChildren[0] ? tableChildren[0].code : "";
810
+ return {
811
+ status: tableStatus,
812
+ kind: "table",
813
+ name: `table ${tableName}`,
814
+ contractPath: tablePath,
815
+ code: tableCode,
816
+ message: tableMessage,
817
+ expected: void 0,
818
+ actual: void 0,
819
+ children: tableChildren
820
+ };
821
+ }
822
+ function buildRootNode(rootChildren) {
823
+ return {
824
+ status: aggregateChildState(rootChildren, "pass").status,
825
+ kind: "contract",
826
+ name: "contract",
827
+ contractPath: "",
828
+ code: "",
829
+ message: "",
830
+ expected: void 0,
831
+ actual: void 0,
832
+ children: rootChildren
833
+ };
834
+ }
835
+ /**
836
+ * Aggregates status, failure messages, and code from children in a single pass.
837
+ * This is more efficient than calling separate functions that each iterate the array.
838
+ */
839
+ function aggregateChildState(children, fallback) {
840
+ let status = fallback;
841
+ const failureMessages = [];
842
+ let firstCode = "";
843
+ for (const child of children) if (child.status === "fail") {
844
+ status = "fail";
845
+ if (!firstCode) firstCode = child.code;
846
+ if (child.message && typeof child.message === "string" && child.message.length > 0) failureMessages.push(child.message);
847
+ } else if (child.status === "warn" && status !== "fail") {
848
+ status = "warn";
849
+ if (!firstCode) firstCode = child.code;
850
+ }
851
+ return {
852
+ status,
853
+ failureMessages,
854
+ firstCode
855
+ };
856
+ }
857
+ function validateFrameworkComponentsForExtensions(contract, frameworkComponents) {
858
+ const contractExtensionPacks = contract.extensionPacks ?? {};
859
+ for (const extensionNamespace of Object.keys(contractExtensionPacks)) if (!frameworkComponents.some((component) => component.id === extensionNamespace && (component.kind === "extension" || component.kind === "adapter" || component.kind === "target"))) throw new Error(`Extension pack '${extensionNamespace}' is declared in the contract but not found in framework components. This indicates a configuration mismatch - the contract was emitted with this extension pack, but it is not provided in the current configuration.`);
860
+ }
861
+ /**
862
+ * Type predicate to check if a component has database dependencies with an init array.
863
+ * The familyId check is redundant since TargetBoundComponentDescriptor<'sql', T> already
864
+ * guarantees familyId is 'sql' at the type level, so we don't need runtime checks for it.
865
+ */
866
+ function hasDatabaseDependenciesInit(component) {
867
+ if (!("databaseDependencies" in component)) return false;
868
+ const dbDeps = component["databaseDependencies"];
869
+ if (dbDeps === void 0 || dbDeps === null || typeof dbDeps !== "object") return false;
870
+ const init = dbDeps["init"];
871
+ if (init === void 0 || !Array.isArray(init)) return false;
872
+ return true;
873
+ }
874
+ function collectDependenciesFromFrameworkComponents(components) {
875
+ const dependencies = [];
876
+ for (const component of components) if (hasDatabaseDependenciesInit(component)) dependencies.push(...component.databaseDependencies.init);
877
+ return dependencies;
878
+ }
879
+ /**
880
+ * Renders the expected native type for a contract column, expanding parameterized types
881
+ * using codec control hooks when available.
882
+ *
883
+ * This function delegates to the `expandNativeType` hook if the codec provides one,
884
+ * ensuring that the SQL family layer remains dialect-agnostic while allowing
885
+ * target-specific adapters (like Postgres) to provide their own expansion logic.
886
+ */
887
+ function renderExpectedNativeType(contractColumn, codecHooks) {
888
+ const { codecId, nativeType, typeParams } = contractColumn;
889
+ if (!typeParams || !codecId) return nativeType;
890
+ const hooks = codecHooks.get(codecId);
891
+ if (hooks?.expandNativeType) return hooks.expandNativeType({
892
+ nativeType,
893
+ codecId,
894
+ typeParams
895
+ });
896
+ return nativeType;
897
+ }
898
+ /**
899
+ * Describes a column default for display purposes.
900
+ */
901
+ function describeColumnDefault(columnDefault) {
902
+ switch (columnDefault.kind) {
903
+ case "literal": return `literal(${columnDefault.expression})`;
904
+ case "function": return columnDefault.expression;
905
+ }
906
+ }
907
+ /**
908
+ * Compares a contract ColumnDefault against a schema raw default string for semantic equality.
909
+ *
910
+ * When a normalizer is provided, the raw schema default is first normalized to a ColumnDefault
911
+ * before comparison. Without a normalizer, falls back to direct string comparison against
912
+ * the contract expression.
913
+ *
914
+ * @param contractDefault - The expected default from the contract (normalized ColumnDefault)
915
+ * @param schemaDefault - The raw default expression from the database (string)
916
+ * @param normalizer - Optional target-specific normalizer to convert raw defaults
917
+ * @param nativeType - The column's native type, passed to normalizer for context
918
+ */
919
+ function columnDefaultsEqual(contractDefault, schemaDefault, normalizer, nativeType) {
920
+ if (!normalizer) return contractDefault.expression === schemaDefault;
921
+ const normalizedSchema = normalizer(schemaDefault, nativeType ?? "");
922
+ if (!normalizedSchema) return false;
923
+ if (contractDefault.kind !== normalizedSchema.kind) return false;
924
+ if (contractDefault.kind === "literal" && normalizedSchema.kind === "literal") return (normalizer(contractDefault.expression, nativeType ?? "")?.expression ?? contractDefault.expression).trim() === normalizedSchema.expression.trim();
925
+ if (contractDefault.kind === "function" && normalizedSchema.kind === "function") {
926
+ const normalizeExpr = (expr) => expr.toLowerCase().replace(/\s+/g, "");
927
+ return normalizeExpr(contractDefault.expression) === normalizeExpr(normalizedSchema.expression);
928
+ }
929
+ return false;
930
+ }
931
+
932
+ //#endregion
933
+ export { verifyDatabaseDependencies as a, isUniqueConstraintSatisfied as i, arraysEqual as n, isIndexSatisfied as r, verifySqlSchema as t };
934
+ //# sourceMappingURL=verify-sql-schema-B4T5MEuz.mjs.map