prisma-next 0.5.0-dev.66 → 0.5.0-dev.68

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 (115) hide show
  1. package/dist/{cli-errors-By1iVE3z.mjs → cli-errors-D3_sMh2K.mjs} +2 -3
  2. package/dist/{cli-errors-By1iVE3z.mjs.map → cli-errors-D3_sMh2K.mjs.map} +1 -1
  3. package/dist/{cli-errors-DDeVsP2Y.d.mts → cli-errors-QH8kf-C2.d.mts} +0 -2
  4. package/dist/cli.mjs +12 -76
  5. package/dist/cli.mjs.map +1 -1
  6. package/dist/client-0ZX24FXF.mjs +1398 -0
  7. package/dist/client-0ZX24FXF.mjs.map +1 -0
  8. package/dist/commands/contract-emit.d.mts.map +1 -1
  9. package/dist/commands/contract-emit.mjs +2 -4
  10. package/dist/commands/contract-infer.d.mts.map +1 -1
  11. package/dist/commands/contract-infer.mjs +2 -4
  12. package/dist/commands/db-init.d.mts.map +1 -1
  13. package/dist/commands/db-init.mjs +11 -11
  14. package/dist/commands/db-init.mjs.map +1 -1
  15. package/dist/commands/db-schema.d.mts.map +1 -1
  16. package/dist/commands/db-schema.mjs +5 -7
  17. package/dist/commands/db-schema.mjs.map +1 -1
  18. package/dist/commands/db-sign.d.mts.map +1 -1
  19. package/dist/commands/db-sign.mjs +8 -9
  20. package/dist/commands/db-sign.mjs.map +1 -1
  21. package/dist/commands/db-update.d.mts.map +1 -1
  22. package/dist/commands/db-update.mjs +11 -11
  23. package/dist/commands/db-update.mjs.map +1 -1
  24. package/dist/commands/db-verify.d.mts.map +1 -1
  25. package/dist/commands/db-verify.mjs +1 -321
  26. package/dist/commands/migration-apply.d.mts.map +1 -1
  27. package/dist/commands/migration-apply.mjs +16 -17
  28. package/dist/commands/migration-apply.mjs.map +1 -1
  29. package/dist/commands/migration-new.d.mts +0 -1
  30. package/dist/commands/migration-new.d.mts.map +1 -1
  31. package/dist/commands/migration-new.mjs +10 -11
  32. package/dist/commands/migration-new.mjs.map +1 -1
  33. package/dist/commands/migration-plan.d.mts.map +1 -1
  34. package/dist/commands/migration-plan.mjs +1 -345
  35. package/dist/commands/migration-ref.d.mts +1 -1
  36. package/dist/commands/migration-ref.d.mts.map +1 -1
  37. package/dist/commands/migration-ref.mjs +5 -6
  38. package/dist/commands/migration-ref.mjs.map +1 -1
  39. package/dist/commands/migration-show.d.mts +1 -1
  40. package/dist/commands/migration-show.d.mts.map +1 -1
  41. package/dist/commands/migration-show.mjs +13 -13
  42. package/dist/commands/migration-show.mjs.map +1 -1
  43. package/dist/commands/migration-status.d.mts.map +1 -1
  44. package/dist/commands/migration-status.mjs +2 -4
  45. package/dist/{config-loader-ih8ViDb_.mjs → config-loader-B6sJjXTv.mjs} +2 -4
  46. package/dist/config-loader-B6sJjXTv.mjs.map +1 -0
  47. package/dist/config-loader.d.mts +0 -1
  48. package/dist/config-loader.d.mts.map +1 -1
  49. package/dist/config-loader.mjs +2 -3
  50. package/dist/{contract-emit-CnTXVVbF.mjs → contract-emit-B3ChISB_.mjs} +22 -13
  51. package/dist/contract-emit-B3ChISB_.mjs.map +1 -0
  52. package/dist/{contract-emit-CcZr3HS9.mjs → contract-emit-DkMqO7f2.mjs} +8 -10
  53. package/dist/contract-emit-DkMqO7f2.mjs.map +1 -0
  54. package/dist/{contract-enrichment-xDeJBC-o.mjs → contract-enrichment-CF6ogEJ_.mjs} +2 -2
  55. package/dist/contract-enrichment-CF6ogEJ_.mjs.map +1 -0
  56. package/dist/{contract-infer-sER84Le-.mjs → contract-infer-BDKAE0B0.mjs} +5 -7
  57. package/dist/{contract-infer-sER84Le-.mjs.map → contract-infer-BDKAE0B0.mjs.map} +1 -1
  58. package/dist/db-verify-B4TdDKOI.mjs +403 -0
  59. package/dist/db-verify-B4TdDKOI.mjs.map +1 -0
  60. package/dist/exports/config-types.mjs +1 -2
  61. package/dist/exports/control-api.d.mts +202 -7
  62. package/dist/exports/control-api.d.mts.map +1 -1
  63. package/dist/exports/control-api.mjs +4 -6
  64. package/dist/exports/index.d.mts.map +1 -1
  65. package/dist/exports/index.mjs +28 -30
  66. package/dist/exports/index.mjs.map +1 -1
  67. package/dist/exports/init-output.d.mts +2 -4
  68. package/dist/exports/init-output.d.mts.map +1 -1
  69. package/dist/exports/init-output.mjs +2 -3
  70. package/dist/{framework-components-Bgcre3Z6.mjs → framework-components-gwAHl7ml.mjs} +3 -4
  71. package/dist/{framework-components-Bgcre3Z6.mjs.map → framework-components-gwAHl7ml.mjs.map} +1 -1
  72. package/dist/{init-DC4sL4Rp.mjs → init-Deo7U8_U.mjs} +13 -30
  73. package/dist/init-Deo7U8_U.mjs.map +1 -0
  74. package/dist/{inspect-live-schema-BQN21nNO.mjs → inspect-live-schema-BAgQMYpD.mjs} +7 -8
  75. package/dist/inspect-live-schema-BAgQMYpD.mjs.map +1 -0
  76. package/dist/migration-cli.d.mts +0 -1
  77. package/dist/migration-cli.d.mts.map +1 -1
  78. package/dist/migration-cli.mjs +2 -3
  79. package/dist/migration-cli.mjs.map +1 -1
  80. package/dist/{migration-command-scaffold-DLmYGRug.mjs → migration-command-scaffold-B8J702Uh.mjs} +7 -8
  81. package/dist/migration-command-scaffold-B8J702Uh.mjs.map +1 -0
  82. package/dist/migration-plan-BcKNnTM7.mjs +530 -0
  83. package/dist/migration-plan-BcKNnTM7.mjs.map +1 -0
  84. package/dist/{migration-status-CDW4RDsO.mjs → migration-status-CjwB2of-.mjs} +10 -14
  85. package/dist/migration-status-CjwB2of-.mjs.map +1 -0
  86. package/dist/{migrations-MEoKMiV5.mjs → migrations-CIK94AJf.mjs} +3 -4
  87. package/dist/migrations-CIK94AJf.mjs.map +1 -0
  88. package/dist/{output-BpcQrnnq.mjs → output-DnjfCC_u.mjs} +9 -3
  89. package/dist/output-DnjfCC_u.mjs.map +1 -0
  90. package/dist/{progress-adapter-DgRGldpT.mjs → progress-adapter-xASh41wr.mjs} +2 -2
  91. package/dist/{progress-adapter-DgRGldpT.mjs.map → progress-adapter-xASh41wr.mjs.map} +1 -1
  92. package/dist/{result-handler-Ch6hVnOo.mjs → result-handler-DWb1rFS-.mjs} +20 -10
  93. package/dist/result-handler-DWb1rFS-.mjs.map +1 -0
  94. package/dist/{terminal-ui-u2YgKghu.mjs → terminal-ui-zaRDhJnP.mjs} +2 -6
  95. package/dist/{terminal-ui-u2YgKghu.mjs.map → terminal-ui-zaRDhJnP.mjs.map} +1 -1
  96. package/dist/{verify-BT9tgCOH.mjs → verify-BEIa9638.mjs} +3 -4
  97. package/dist/verify-BEIa9638.mjs.map +1 -0
  98. package/package.json +17 -17
  99. package/dist/client-hUCMXFE_.mjs +0 -1031
  100. package/dist/client-hUCMXFE_.mjs.map +0 -1
  101. package/dist/commands/db-verify.mjs.map +0 -1
  102. package/dist/commands/migration-plan.mjs.map +0 -1
  103. package/dist/config-loader-ih8ViDb_.mjs.map +0 -1
  104. package/dist/contract-emit-BkRH9lGt.mjs +0 -4
  105. package/dist/contract-emit-CcZr3HS9.mjs.map +0 -1
  106. package/dist/contract-emit-CnTXVVbF.mjs.map +0 -1
  107. package/dist/contract-enrichment-xDeJBC-o.mjs.map +0 -1
  108. package/dist/init-DC4sL4Rp.mjs.map +0 -1
  109. package/dist/inspect-live-schema-BQN21nNO.mjs.map +0 -1
  110. package/dist/migration-command-scaffold-DLmYGRug.mjs.map +0 -1
  111. package/dist/migration-status-CDW4RDsO.mjs.map +0 -1
  112. package/dist/migrations-MEoKMiV5.mjs.map +0 -1
  113. package/dist/output-BpcQrnnq.mjs.map +0 -1
  114. package/dist/result-handler-Ch6hVnOo.mjs.map +0 -1
  115. package/dist/verify-BT9tgCOH.mjs.map +0 -1
