@prisma-next/migration-tools 0.5.0-dev.8 → 0.5.0-dev.81

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 (130) hide show
  1. package/README.md +34 -22
  2. package/dist/{constants-BRi0X7B_.mjs → constants-DWV9_o2Z.mjs} +2 -2
  3. package/dist/{constants-BRi0X7B_.mjs.map → constants-DWV9_o2Z.mjs.map} +1 -1
  4. package/dist/errors-EPL_9p9f.mjs +297 -0
  5. package/dist/errors-EPL_9p9f.mjs.map +1 -0
  6. package/dist/exports/aggregate.d.mts +614 -0
  7. package/dist/exports/aggregate.d.mts.map +1 -0
  8. package/dist/exports/aggregate.mjs +611 -0
  9. package/dist/exports/aggregate.mjs.map +1 -0
  10. package/dist/exports/constants.d.mts.map +1 -1
  11. package/dist/exports/constants.mjs +2 -3
  12. package/dist/exports/errors.d.mts +68 -0
  13. package/dist/exports/errors.d.mts.map +1 -0
  14. package/dist/exports/errors.mjs +2 -0
  15. package/dist/exports/graph.d.mts +2 -0
  16. package/dist/exports/graph.mjs +1 -0
  17. package/dist/exports/hash.d.mts +52 -0
  18. package/dist/exports/hash.d.mts.map +1 -0
  19. package/dist/exports/hash.mjs +2 -0
  20. package/dist/exports/invariants.d.mts +34 -0
  21. package/dist/exports/invariants.d.mts.map +1 -0
  22. package/dist/exports/invariants.mjs +2 -0
  23. package/dist/exports/io.d.mts +66 -6
  24. package/dist/exports/io.d.mts.map +1 -1
  25. package/dist/exports/io.mjs +2 -3
  26. package/dist/exports/metadata.d.mts +2 -0
  27. package/dist/exports/metadata.mjs +1 -0
  28. package/dist/exports/migration-graph.d.mts +2 -0
  29. package/dist/exports/migration-graph.mjs +2 -0
  30. package/dist/exports/migration-ts.d.mts.map +1 -1
  31. package/dist/exports/migration-ts.mjs +2 -4
  32. package/dist/exports/migration-ts.mjs.map +1 -1
  33. package/dist/exports/migration.d.mts +15 -14
  34. package/dist/exports/migration.d.mts.map +1 -1
  35. package/dist/exports/migration.mjs +70 -43
  36. package/dist/exports/migration.mjs.map +1 -1
  37. package/dist/exports/package.d.mts +3 -0
  38. package/dist/exports/package.mjs +1 -0
  39. package/dist/exports/refs.d.mts.map +1 -1
  40. package/dist/exports/refs.mjs +3 -4
  41. package/dist/exports/refs.mjs.map +1 -1
  42. package/dist/exports/spaces.d.mts +550 -0
  43. package/dist/exports/spaces.d.mts.map +1 -0
  44. package/dist/exports/spaces.mjs +223 -0
  45. package/dist/exports/spaces.mjs.map +1 -0
  46. package/dist/graph-HMWAldoR.d.mts +28 -0
  47. package/dist/graph-HMWAldoR.d.mts.map +1 -0
  48. package/dist/hash-By50zM_E.mjs +74 -0
  49. package/dist/hash-By50zM_E.mjs.map +1 -0
  50. package/dist/invariants-Duc8f9NM.mjs +52 -0
  51. package/dist/invariants-Duc8f9NM.mjs.map +1 -0
  52. package/dist/io-D13dLvUh.mjs +239 -0
  53. package/dist/io-D13dLvUh.mjs.map +1 -0
  54. package/dist/metadata-CFvm3ayn.d.mts +2 -0
  55. package/dist/migration-graph-DGNnKDY5.mjs +523 -0
  56. package/dist/migration-graph-DGNnKDY5.mjs.map +1 -0
  57. package/dist/migration-graph-DulOITvG.d.mts +124 -0
  58. package/dist/migration-graph-DulOITvG.d.mts.map +1 -0
  59. package/dist/op-schema-D5qkXfEf.mjs +13 -0
  60. package/dist/op-schema-D5qkXfEf.mjs.map +1 -0
  61. package/dist/package-BjiZ7KDy.d.mts +21 -0
  62. package/dist/package-BjiZ7KDy.d.mts.map +1 -0
  63. package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
  64. package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
  65. package/package.json +42 -17
  66. package/src/aggregate/loader.ts +409 -0
  67. package/src/aggregate/marker-types.ts +16 -0
  68. package/src/aggregate/planner-types.ts +171 -0
  69. package/src/aggregate/planner.ts +158 -0
  70. package/src/aggregate/project-schema-to-space.ts +64 -0
  71. package/src/aggregate/strategies/graph-walk.ts +118 -0
  72. package/src/aggregate/strategies/synth.ts +122 -0
  73. package/src/aggregate/types.ts +89 -0
  74. package/src/aggregate/verifier.ts +230 -0
  75. package/src/assert-descriptor-self-consistency.ts +70 -0
  76. package/src/compute-extension-space-apply-path.ts +152 -0
  77. package/src/concatenate-space-apply-inputs.ts +90 -0
  78. package/src/detect-space-contract-drift.ts +91 -0
  79. package/src/emit-contract-space-artefacts.ts +70 -0
  80. package/src/errors.ts +251 -17
  81. package/src/exports/aggregate.ts +42 -0
  82. package/src/exports/errors.ts +8 -0
  83. package/src/exports/graph.ts +1 -0
  84. package/src/exports/hash.ts +2 -0
  85. package/src/exports/invariants.ts +1 -0
  86. package/src/exports/io.ts +3 -1
  87. package/src/exports/metadata.ts +1 -0
  88. package/src/exports/{dag.ts → migration-graph.ts} +3 -2
  89. package/src/exports/migration.ts +0 -1
  90. package/src/exports/package.ts +2 -0
  91. package/src/exports/spaces.ts +49 -0
  92. package/src/gather-disk-contract-space-state.ts +62 -0
  93. package/src/graph-ops.ts +57 -30
  94. package/src/graph.ts +25 -0
  95. package/src/hash.ts +91 -0
  96. package/src/invariants.ts +56 -0
  97. package/src/io.ts +163 -40
  98. package/src/metadata.ts +1 -0
  99. package/src/migration-base.ts +97 -56
  100. package/src/migration-graph.ts +676 -0
  101. package/src/op-schema.ts +11 -0
  102. package/src/package.ts +21 -0
  103. package/src/plan-all-spaces.ts +76 -0
  104. package/src/read-contract-space-contract.ts +44 -0
  105. package/src/read-contract-space-head-ref.ts +63 -0
  106. package/src/space-layout.ts +48 -0
  107. package/src/verify-contract-spaces.ts +272 -0
  108. package/dist/attestation-BnzTb0Qp.mjs +0 -65
  109. package/dist/attestation-BnzTb0Qp.mjs.map +0 -1
  110. package/dist/errors-BmiSgz1j.mjs +0 -160
  111. package/dist/errors-BmiSgz1j.mjs.map +0 -1
  112. package/dist/exports/attestation.d.mts +0 -37
  113. package/dist/exports/attestation.d.mts.map +0 -1
  114. package/dist/exports/attestation.mjs +0 -4
  115. package/dist/exports/dag.d.mts +0 -51
  116. package/dist/exports/dag.d.mts.map +0 -1
  117. package/dist/exports/dag.mjs +0 -386
  118. package/dist/exports/dag.mjs.map +0 -1
  119. package/dist/exports/types.d.mts +0 -35
  120. package/dist/exports/types.d.mts.map +0 -1
  121. package/dist/exports/types.mjs +0 -3
  122. package/dist/io-Cd6GLyjK.mjs +0 -153
  123. package/dist/io-Cd6GLyjK.mjs.map +0 -1
  124. package/dist/types-DyGXcWWp.d.mts +0 -71
  125. package/dist/types-DyGXcWWp.d.mts.map +0 -1
  126. package/src/attestation.ts +0 -81
  127. package/src/dag.ts +0 -426
  128. package/src/exports/attestation.ts +0 -2
  129. package/src/exports/types.ts +0 -10
  130. package/src/types.ts +0 -66
