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