@@ -1,1031 +0,0 @@
1
- import { t as assertFrameworkComponentsCompatible } from "./framework-components-Bgcre3Z6.mjs";
2
- import { t as enrichContract } from "./contract-enrichment-xDeJBC-o.mjs";
3
- import { emit } from "@prisma-next/emitter";
4
- import { ifDefined } from "@prisma-next/utils/defined";
5
- import { notOk, ok } from "@prisma-next/utils/result";
6
- import { APP_SPACE_ID, createControlStack, hasMigrations, hasOperationPreview, hasPslContractInfer, hasSchemaView } from "@prisma-next/framework-components/control";
7
- import { EMPTY_CONTRACT_HASH } from "@prisma-next/migration-tools/constants";
8
-
9
- //#region src/control-api/errors.ts
10
- var ContractValidationError = class extends Error {
11
- cause;
12
- constructor(message, cause) {
13
- super(message);
14
- this.name = "ContractValidationError";
15
- this.cause = cause;
16
- }
17
- };
18
-
19
- //#endregion
20
- //#region src/control-api/operations/migration-helpers.ts
21
- /**
22
- * Strips operation objects to their public shape (id, label, operationClass).
23
- * Used at the API boundary to avoid leaking internal fields (precheck, execute, postcheck, etc.).
24
- */
25
- function stripOperations(operations) {
26
- return operations.map((op) => ({
27
- id: op.id,
28
- label: op.label,
29
- operationClass: op.operationClass
30
- }));
31
- }
32
- /**
33
- * Creates per-operation progress callbacks for the runner.
34
- * Returns undefined when no onProgress callback is provided.
35
- */
36
- function createOperationCallbacks(onProgress, action, parentSpanId) {
37
- if (!onProgress) return;
38
- return {
39
- onOperationStart: (op) => {
40
- onProgress({
41
- action,
42
- kind: "spanStart",
43
- spanId: `operation:${op.id}`,
44
- parentSpanId,
45
- label: op.label
46
- });
47
- },
48
- onOperationComplete: (op) => {
49
- onProgress({
50
- action,
51
- kind: "spanEnd",
52
- spanId: `operation:${op.id}`,
53
- outcome: "ok"
54
- });
55
- }
56
- };
57
- }
58
-
59
- //#endregion
60
- //#region src/control-api/operations/db-init.ts
61
- /**
62
- * Executes the dbInit operation.
63
- *
64
- * This is the core logic extracted from the CLI command, without any file I/O,
65
- * process.exit(), or console output. It uses the Result pattern to return
66
- * success or failure details.
67
- *
68
- * @param options - The options for executing dbInit
69
- * @returns Result with DbInitSuccess on success, DbInitFailure on failure
70
- */
71
- async function executeDbInit(options) {
72
- const { driver, familyInstance, contract, mode, migrations, frameworkComponents, onProgress } = options;
73
- const planner = migrations.createPlanner(familyInstance);
74
- const runner = migrations.createRunner(familyInstance);
75
- const introspectSpanId = "introspect";
76
- onProgress?.({
77
- action: "dbInit",
78
- kind: "spanStart",
79
- spanId: introspectSpanId,
80
- label: "Introspecting database schema"
81
- });
82
- const schemaIR = await familyInstance.introspect({ driver });
83
- onProgress?.({
84
- action: "dbInit",
85
- kind: "spanEnd",
86
- spanId: introspectSpanId,
87
- outcome: "ok"
88
- });
89
- const policy = { allowedOperationClasses: ["additive"] };
90
- const planSpanId = "plan";
91
- onProgress?.({
92
- action: "dbInit",
93
- kind: "spanStart",
94
- spanId: planSpanId,
95
- label: "Planning migration"
96
- });
97
- const plannerResult = await planner.plan({
98
- contract,
99
- schema: schemaIR,
100
- policy,
101
- fromContract: null,
102
- frameworkComponents,
103
- spaceId: APP_SPACE_ID
104
- });
105
- if (plannerResult.kind === "failure") {
106
- onProgress?.({
107
- action: "dbInit",
108
- kind: "spanEnd",
109
- spanId: planSpanId,
110
- outcome: "error"
111
- });
112
- return notOk({
113
- code: "PLANNING_FAILED",
114
- summary: "Migration planning failed due to conflicts",
115
- conflicts: plannerResult.conflicts,
116
- why: void 0,
117
- meta: void 0
118
- });
119
- }
120
- const migrationPlan = plannerResult.plan;
121
- onProgress?.({
122
- action: "dbInit",
123
- kind: "spanEnd",
124
- spanId: planSpanId,
125
- outcome: "ok"
126
- });
127
- const checkMarkerSpanId = "checkMarker";
128
- onProgress?.({
129
- action: "dbInit",
130
- kind: "spanStart",
131
- spanId: checkMarkerSpanId,
132
- label: "Checking database signature"
133
- });
134
- const existingMarker = await familyInstance.readMarker({
135
- driver,
136
- space: APP_SPACE_ID
137
- });
138
- if (existingMarker) {
139
- if (existingMarker.storageHash === migrationPlan.destination.storageHash && (!migrationPlan.destination.profileHash || existingMarker.profileHash === migrationPlan.destination.profileHash)) {
140
- onProgress?.({
141
- action: "dbInit",
142
- kind: "spanEnd",
143
- spanId: checkMarkerSpanId,
144
- outcome: "skipped"
145
- });
146
- return ok({
147
- mode,
148
- plan: { operations: [] },
149
- destination: {
150
- storageHash: migrationPlan.destination.storageHash,
151
- ...ifDefined("profileHash", migrationPlan.destination.profileHash)
152
- },
153
- ...ifDefined("execution", mode === "apply" ? {
154
- operationsPlanned: 0,
155
- operationsExecuted: 0
156
- } : void 0),
157
- ...ifDefined("marker", mode === "apply" ? {
158
- storageHash: existingMarker.storageHash,
159
- profileHash: existingMarker.profileHash
160
- } : void 0),
161
- summary: "Database already at target contract state"
162
- });
163
- }
164
- onProgress?.({
165
- action: "dbInit",
166
- kind: "spanEnd",
167
- spanId: checkMarkerSpanId,
168
- outcome: "error"
169
- });
170
- return notOk({
171
- code: "MARKER_ORIGIN_MISMATCH",
172
- summary: "Existing contract marker does not match plan destination",
173
- marker: {
174
- storageHash: existingMarker.storageHash,
175
- profileHash: existingMarker.profileHash
176
- },
177
- destination: {
178
- storageHash: migrationPlan.destination.storageHash,
179
- profileHash: migrationPlan.destination.profileHash
180
- },
181
- why: void 0,
182
- conflicts: void 0,
183
- meta: void 0
184
- });
185
- }
186
- onProgress?.({
187
- action: "dbInit",
188
- kind: "spanEnd",
189
- spanId: checkMarkerSpanId,
190
- outcome: "ok"
191
- });
192
- if (mode === "plan") {
193
- const preview = hasOperationPreview(familyInstance) ? familyInstance.toOperationPreview(migrationPlan.operations) : void 0;
194
- return ok({
195
- mode: "plan",
196
- plan: {
197
- operations: stripOperations(migrationPlan.operations),
198
- ...ifDefined("preview", preview)
199
- },
200
- destination: {
201
- storageHash: migrationPlan.destination.storageHash,
202
- ...ifDefined("profileHash", migrationPlan.destination.profileHash)
203
- },
204
- summary: `Planned ${migrationPlan.operations.length} operation(s)`
205
- });
206
- }
207
- const applySpanId = "apply";
208
- onProgress?.({
209
- action: "dbInit",
210
- kind: "spanStart",
211
- spanId: applySpanId,
212
- label: "Applying migration plan"
213
- });
214
- const callbacks = createOperationCallbacks(onProgress, "dbInit", applySpanId);
215
- const runnerResult = await runner.execute({
216
- plan: migrationPlan,
217
- driver,
218
- destinationContract: contract,
219
- policy,
220
- ...ifDefined("callbacks", callbacks),
221
- executionChecks: {
222
- prechecks: false,
223
- postchecks: false,
224
- idempotencyChecks: false
225
- },
226
- frameworkComponents
227
- });
228
- if (!runnerResult.ok) {
229
- onProgress?.({
230
- action: "dbInit",
231
- kind: "spanEnd",
232
- spanId: applySpanId,
233
- outcome: "error"
234
- });
235
- return notOk({
236
- code: "RUNNER_FAILED",
237
- summary: runnerResult.failure.summary,
238
- why: runnerResult.failure.why,
239
- meta: runnerResult.failure.meta,
240
- conflicts: void 0
241
- });
242
- }
243
- const execution = runnerResult.value;
244
- onProgress?.({
245
- action: "dbInit",
246
- kind: "spanEnd",
247
- spanId: applySpanId,
248
- outcome: "ok"
249
- });
250
- return ok({
251
- mode: "apply",
252
- plan: { operations: stripOperations(migrationPlan.operations) },
253
- destination: {
254
- storageHash: migrationPlan.destination.storageHash,
255
- ...ifDefined("profileHash", migrationPlan.destination.profileHash)
256
- },
257
- execution: {
258
- operationsPlanned: execution.operationsPlanned,
259
- operationsExecuted: execution.operationsExecuted
260
- },
261
- marker: migrationPlan.destination.profileHash ? {
262
- storageHash: migrationPlan.destination.storageHash,
263
- profileHash: migrationPlan.destination.profileHash
264
- } : { storageHash: migrationPlan.destination.storageHash },
265
- summary: `Applied ${execution.operationsExecuted} operation(s), database signed`
266
- });
267
- }
268
-
269
- //#endregion
270
- //#region src/control-api/operations/db-update.ts
271
- const DB_UPDATE_POLICY = { allowedOperationClasses: [
272
- "additive",
273
- "widening",
274
- "destructive"
275
- ] };
276
- /**
277
- * Executes the db update operation: introspect → plan → (optionally) apply → marker.
278
- *
279
- * db update is a pure reconciliation command: it introspects the live schema, plans the diff
280
- * to the destination contract, and applies operations. The marker is bookkeeping only — written
281
- * after apply so that `verify` and `db init` can reference it, but never read or validated
282
- * by db update itself. The runner creates the marker table if it does not exist.
283
- */
284
- async function executeDbUpdate(options) {
285
- const { driver, familyInstance, contract, mode, migrations, frameworkComponents, onProgress } = options;
286
- const planner = migrations.createPlanner(familyInstance);
287
- const runner = migrations.createRunner(familyInstance);
288
- const introspectSpanId = "introspect";
289
- onProgress?.({
290
- action: "dbUpdate",
291
- kind: "spanStart",
292
- spanId: introspectSpanId,
293
- label: "Introspecting database schema"
294
- });
295
- const schemaIR = await familyInstance.introspect({ driver });
296
- onProgress?.({
297
- action: "dbUpdate",
298
- kind: "spanEnd",
299
- spanId: introspectSpanId,
300
- outcome: "ok"
301
- });
302
- const policy = DB_UPDATE_POLICY;
303
- const planSpanId = "plan";
304
- onProgress?.({
305
- action: "dbUpdate",
306
- kind: "spanStart",
307
- spanId: planSpanId,
308
- label: "Planning migration"
309
- });
310
- const plannerResult = await planner.plan({
311
- contract,
312
- schema: schemaIR,
313
- policy,
314
- fromContract: null,
315
- frameworkComponents,
316
- spaceId: APP_SPACE_ID
317
- });
318
- if (plannerResult.kind === "failure") {
319
- onProgress?.({
320
- action: "dbUpdate",
321
- kind: "spanEnd",
322
- spanId: planSpanId,
323
- outcome: "error"
324
- });
325
- return notOk({
326
- code: "PLANNING_FAILED",
327
- summary: "Migration planning failed due to conflicts",
328
- conflicts: plannerResult.conflicts,
329
- why: void 0,
330
- meta: void 0
331
- });
332
- }
333
- onProgress?.({
334
- action: "dbUpdate",
335
- kind: "spanEnd",
336
- spanId: planSpanId,
337
- outcome: "ok"
338
- });
339
- const migrationPlan = plannerResult.plan;
340
- if (mode === "plan") {
341
- const preview = hasOperationPreview(familyInstance) ? familyInstance.toOperationPreview(migrationPlan.operations) : void 0;
342
- return ok({
343
- mode: "plan",
344
- plan: {
345
- operations: stripOperations(migrationPlan.operations),
346
- ...ifDefined("preview", preview)
347
- },
348
- destination: {
349
- storageHash: migrationPlan.destination.storageHash,
350
- ...ifDefined("profileHash", migrationPlan.destination.profileHash)
351
- },
352
- summary: `Planned ${migrationPlan.operations.length} operation(s)`
353
- });
354
- }
355
- if (!options.acceptDataLoss) {
356
- const destructiveOps = migrationPlan.operations.filter((op) => op.operationClass === "destructive").map((op) => ({
357
- id: op.id,
358
- label: op.label
359
- }));
360
- if (destructiveOps.length > 0) return notOk({
361
- code: "DESTRUCTIVE_CHANGES",
362
- summary: `Planned ${destructiveOps.length} destructive operation(s) that require confirmation`,
363
- why: "Destructive operations require confirmation — re-run with -y to accept",
364
- conflicts: void 0,
365
- meta: { destructiveOperations: destructiveOps }
366
- });
367
- }
368
- const applySpanId = "apply";
369
- onProgress?.({
370
- action: "dbUpdate",
371
- kind: "spanStart",
372
- spanId: applySpanId,
373
- label: "Applying migration plan"
374
- });
375
- const callbacks = createOperationCallbacks(onProgress, "dbUpdate", applySpanId);
376
- const runnerResult = await runner.execute({
377
- plan: migrationPlan,
378
- driver,
379
- destinationContract: contract,
380
- policy,
381
- ...callbacks ? { callbacks } : {},
382
- executionChecks: {
383
- prechecks: false,
384
- postchecks: false,
385
- idempotencyChecks: false
386
- },
387
- frameworkComponents
388
- });
389
- if (!runnerResult.ok) {
390
- onProgress?.({
391
- action: "dbUpdate",
392
- kind: "spanEnd",
393
- spanId: applySpanId,
394
- outcome: "error"
395
- });
396
- return notOk({
397
- code: "RUNNER_FAILED",
398
- summary: runnerResult.failure.summary,
399
- why: runnerResult.failure.why,
400
- meta: runnerResult.failure.meta,
401
- conflicts: void 0
402
- });
403
- }
404
- const execution = runnerResult.value;
405
- onProgress?.({
406
- action: "dbUpdate",
407
- kind: "spanEnd",
408
- spanId: applySpanId,
409
- outcome: "ok"
410
- });
411
- return ok({
412
- mode: "apply",
413
- plan: { operations: stripOperations(migrationPlan.operations) },
414
- destination: {
415
- storageHash: migrationPlan.destination.storageHash,
416
- ...ifDefined("profileHash", migrationPlan.destination.profileHash)
417
- },
418
- execution: {
419
- operationsPlanned: execution.operationsPlanned,
420
- operationsExecuted: execution.operationsExecuted
421
- },
422
- marker: migrationPlan.destination.profileHash ? {
423
- storageHash: migrationPlan.destination.storageHash,
424
- profileHash: migrationPlan.destination.profileHash
425
- } : { storageHash: migrationPlan.destination.storageHash },
426
- summary: execution.operationsExecuted === 0 ? "Database already matches contract, signature updated" : `Applied ${execution.operationsExecuted} operation(s), signature updated`
427
- });
428
- }
429
-
430
- //#endregion
431
- //#region src/control-api/operations/migration-apply.ts
432
- /**
433
- * Apply a sequence of migration packages against the configured driver.
434
- *
435
- * Validates the path's continuity (origin → ... → destination, no gaps),
436
- * then drives the family/target's migration runner over each package's
437
- * operations in order, surfacing per-migration progress through `onProgress`.
438
- *
439
- * The `pendingMigrations` parameter is trusted input. Callers are responsible
440
- * for upstream verification of the originating migration packages — typically
441
- * by loading them via `readMigrationPackage` from
442
- * `@prisma-next/migration-tools/io`, which performs hash-integrity checks at
443
- * the load boundary. This operation does not re-verify the packages and
444
- * assumes the `(metadata, ops)` pairs on disk have not been tampered with
445
- * since emit.
446
- */
447
- async function executeMigrationApply(options) {
448
- const { driver, familyInstance, originHash, destinationHash, pendingMigrations, migrations, frameworkComponents, targetId, onProgress } = options;
449
- if (pendingMigrations.length === 0) {
450
- if (originHash !== destinationHash) return notOk({
451
- code: "MIGRATION_PATH_NOT_FOUND",
452
- summary: "No migrations provided for requested origin and destination",
453
- why: `Requested ${originHash} -> ${destinationHash} but pendingMigrations is empty`,
454
- meta: {
455
- originHash,
456
- destinationHash
457
- }
458
- });
459
- return ok({
460
- migrationsApplied: 0,
461
- markerHash: originHash,
462
- applied: [],
463
- summary: "Already up to date"
464
- });
465
- }
466
- const firstMigration = pendingMigrations[0];
467
- const lastMigration = pendingMigrations[pendingMigrations.length - 1];
468
- const firstFromMarker = firstMigration.from ?? EMPTY_CONTRACT_HASH;
469
- if (firstFromMarker !== originHash || lastMigration.to !== destinationHash) return notOk({
470
- code: "MIGRATION_PATH_NOT_FOUND",
471
- summary: "Migration apply path does not match requested origin and destination",
472
- why: `Path resolved as ${firstFromMarker} -> ${lastMigration.to}, but requested ${originHash} -> ${destinationHash}`,
473
- meta: {
474
- originHash,
475
- destinationHash,
476
- pathOrigin: firstFromMarker,
477
- pathDestination: lastMigration.to
478
- }
479
- });
480
- for (let i = 1; i < pendingMigrations.length; i++) {
481
- const previous = pendingMigrations[i - 1];
482
- const current = pendingMigrations[i];
483
- const currentFromMarker = current.from ?? EMPTY_CONTRACT_HASH;
484
- if (previous.to !== currentFromMarker) return notOk({
485
- code: "MIGRATION_PATH_NOT_FOUND",
486
- summary: "Migration apply path contains a discontinuity between adjacent migrations",
487
- why: `Migration "${previous.dirName}" ends at ${previous.to}, but next migration "${current.dirName}" starts at ${currentFromMarker}`,
488
- meta: {
489
- originHash,
490
- destinationHash,
491
- previousDirName: previous.dirName,
492
- previousTo: previous.to,
493
- currentDirName: current.dirName,
494
- currentFrom: currentFromMarker,
495
- discontinuityIndex: i
496
- }
497
- });
498
- }
499
- const runner = migrations.createRunner(familyInstance);
500
- const applied = [];
501
- for (const migration of pendingMigrations) {
502
- const migrationSpanId = `migration:${migration.dirName}`;
503
- onProgress?.({
504
- action: "migrationApply",
505
- kind: "spanStart",
506
- spanId: migrationSpanId,
507
- label: `Applying ${migration.dirName}`
508
- });
509
- const { operations } = migration;
510
- const policy = { allowedOperationClasses: [
511
- "additive",
512
- "widening",
513
- "destructive",
514
- "data"
515
- ] };
516
- const plan = {
517
- targetId,
518
- spaceId: APP_SPACE_ID,
519
- origin: migration.from === null ? null : { storageHash: migration.from },
520
- destination: { storageHash: migration.to },
521
- operations,
522
- providedInvariants: migration.providedInvariants
523
- };
524
- const destinationContract = familyInstance.validateContract(migration.toContract);
525
- const runnerResult = await runner.execute({
526
- plan,
527
- driver,
528
- destinationContract,
529
- policy,
530
- executionChecks: {
531
- prechecks: true,
532
- postchecks: true,
533
- idempotencyChecks: true
534
- },
535
- frameworkComponents
536
- });
537
- if (!runnerResult.ok) {
538
- onProgress?.({
539
- action: "migrationApply",
540
- kind: "spanEnd",
541
- spanId: migrationSpanId,
542
- outcome: "error"
543
- });
544
- return notOk({
545
- code: "RUNNER_FAILED",
546
- summary: runnerResult.failure.summary,
547
- why: runnerResult.failure.why,
548
- meta: {
549
- migration: migration.dirName,
550
- from: migration.from,
551
- to: migration.to,
552
- ...runnerResult.failure.meta ?? {}
553
- }
554
- });
555
- }
556
- onProgress?.({
557
- action: "migrationApply",
558
- kind: "spanEnd",
559
- spanId: migrationSpanId,
560
- outcome: "ok"
561
- });
562
- applied.push({
563
- dirName: migration.dirName,
564
- from: migration.from,
565
- to: migration.to,
566
- operationsExecuted: runnerResult.value.operationsExecuted
567
- });
568
- }
569
- const finalHash = pendingMigrations[pendingMigrations.length - 1].to;
570
- const totalOps = applied.reduce((sum, a) => sum + a.operationsExecuted, 0);
571
- return ok({
572
- migrationsApplied: applied.length,
573
- markerHash: finalHash,
574
- applied,
575
- summary: `Applied ${applied.length} migration(s) (${totalOps} operation(s)), marker at ${finalHash}`
576
- });
577
- }
578
-
579
- //#endregion
580
- //#region src/control-api/client.ts
581
- /**
582
- * Creates a programmatic control client for Prisma Next operations.
583
- *
584
- * The client accepts framework component descriptors at creation time,
585
- * manages driver lifecycle via connect()/close(), and exposes domain
586
- * operations that delegate to the existing family instance methods.
587
- *
588
- * @see {@link ControlClient} for the client interface
589
- * @see README.md "Programmatic Control API" section for usage examples
590
- */
591
- function createControlClient(options) {
592
- return new ControlClientImpl(options);
593
- }
594
- /**
595
- * Implementation of ControlClient.
596
- * Manages initialization and connection state, delegates operations to family instance.
597
- */
598
- var ControlClientImpl = class {
599
- options;
600
- stack = null;
601
- driver = null;
602
- familyInstance = null;
603
- frameworkComponents = null;
604
- initialized = false;
605
- defaultConnection;
606
- constructor(options) {
607
- this.options = options;
608
- this.defaultConnection = options.connection;
609
- }
610
- init() {
611
- if (this.initialized) return;
612
- this.stack = createControlStack({
613
- family: this.options.family,
614
- target: this.options.target,
615
- adapter: this.options.adapter,
616
- driver: this.options.driver,
617
- extensionPacks: this.options.extensionPacks
618
- });
619
- this.familyInstance = this.options.family.create(this.stack);
620
- const rawComponents = [
621
- this.options.target,
622
- this.options.adapter,
623
- ...this.options.extensionPacks ?? []
624
- ];
625
- this.frameworkComponents = assertFrameworkComponentsCompatible(this.options.family.familyId, this.options.target.targetId, rawComponents);
626
- this.initialized = true;
627
- }
628
- async connect(connection) {
629
- this.init();
630
- if (this.driver) throw new Error("Already connected. Call close() before reconnecting.");
631
- const resolvedConnection = connection ?? this.defaultConnection;
632
- if (resolvedConnection === void 0) throw new Error("No connection provided. Pass a connection to connect() or provide a default connection when creating the client.");
633
- if (!this.stack?.driver) throw new Error("Driver is not configured. Pass a driver descriptor when creating the control client to enable database operations.");
634
- this.driver = await this.stack.driver.create(resolvedConnection);
635
- }
636
- async close() {
637
- if (this.driver) {
638
- await this.driver.close();
639
- this.driver = null;
640
- }
641
- }
642
- async ensureConnected() {
643
- this.init();
644
- if (!this.driver && this.defaultConnection !== void 0) await this.connect(this.defaultConnection);
645
- if (!this.driver || !this.familyInstance || !this.frameworkComponents) throw new Error("Not connected. Call connect(connection) first.");
646
- return {
647
- driver: this.driver,
648
- familyInstance: this.familyInstance,
649
- frameworkComponents: this.frameworkComponents
650
- };
651
- }
652
- async connectWithProgress(connection, action, onProgress) {
653
- if (connection === void 0) return;
654
- onProgress?.({
655
- action,
656
- kind: "spanStart",
657
- spanId: "connect",
658
- label: "Connecting to database..."
659
- });
660
- try {
661
- await this.connect(connection);
662
- onProgress?.({
663
- action,
664
- kind: "spanEnd",
665
- spanId: "connect",
666
- outcome: "ok"
667
- });
668
- } catch (error) {
669
- onProgress?.({
670
- action,
671
- kind: "spanEnd",
672
- spanId: "connect",
673
- outcome: "error"
674
- });
675
- throw error;
676
- }
677
- }
678
- async verify(options) {
679
- const { onProgress } = options;
680
- await this.connectWithProgress(options.connection, "verify", onProgress);
681
- const { driver, familyInstance } = await this.ensureConnected();
682
- let contract;
683
- try {
684
- contract = familyInstance.validateContract(options.contract);
685
- } catch (error) {
686
- throw new ContractValidationError(error instanceof Error ? error.message : String(error), error);
687
- }
688
- onProgress?.({
689
- action: "verify",
690
- kind: "spanStart",
691
- spanId: "verify",
692
- label: "Verifying database marker..."
693
- });
694
- try {
695
- const result = await familyInstance.verify({
696
- driver,
697
- contract,
698
- expectedTargetId: this.options.target.targetId,
699
- contractPath: ""
700
- });
701
- onProgress?.({
702
- action: "verify",
703
- kind: "spanEnd",
704
- spanId: "verify",
705
- outcome: result.ok ? "ok" : "error"
706
- });
707
- return result;
708
- } catch (error) {
709
- onProgress?.({
710
- action: "verify",
711
- kind: "spanEnd",
712
- spanId: "verify",
713
- outcome: "error"
714
- });
715
- throw error;
716
- }
717
- }
718
- async schemaVerify(options) {
719
- const { onProgress } = options;
720
- await this.connectWithProgress(options.connection, "schemaVerify", onProgress);
721
- const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
722
- let contract;
723
- try {
724
- contract = familyInstance.validateContract(options.contract);
725
- } catch (error) {
726
- throw new ContractValidationError(error instanceof Error ? error.message : String(error), error);
727
- }
728
- onProgress?.({
729
- action: "schemaVerify",
730
- kind: "spanStart",
731
- spanId: "schemaVerify",
732
- label: "Verifying database schema..."
733
- });
734
- try {
735
- const result = await familyInstance.schemaVerify({
736
- driver,
737
- contract,
738
- strict: options.strict ?? false,
739
- contractPath: "",
740
- frameworkComponents
741
- });
742
- onProgress?.({
743
- action: "schemaVerify",
744
- kind: "spanEnd",
745
- spanId: "schemaVerify",
746
- outcome: result.ok ? "ok" : "error"
747
- });
748
- return result;
749
- } catch (error) {
750
- onProgress?.({
751
- action: "schemaVerify",
752
- kind: "spanEnd",
753
- spanId: "schemaVerify",
754
- outcome: "error"
755
- });
756
- throw error;
757
- }
758
- }
759
- async sign(options) {
760
- const { onProgress } = options;
761
- await this.connectWithProgress(options.connection, "sign", onProgress);
762
- const { driver, familyInstance } = await this.ensureConnected();
763
- let contract;
764
- try {
765
- contract = familyInstance.validateContract(options.contract);
766
- } catch (error) {
767
- throw new ContractValidationError(error instanceof Error ? error.message : String(error), error);
768
- }
769
- onProgress?.({
770
- action: "sign",
771
- kind: "spanStart",
772
- spanId: "sign",
773
- label: "Signing database..."
774
- });
775
- try {
776
- const result = await familyInstance.sign({
777
- driver,
778
- contract,
779
- contractPath: options.contractPath ?? "",
780
- ...ifDefined("configPath", options.configPath)
781
- });
782
- onProgress?.({
783
- action: "sign",
784
- kind: "spanEnd",
785
- spanId: "sign",
786
- outcome: "ok"
787
- });
788
- return result;
789
- } catch (error) {
790
- onProgress?.({
791
- action: "sign",
792
- kind: "spanEnd",
793
- spanId: "sign",
794
- outcome: "error"
795
- });
796
- throw error;
797
- }
798
- }
799
- async dbInit(options) {
800
- const { onProgress } = options;
801
- await this.connectWithProgress(options.connection, "dbInit", onProgress);
802
- const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
803
- if (!hasMigrations(this.options.target)) throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
804
- let contract;
805
- try {
806
- contract = familyInstance.validateContract(options.contract);
807
- } catch (error) {
808
- throw new ContractValidationError(error instanceof Error ? error.message : String(error), error);
809
- }
810
- return executeDbInit({
811
- driver,
812
- familyInstance,
813
- contract,
814
- mode: options.mode,
815
- migrations: this.options.target.migrations,
816
- frameworkComponents,
817
- ...ifDefined("onProgress", onProgress)
818
- });
819
- }
820
- async dbUpdate(options) {
821
- const { onProgress } = options;
822
- await this.connectWithProgress(options.connection, "dbUpdate", onProgress);
823
- const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
824
- if (!hasMigrations(this.options.target)) throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
825
- let contract;
826
- try {
827
- contract = familyInstance.validateContract(options.contract);
828
- } catch (error) {
829
- throw new ContractValidationError(error instanceof Error ? error.message : String(error), error);
830
- }
831
- return executeDbUpdate({
832
- driver,
833
- familyInstance,
834
- contract,
835
- mode: options.mode,
836
- migrations: this.options.target.migrations,
837
- frameworkComponents,
838
- ...ifDefined("acceptDataLoss", options.acceptDataLoss),
839
- ...ifDefined("onProgress", onProgress)
840
- });
841
- }
842
- async readMarker() {
843
- const { driver, familyInstance } = await this.ensureConnected();
844
- return familyInstance.readMarker({
845
- driver,
846
- space: APP_SPACE_ID
847
- });
848
- }
849
- async migrationApply(options) {
850
- const { onProgress } = options;
851
- await this.connectWithProgress(options.connection, "migrationApply", onProgress);
852
- const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
853
- if (!hasMigrations(this.options.target)) throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
854
- return executeMigrationApply({
855
- driver,
856
- familyInstance,
857
- originHash: options.originHash,
858
- destinationHash: options.destinationHash,
859
- pendingMigrations: options.pendingMigrations,
860
- migrations: this.options.target.migrations,
861
- frameworkComponents,
862
- targetId: this.options.target.targetId,
863
- ...onProgress ? { onProgress } : {}
864
- });
865
- }
866
- async introspect(options) {
867
- const onProgress = options?.onProgress;
868
- await this.connectWithProgress(options?.connection, "introspect", onProgress);
869
- const { driver, familyInstance } = await this.ensureConnected();
870
- options?.schema;
871
- onProgress?.({
872
- action: "introspect",
873
- kind: "spanStart",
874
- spanId: "introspect",
875
- label: "Introspecting database schema..."
876
- });
877
- try {
878
- const result = await familyInstance.introspect({ driver });
879
- onProgress?.({
880
- action: "introspect",
881
- kind: "spanEnd",
882
- spanId: "introspect",
883
- outcome: "ok"
884
- });
885
- return result;
886
- } catch (error) {
887
- onProgress?.({
888
- action: "introspect",
889
- kind: "spanEnd",
890
- spanId: "introspect",
891
- outcome: "error"
892
- });
893
- throw error;
894
- }
895
- }
896
- toSchemaView(schemaIR) {
897
- this.init();
898
- if (this.familyInstance && hasSchemaView(this.familyInstance)) return this.familyInstance.toSchemaView(schemaIR);
899
- }
900
- inferPslContract(schemaIR) {
901
- this.init();
902
- if (this.familyInstance && hasPslContractInfer(this.familyInstance)) return this.familyInstance.inferPslContract(schemaIR);
903
- }
904
- toOperationPreview(operations) {
905
- this.init();
906
- if (this.familyInstance && hasOperationPreview(this.familyInstance)) return this.familyInstance.toOperationPreview(operations);
907
- }
908
- async emit(options) {
909
- const { onProgress, contractConfig } = options;
910
- this.init();
911
- if (!this.familyInstance) throw new Error("Family instance was not initialized. This is a bug.");
912
- let contractRaw;
913
- onProgress?.({
914
- action: "emit",
915
- kind: "spanStart",
916
- spanId: "resolveSource",
917
- label: "Resolving contract source..."
918
- });
919
- try {
920
- const stack = this.stack;
921
- const sourceContext = {
922
- composedExtensionPacks: stack.extensionPacks.map((p) => p.id),
923
- scalarTypeDescriptors: stack.scalarTypeDescriptors,
924
- authoringContributions: stack.authoringContributions,
925
- codecLookup: stack.codecLookup,
926
- controlMutationDefaults: stack.controlMutationDefaults,
927
- resolvedInputs: contractConfig.source.inputs ?? []
928
- };
929
- const providerResult = await contractConfig.source.load(sourceContext);
930
- if (!providerResult.ok) {
931
- onProgress?.({
932
- action: "emit",
933
- kind: "spanEnd",
934
- spanId: "resolveSource",
935
- outcome: "error"
936
- });
937
- return notOk({
938
- code: "CONTRACT_SOURCE_INVALID",
939
- summary: providerResult.failure.summary,
940
- why: providerResult.failure.summary,
941
- meta: providerResult.failure.meta,
942
- diagnostics: providerResult.failure
943
- });
944
- }
945
- contractRaw = providerResult.value;
946
- onProgress?.({
947
- action: "emit",
948
- kind: "spanEnd",
949
- spanId: "resolveSource",
950
- outcome: "ok"
951
- });
952
- } catch (error) {
953
- onProgress?.({
954
- action: "emit",
955
- kind: "spanEnd",
956
- spanId: "resolveSource",
957
- outcome: "error"
958
- });
959
- const message = error instanceof Error ? error.message : String(error);
960
- return notOk({
961
- code: "CONTRACT_SOURCE_INVALID",
962
- summary: "Failed to resolve contract source",
963
- why: message,
964
- diagnostics: {
965
- summary: "Contract source provider threw an exception",
966
- diagnostics: [{
967
- code: "PROVIDER_THROW",
968
- message
969
- }]
970
- },
971
- meta: void 0
972
- });
973
- }
974
- onProgress?.({
975
- action: "emit",
976
- kind: "spanStart",
977
- spanId: "emit",
978
- label: "Emitting contract..."
979
- });
980
- try {
981
- const enrichedIR = enrichContract(contractRaw, this.frameworkComponents ?? []);
982
- try {
983
- this.familyInstance.validateContract(enrichedIR);
984
- } catch (error) {
985
- onProgress?.({
986
- action: "emit",
987
- kind: "spanEnd",
988
- spanId: "emit",
989
- outcome: "error"
990
- });
991
- return notOk({
992
- code: "CONTRACT_VALIDATION_FAILED",
993
- summary: "Contract validation failed",
994
- why: error instanceof Error ? error.message : String(error),
995
- meta: void 0
996
- });
997
- }
998
- const result = await emit(enrichedIR, this.stack, this.options.family.emission);
999
- onProgress?.({
1000
- action: "emit",
1001
- kind: "spanEnd",
1002
- spanId: "emit",
1003
- outcome: "ok"
1004
- });
1005
- return ok({
1006
- storageHash: result.storageHash,
1007
- ...ifDefined("executionHash", result.executionHash),
1008
- profileHash: result.profileHash,
1009
- contractJson: result.contractJson,
1010
- contractDts: result.contractDts
1011
- });
1012
- } catch (error) {
1013
- onProgress?.({
1014
- action: "emit",
1015
- kind: "spanEnd",
1016
- spanId: "emit",
1017
- outcome: "error"
1018
- });
1019
- return notOk({
1020
- code: "EMIT_FAILED",
1021
- summary: "Failed to emit contract",
1022
- why: error instanceof Error ? error.message : String(error),
1023
- meta: void 0
1024
- });
1025
- }
1026
- }
1027
- };
1028
-
1029
- //#endregion
1030
- export { ContractValidationError as n, createControlClient as t };
1031
- //# sourceMappingURL=client-hUCMXFE_.mjs.map