@@ -8,20 +8,27 @@ import type {
8
8
  } from '@prisma-next/framework-components/control';
9
9
  import { ifDefined } from '@prisma-next/utils/defined';
10
10
  import { type } from 'arktype';
11
- import { computeMigrationId } from './attestation';
12
- import type { MigrationHints, MigrationManifest, MigrationOps } from './types';
11
+ import { errorInvalidOperationEntry, errorStaleContractBookends } from './errors';
12
+ import { computeMigrationHash } from './hash';
13
+ import { deriveProvidedInvariants } from './invariants';
14
+ import type { MigrationHints, MigrationMetadata } from './metadata';
15
+ import { MigrationOpSchema } from './op-schema';
16
+ import type { MigrationOps } from './package';
13
17
 
14
18
  export interface MigrationMeta {
15
- readonly from: string;
19
+ readonly from: string | null;
16
20
  readonly to: string;
17
- readonly kind?: 'regular' | 'baseline';
18
21
  readonly labels?: readonly string[];
19
22
  }
20
23
 
24
+ // `from` rejects empty strings to mirror `MigrationMetadataSchema` in
25
+ // `./io.ts`. Without this match, an authored migration could `describe()` with
26
+ // `from: ''` and pass `buildMigrationArtifacts`'s validation, only to have
27
+ // `readMigrationPackage` reject the resulting `migration.json` later — the
28
+ // two validators must agree on the legal value space.
21
29
  const MigrationMetaSchema = type({
22
- from: 'string',
30
+ from: 'string > 0 | null',
23
31
  to: 'string',
24
- 'kind?': "'regular' | 'baseline'",
25
32
  'labels?': type('string').array(),
26
33
  });
27
34
 
@@ -30,7 +37,7 @@ const MigrationMetaSchema = type({
30
37
  *
31
38
  * A `Migration` subclass is itself a `MigrationPlan`: CLI commands and the
32
39
  * runner can consume it directly via `targetId`, `operations`, `origin`, and
33
- * `destination`. The manifest-shaped inputs come from `describe()`, which
40
+ * `destination`. The metadata-shaped inputs come from `describe()`, which
34
41
  * every migration must implement — `migration.json` is required for a
35
42
  * migration to be valid.
36
43
  */
@@ -74,11 +81,7 @@ export abstract class Migration<
74
81
 
75
82
  get origin(): { readonly storageHash: string } | null {
76
83
  const from = this.describe().from;
77
- // An empty `from` represents a migration with no prior origin (e.g.
78
- // initial baseline, or an in-process plan that was never persisted).
79
- // Surface that as a null origin so runners treat the plan as
80
- // origin-less rather than matching against an empty storage hash.
81
- return from === '' ? null : { storageHash: from };
84
+ return from === null ? null : { storageHash: from };
82
85
  }
83
86
 
84
87
  get destination(): { readonly storageHash: string } {
@@ -104,83 +107,114 @@ export function isDirectEntrypoint(importMetaUrl: string): boolean {
104
107
  }
105
108
  }
106
109
 
107
- export function printMigrationHelp(): void {
108
- printHelp();
109
- }
110
-
111
- function printHelp(): void {
112
- process.stdout.write(
113
- [
114
- 'Usage: node <migration-file> [options]',
115
- '',
116
- 'Options:',
117
- ' --dry-run Print operations to stdout without writing files',
118
- ' --help Show this help message',
119
- '',
120
- ].join('\n'),
121
- );
122
- }
123
-
124
110
  /**
125
111
  * In-memory artifacts produced from a `Migration` instance: the
126
- * serialized `ops.json` body, the `migration.json` manifest object, and
112
+ * serialized `ops.json` body, the `migration.json` metadata object, and
127
113
  * its serialized form. Returned by `buildMigrationArtifacts` so callers
128
114
  * (today: `MigrationCLI.run` in `@prisma-next/cli/migration-cli`) can
129
115
  * decide how to persist them — write to disk, print in dry-run, ship
130
116
  * over the wire — without coupling artifact construction to file I/O.
117
+ *
118
+ * `metadataJson` is `JSON.stringify(metadata, null, 2)` — the canonical
119
+ * on-disk shape that the arktype loader-schema in `./io` validates.
131
120
  */
132
121
  export interface MigrationArtifacts {
133
122
  readonly opsJson: string;
134
- readonly manifest: MigrationManifest;
135
- readonly manifestJson: string;
123
+ readonly metadata: MigrationMetadata;
124
+ readonly metadataJson: string;
136
125
  }
137
126
 
138
127
  /**
139
- * Build the attested manifest from `describe()`-derived metadata, the
140
- * operations list, and the previously-scaffolded manifest (if any).
128
+ * Build the attested metadata from `describe()`-derived metadata, the
129
+ * operations list, and the previously-scaffolded metadata (if any).
141
130
  *
142
131
  * When a `migration.json` already exists for this package (the common
143
132
  * case: it was scaffolded by `migration plan`), preserve the contract
144
133
  * bookends, hints, labels, and `createdAt` set there — those fields are
145
134
  * owned by the CLI scaffolder, not the authored class. Only the
146
- * `describe()`-derived fields (`from`, `to`, `kind`) and the operations
147
- * change as the author iterates. When no manifest exists yet (a bare
135
+ * `describe()`-derived fields (`from`, `to`) and the operations
136
+ * change as the author iterates. When no metadata exists yet (a bare
148
137
  * `migration.ts` run from scratch), synthesize a minimal but
149
- * schema-conformant manifest so the resulting package can still be read,
138
+ * schema-conformant record so the resulting package can still be read,
150
139
  * verified, and applied.
151
140
  *
152
- * The `migrationId` is recomputed against the current manifest + ops so
141
+ * The `migrationHash` is recomputed against the current metadata + ops so
153
142
  * the on-disk artifacts are always fully attested.
154
143
  */
155
- function buildAttestedManifest(
144
+ function buildAttestedMetadata(
156
145
  meta: MigrationMeta,
157
146
  ops: MigrationOps,
158
- existing: Partial<MigrationManifest> | null,
159
- ): MigrationManifest {
160
- const baseManifest: Omit<MigrationManifest, 'migrationId'> = {
147
+ existing: Partial<MigrationMetadata> | null,
148
+ ): MigrationMetadata {
149
+ assertBookendsMatchMeta(meta, existing);
150
+
151
+ const baseMetadata: Omit<MigrationMetadata, 'migrationHash'> = {
161
152
  from: meta.from,
162
153
  to: meta.to,
163
- kind: meta.kind ?? 'regular',
164
154
  labels: meta.labels ?? existing?.labels ?? [],
155
+ providedInvariants: deriveProvidedInvariants(ops),
165
156
  createdAt: existing?.createdAt ?? new Date().toISOString(),
166
157
  fromContract: existing?.fromContract ?? null,
167
- // When no scaffolded manifest exists we synthesize a minimal contract
158
+ // When no scaffolded metadata exists we synthesize a minimal contract
168
159
  // stub so the package is still readable end-to-end. The cast is
169
160
  // intentional: only the storage bookend matters for hash computation
170
- // (everything else is stripped by `computeMigrationId`), and a real
161
+ // (everything else is stripped by `computeMigrationHash`), and a real
171
162
  // contract bookend would only be available after `migration plan`.
172
163
  toContract: existing?.toContract ?? ({ storage: { storageHash: meta.to } } as Contract),
173
164
  hints: normalizeHints(existing?.hints),
174
165
  ...ifDefined('authorship', existing?.authorship),
175
166
  };
176
167
 
177
- const migrationId = computeMigrationId(baseManifest, ops);
178
- return { ...baseManifest, migrationId };
168
+ const migrationHash = computeMigrationHash(baseMetadata, ops);
169
+ return { ...baseMetadata, migrationHash };
170
+ }
171
+
172
+ /**
173
+ * Verify each preserved contract bookend in `existing` agrees with the
174
+ * corresponding side of `describe()`'s output. A mismatch indicates the
175
+ * migration's `describe()` was edited after `migration plan` scaffolded
176
+ * the package, leaving a self-inconsistent manifest. Failing fast at
177
+ * write-time turns a silent foot-gun into an actionable diagnostic.
178
+ *
179
+ * Skipped when a side's `existing.<side>Contract` is null/absent (the
180
+ * synthesis path stays open for origin-less initial migrations and for
181
+ * bare `migration.ts` runs from scratch). When a bookend is *present*
182
+ * but its `storage.storageHash` is missing, that's treated as a
183
+ * mismatch — a malformed bookend is not equivalent to "no bookend".
184
+ *
185
+ * This check is paired with TML-2274, which removes `fromContract` /
186
+ * `toContract` from the manifest entirely; once that lands, this
187
+ * function and its error code are deleted.
188
+ */
189
+ function assertBookendsMatchMeta(
190
+ meta: MigrationMeta,
191
+ existing: Partial<MigrationMetadata> | null,
192
+ ): void {
193
+ if (existing?.fromContract != null) {
194
+ const contractHash = existing.fromContract.storage?.storageHash ?? '';
195
+ if (contractHash !== meta.from) {
196
+ throw errorStaleContractBookends({
197
+ side: 'from',
198
+ metaHash: meta.from,
199
+ contractHash,
200
+ });
201
+ }
202
+ }
203
+ if (existing?.toContract != null) {
204
+ const contractHash = existing.toContract.storage?.storageHash ?? '';
205
+ if (contractHash !== meta.to) {
206
+ throw errorStaleContractBookends({
207
+ side: 'to',
208
+ metaHash: meta.to,
209
+ contractHash,
210
+ });
211
+ }
212
+ }
179
213
  }
180
214
 
181
215
  /**
182
216
  * Project `existing.hints` down to the known `MigrationHints` shape, dropping
183
- * any legacy keys that may linger in manifests scaffolded by older CLI
217
+ * any legacy keys that may linger in metadata scaffolded by older CLI
184
218
  * versions (e.g. `planningStrategy`). Picking fields explicitly instead of
185
219
  * spreading keeps refreshed `migration.json` files schema-clean regardless
186
220
  * of what was on disk before.
@@ -195,33 +229,40 @@ function normalizeHints(existing: MigrationHints | undefined): MigrationHints {
195
229
 
196
230
  /**
197
231
  * Pure conversion from a `Migration` instance (plus the previously
198
- * scaffolded manifest, when one exists on disk) to the in-memory
232
+ * scaffolded metadata, when one exists on disk) to the in-memory
199
233
  * artifacts that downstream tooling persists. Owns metadata validation,
200
- * manifest synthesis/preservation, hint normalization, and the
201
- * content-addressed `migrationId` computation, but performs no file I/O
234
+ * metadata synthesis/preservation, hint normalization, and the
235
+ * content-addressed `migrationHash` computation, but performs no file I/O
202
236
  * — callers handle reads (to source `existing`) and writes (to persist
203
- * `opsJson` / `manifestJson`).
237
+ * `opsJson` / `metadataJson`).
204
238
  */
205
239
  export function buildMigrationArtifacts(
206
240
  instance: Migration,
207
- existing: Partial<MigrationManifest> | null,
241
+ existing: Partial<MigrationMetadata> | null,
208
242
  ): MigrationArtifacts {
209
243
  const ops = instance.operations;
210
244
  if (!Array.isArray(ops)) {
211
245
  throw new Error('operations must be an array');
212
246
  }
213
247
 
248
+ for (let index = 0; index < ops.length; index++) {
249
+ const result = MigrationOpSchema(ops[index]);
250
+ if (result instanceof type.errors) {
251
+ throw errorInvalidOperationEntry(index, result.summary);
252
+ }
253
+ }
254
+
214
255
  const rawMeta: unknown = instance.describe();
215
256
  const parsed = MigrationMetaSchema(rawMeta);
216
257
  if (parsed instanceof type.errors) {
217
258
  throw new Error(`describe() returned invalid metadata: ${parsed.summary}`);
218
259
  }
219
260
 
220
- const manifest = buildAttestedManifest(parsed, ops, existing);
261
+ const metadata = buildAttestedMetadata(parsed, ops, existing);
221
262
 
222
263
  return {
223
264
  opsJson: JSON.stringify(ops, null, 2),
224
- manifest,
225
- manifestJson: JSON.stringify(manifest, null, 2),
265
+ metadata,
266
+ metadataJson: JSON.stringify(metadata, null, 2),
226
267
  };
227
268
  }