@prisma-next/migration-tools 0.5.0-dev.9 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -22
- package/dist/{constants-BRi0X7B_.mjs → constants-DWV9_o2Z.mjs} +2 -2
- package/dist/{constants-BRi0X7B_.mjs.map → constants-DWV9_o2Z.mjs.map} +1 -1
- package/dist/errors-EPL_9p9f.mjs +297 -0
- package/dist/errors-EPL_9p9f.mjs.map +1 -0
- package/dist/exports/aggregate.d.mts +599 -0
- package/dist/exports/aggregate.d.mts.map +1 -0
- package/dist/exports/aggregate.mjs +599 -0
- package/dist/exports/aggregate.mjs.map +1 -0
- package/dist/exports/constants.d.mts.map +1 -1
- package/dist/exports/constants.mjs +2 -3
- package/dist/exports/errors.d.mts +68 -0
- package/dist/exports/errors.d.mts.map +1 -0
- package/dist/exports/errors.mjs +2 -0
- package/dist/exports/graph.d.mts +2 -0
- package/dist/exports/graph.mjs +1 -0
- package/dist/exports/hash.d.mts +52 -0
- package/dist/exports/hash.d.mts.map +1 -0
- package/dist/exports/hash.mjs +2 -0
- package/dist/exports/invariants.d.mts +39 -0
- package/dist/exports/invariants.d.mts.map +1 -0
- package/dist/exports/invariants.mjs +2 -0
- package/dist/exports/io.d.mts +66 -6
- package/dist/exports/io.d.mts.map +1 -1
- package/dist/exports/io.mjs +2 -3
- package/dist/exports/metadata.d.mts +2 -0
- package/dist/exports/metadata.mjs +1 -0
- package/dist/exports/migration-graph.d.mts +2 -0
- package/dist/exports/migration-graph.mjs +2 -0
- package/dist/exports/migration-ts.d.mts.map +1 -1
- package/dist/exports/migration-ts.mjs +2 -4
- package/dist/exports/migration-ts.mjs.map +1 -1
- package/dist/exports/migration.d.mts +15 -14
- package/dist/exports/migration.d.mts.map +1 -1
- package/dist/exports/migration.mjs +70 -43
- package/dist/exports/migration.mjs.map +1 -1
- package/dist/exports/package.d.mts +3 -0
- package/dist/exports/package.mjs +1 -0
- package/dist/exports/refs.d.mts.map +1 -1
- package/dist/exports/refs.mjs +3 -4
- package/dist/exports/refs.mjs.map +1 -1
- package/dist/exports/spaces.d.mts +526 -0
- package/dist/exports/spaces.d.mts.map +1 -0
- package/dist/exports/spaces.mjs +266 -0
- package/dist/exports/spaces.mjs.map +1 -0
- package/dist/graph-HMWAldoR.d.mts +28 -0
- package/dist/graph-HMWAldoR.d.mts.map +1 -0
- package/dist/hash-By50zM_E.mjs +74 -0
- package/dist/hash-By50zM_E.mjs.map +1 -0
- package/dist/invariants-qgQGlsrV.mjs +57 -0
- package/dist/invariants-qgQGlsrV.mjs.map +1 -0
- package/dist/io-D5YYptRO.mjs +239 -0
- package/dist/io-D5YYptRO.mjs.map +1 -0
- package/dist/metadata-CFvm3ayn.d.mts +2 -0
- package/dist/migration-graph-DGNnKDY5.mjs +523 -0
- package/dist/migration-graph-DGNnKDY5.mjs.map +1 -0
- package/dist/migration-graph-DulOITvG.d.mts +124 -0
- package/dist/migration-graph-DulOITvG.d.mts.map +1 -0
- package/dist/op-schema-D5qkXfEf.mjs +13 -0
- package/dist/op-schema-D5qkXfEf.mjs.map +1 -0
- package/dist/package-BjiZ7KDy.d.mts +21 -0
- package/dist/package-BjiZ7KDy.d.mts.map +1 -0
- package/dist/read-contract-space-contract-Cme8KZk_.mjs +259 -0
- package/dist/read-contract-space-contract-Cme8KZk_.mjs.map +1 -0
- package/package.json +42 -17
- package/src/aggregate/loader.ts +379 -0
- package/src/aggregate/marker-types.ts +16 -0
- package/src/aggregate/planner-types.ts +171 -0
- package/src/aggregate/planner.ts +159 -0
- package/src/aggregate/project-schema-to-space.ts +64 -0
- package/src/aggregate/strategies/graph-walk.ts +118 -0
- package/src/aggregate/strategies/synth.ts +122 -0
- package/src/aggregate/types.ts +89 -0
- package/src/aggregate/verifier.ts +230 -0
- package/src/assert-descriptor-self-consistency.ts +70 -0
- package/src/compute-extension-space-apply-path.ts +152 -0
- package/src/concatenate-space-apply-inputs.ts +90 -0
- package/src/contract-space-from-json.ts +63 -0
- package/src/emit-contract-space-artefacts.ts +70 -0
- package/src/errors.ts +251 -17
- package/src/exports/aggregate.ts +42 -0
- package/src/exports/errors.ts +8 -0
- package/src/exports/graph.ts +1 -0
- package/src/exports/hash.ts +2 -0
- package/src/exports/invariants.ts +1 -0
- package/src/exports/io.ts +3 -1
- package/src/exports/metadata.ts +1 -0
- package/src/exports/{dag.ts → migration-graph.ts} +3 -2
- package/src/exports/migration.ts +0 -1
- package/src/exports/package.ts +2 -0
- package/src/exports/spaces.ts +45 -0
- package/src/gather-disk-contract-space-state.ts +62 -0
- package/src/graph-ops.ts +57 -30
- package/src/graph.ts +25 -0
- package/src/hash.ts +91 -0
- package/src/invariants.ts +61 -0
- package/src/io.ts +163 -40
- package/src/metadata.ts +1 -0
- package/src/migration-base.ts +97 -56
- package/src/migration-graph.ts +676 -0
- package/src/op-schema.ts +11 -0
- package/src/package.ts +21 -0
- package/src/plan-all-spaces.ts +76 -0
- package/src/read-contract-space-contract.ts +44 -0
- package/src/read-contract-space-head-ref.ts +63 -0
- package/src/space-layout.ts +48 -0
- package/src/verify-contract-spaces.ts +272 -0
- package/dist/attestation-BnzTb0Qp.mjs +0 -65
- package/dist/attestation-BnzTb0Qp.mjs.map +0 -1
- package/dist/errors-BmiSgz1j.mjs +0 -160
- package/dist/errors-BmiSgz1j.mjs.map +0 -1
- package/dist/exports/attestation.d.mts +0 -37
- package/dist/exports/attestation.d.mts.map +0 -1
- package/dist/exports/attestation.mjs +0 -4
- package/dist/exports/dag.d.mts +0 -51
- package/dist/exports/dag.d.mts.map +0 -1
- package/dist/exports/dag.mjs +0 -386
- package/dist/exports/dag.mjs.map +0 -1
- package/dist/exports/types.d.mts +0 -35
- package/dist/exports/types.d.mts.map +0 -1
- package/dist/exports/types.mjs +0 -3
- package/dist/io-Cd6GLyjK.mjs +0 -153
- package/dist/io-Cd6GLyjK.mjs.map +0 -1
- package/dist/types-DyGXcWWp.d.mts +0 -71
- package/dist/types-DyGXcWWp.d.mts.map +0 -1
- package/src/attestation.ts +0 -81
- package/src/dag.ts +0 -426
- package/src/exports/attestation.ts +0 -2
- package/src/exports/types.ts +0 -10
- package/src/types.ts +0 -66
package/src/errors.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
2
|
+
import { basename, dirname, relative } from 'pathe';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build the canonical "re-emit this package" remediation hint.
|
|
6
|
+
*
|
|
7
|
+
* Every on-disk migration package ships its own `migration.ts` author-time
|
|
8
|
+
* file. Running it regenerates `migration.json` and `ops.json` with the
|
|
9
|
+
* correct hash + metadata, so it is the right primitive whenever a single
|
|
10
|
+
* package's on-disk artifacts are missing, malformed, or otherwise corrupt.
|
|
11
|
+
* Pointing users at `migration plan` would emit a *new* package rather than
|
|
12
|
+
* heal the broken one.
|
|
13
|
+
*/
|
|
14
|
+
function reemitHint(dir: string, fallback?: string): string {
|
|
15
|
+
const relativeDir = relative(process.cwd(), dir);
|
|
16
|
+
const reemit = `Re-emit the package by running \`node "${relativeDir}/migration.ts"\``;
|
|
17
|
+
return fallback ? `${reemit}, ${fallback}` : `${reemit}.`;
|
|
18
|
+
}
|
|
19
|
+
|
|
1
20
|
/**
|
|
2
21
|
* Structured error for migration tooling operations.
|
|
3
22
|
*
|
|
4
23
|
* Follows the NAMESPACE.SUBCODE convention from ADR 027. All codes live under
|
|
5
|
-
* the MIGRATION namespace. These are tooling-time errors (file I/O,
|
|
6
|
-
* migration history reconstruction), distinct from the runtime
|
|
7
|
-
* failures (PRECHECK_FAILED, POSTCHECK_FAILED,
|
|
24
|
+
* the MIGRATION namespace. These are tooling-time errors (file I/O, hash
|
|
25
|
+
* verification, migration history reconstruction), distinct from the runtime
|
|
26
|
+
* MIGRATION.* codes for apply-time failures (PRECHECK_FAILED, POSTCHECK_FAILED,
|
|
27
|
+
* etc.).
|
|
8
28
|
*
|
|
9
29
|
* Fields:
|
|
10
30
|
* - code: Stable machine-readable code (MIGRATION.SUBCODE)
|
|
@@ -55,7 +75,10 @@ export function errorDirectoryExists(dir: string): MigrationToolsError {
|
|
|
55
75
|
export function errorMissingFile(file: string, dir: string): MigrationToolsError {
|
|
56
76
|
return new MigrationToolsError('MIGRATION.FILE_MISSING', `Missing ${file}`, {
|
|
57
77
|
why: `Expected "${file}" in "${dir}" but the file does not exist.`,
|
|
58
|
-
fix:
|
|
78
|
+
fix: reemitHint(
|
|
79
|
+
dir,
|
|
80
|
+
'or delete the directory if the migration is unwanted and the source TypeScript is gone.',
|
|
81
|
+
),
|
|
59
82
|
details: { file, dir },
|
|
60
83
|
});
|
|
61
84
|
}
|
|
@@ -63,19 +86,52 @@ export function errorMissingFile(file: string, dir: string): MigrationToolsError
|
|
|
63
86
|
export function errorInvalidJson(filePath: string, parseError: string): MigrationToolsError {
|
|
64
87
|
return new MigrationToolsError('MIGRATION.INVALID_JSON', 'Invalid JSON in migration file', {
|
|
65
88
|
why: `Failed to parse "${filePath}": ${parseError}`,
|
|
66
|
-
fix:
|
|
89
|
+
fix: reemitHint(dirname(filePath), 'or restore the directory from version control.'),
|
|
67
90
|
details: { filePath, parseError },
|
|
68
91
|
});
|
|
69
92
|
}
|
|
70
93
|
|
|
71
94
|
export function errorInvalidManifest(filePath: string, reason: string): MigrationToolsError {
|
|
72
95
|
return new MigrationToolsError('MIGRATION.INVALID_MANIFEST', 'Invalid migration manifest', {
|
|
73
|
-
why: `
|
|
74
|
-
fix:
|
|
96
|
+
why: `Migration manifest at "${filePath}" is invalid: ${reason}`,
|
|
97
|
+
fix: reemitHint(dirname(filePath), 'or restore the directory from version control.'),
|
|
75
98
|
details: { filePath, reason },
|
|
76
99
|
});
|
|
77
100
|
}
|
|
78
101
|
|
|
102
|
+
export function errorInvalidOperationEntry(index: number, reason: string): MigrationToolsError {
|
|
103
|
+
return new MigrationToolsError(
|
|
104
|
+
'MIGRATION.INVALID_OPERATION_ENTRY',
|
|
105
|
+
'Migration operation entry is malformed',
|
|
106
|
+
{
|
|
107
|
+
why: `Operation at index ${index} returned by the migration class failed schema validation: ${reason}.`,
|
|
108
|
+
fix: "Update the migration class so each entry of `operations` carries `id` (string), `label` (string), and `operationClass` (one of 'additive' | 'widening' | 'destructive' | 'data').",
|
|
109
|
+
details: { index, reason },
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function errorStaleContractBookends(args: {
|
|
115
|
+
readonly side: 'from' | 'to';
|
|
116
|
+
readonly metaHash: string | null;
|
|
117
|
+
readonly contractHash: string;
|
|
118
|
+
}): MigrationToolsError {
|
|
119
|
+
const { side, metaHash, contractHash } = args;
|
|
120
|
+
// `meta.from` is `string | null` (null = baseline). Render `null` as a
|
|
121
|
+
// human-readable token in the diagnostic so the message stays clear when
|
|
122
|
+
// the mismatch is a baseline-vs-non-baseline disagreement.
|
|
123
|
+
const renderedMetaHash = metaHash === null ? 'null (baseline)' : `"${metaHash}"`;
|
|
124
|
+
return new MigrationToolsError(
|
|
125
|
+
'MIGRATION.STALE_CONTRACT_BOOKENDS',
|
|
126
|
+
'Migration manifest contract bookends disagree with describe()',
|
|
127
|
+
{
|
|
128
|
+
why: `migration.json stores ${side}Contract.storage.storageHash "${contractHash}", but describe() returned meta.${side} = ${renderedMetaHash}. The bookend is stale — most likely the migration's describe() was edited after the package was scaffolded by \`migration plan\`.`,
|
|
129
|
+
fix: 'Re-run `migration plan` to regenerate the package with fresh contract bookends, or restore the directory from version control.',
|
|
130
|
+
details: { side, metaHash, contractHash },
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
79
135
|
export function errorInvalidSlug(slug: string): MigrationToolsError {
|
|
80
136
|
return new MigrationToolsError('MIGRATION.INVALID_NAME', 'Invalid migration name', {
|
|
81
137
|
why: `The slug "${slug}" contains no valid characters after sanitization (only a-z, 0-9 are kept).`,
|
|
@@ -92,13 +148,58 @@ export function errorInvalidDestName(destName: string): MigrationToolsError {
|
|
|
92
148
|
});
|
|
93
149
|
}
|
|
94
150
|
|
|
95
|
-
export function
|
|
151
|
+
export function errorInvalidSpaceId(spaceId: string): MigrationToolsError {
|
|
152
|
+
return new MigrationToolsError(
|
|
153
|
+
'MIGRATION.INVALID_SPACE_ID',
|
|
154
|
+
'Invalid contract space identifier',
|
|
155
|
+
{
|
|
156
|
+
why: `The space id "${spaceId}" does not match the required pattern /^[a-z][a-z0-9_-]{0,63}$/. Space ids are used as filesystem directory names under \`migrations/\`, so the pattern is conservative on purpose.`,
|
|
157
|
+
fix: 'Pick a lowercase identifier that begins with a letter and contains only lowercase letters, digits, hyphens, or underscores; max 64 characters total.',
|
|
158
|
+
details: { spaceId },
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function errorDescriptorHeadHashMismatch(args: {
|
|
164
|
+
readonly extensionId: string;
|
|
165
|
+
readonly recomputedHash: string;
|
|
166
|
+
readonly headRefHash: string;
|
|
167
|
+
}): MigrationToolsError {
|
|
168
|
+
const { extensionId, recomputedHash, headRefHash } = args;
|
|
169
|
+
return new MigrationToolsError(
|
|
170
|
+
'MIGRATION.DESCRIPTOR_HEAD_HASH_MISMATCH',
|
|
171
|
+
"Extension descriptor's headRef.hash does not match its contractJson",
|
|
172
|
+
{
|
|
173
|
+
why: `Extension "${extensionId}" publishes a \`contractSpace\` whose \`headRef.hash\` (${headRefHash}) does not match the canonical hash recomputed from \`contractSpace.contractJson\` (${recomputedHash}). This means the extension descriptor was published with stale \`headRef.hash\` — typically because the contract was bumped without rerunning the extension's emit pipeline.`,
|
|
174
|
+
fix: 'Re-run the extension authoring pipeline so `contractJson.storage.storageHash` and `headRef.hash` agree, then republish the extension. If you are the extension author and you intentionally bumped `contractJson`, recompute and update `headRef.hash` (and refresh any on-disk migration metadata that derives from it).',
|
|
175
|
+
details: { extensionId, recomputedHash, headRefHash },
|
|
176
|
+
},
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function errorDuplicateSpaceId(spaceId: string): MigrationToolsError {
|
|
181
|
+
return new MigrationToolsError(
|
|
182
|
+
'MIGRATION.DUPLICATE_SPACE_ID',
|
|
183
|
+
'Duplicate contract space identifier',
|
|
184
|
+
{
|
|
185
|
+
why: `The space id "${spaceId}" appears more than once in the per-space planner input. Each space id must be unique across the inputs (the per-space planner emits one output entry per id).`,
|
|
186
|
+
fix: 'Deduplicate the inputs before passing them to `planAllSpaces` — typically by checking your `extensionPacks` declaration for repeated entries.',
|
|
187
|
+
details: { spaceId },
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function errorSameSourceAndTarget(dir: string, hash: string): MigrationToolsError {
|
|
193
|
+
const dirName = basename(dir);
|
|
96
194
|
return new MigrationToolsError(
|
|
97
195
|
'MIGRATION.SAME_SOURCE_AND_TARGET',
|
|
98
|
-
'Migration has same source and target',
|
|
196
|
+
'Migration without data-transform operations has same source and target',
|
|
99
197
|
{
|
|
100
|
-
why: `Migration "${dirName}" has from === to === "${hash}".
|
|
101
|
-
fix:
|
|
198
|
+
why: `Migration "${dirName}" has from === to === "${hash}" and declares no data-transform operations. Self-edges are only allowed when the migration runs at least one dataTransform — otherwise the migration is a no-op.`,
|
|
199
|
+
fix: reemitHint(
|
|
200
|
+
dir,
|
|
201
|
+
'and either change the contract so from ≠ to, add a dataTransform op, or delete the directory if the migration is unwanted.',
|
|
202
|
+
),
|
|
102
203
|
details: { dirName, hash },
|
|
103
204
|
},
|
|
104
205
|
);
|
|
@@ -175,14 +276,147 @@ export function errorInvalidRefValue(value: string): MigrationToolsError {
|
|
|
175
276
|
});
|
|
176
277
|
}
|
|
177
278
|
|
|
178
|
-
export function
|
|
279
|
+
export function errorDuplicateMigrationHash(migrationHash: string): MigrationToolsError {
|
|
179
280
|
return new MigrationToolsError(
|
|
180
|
-
'MIGRATION.
|
|
181
|
-
'Duplicate
|
|
281
|
+
'MIGRATION.DUPLICATE_MIGRATION_HASH',
|
|
282
|
+
'Duplicate migrationHash in migration graph',
|
|
182
283
|
{
|
|
183
|
-
why: `Multiple migrations share
|
|
184
|
-
fix: 'Regenerate one of the conflicting migrations so each
|
|
185
|
-
details: {
|
|
284
|
+
why: `Multiple migrations share migrationHash "${migrationHash}". Each migration must have a unique content-addressed identity.`,
|
|
285
|
+
fix: 'Regenerate one of the conflicting migrations so each migrationHash is unique, then re-run migration commands.',
|
|
286
|
+
details: { migrationHash },
|
|
186
287
|
},
|
|
187
288
|
);
|
|
188
289
|
}
|
|
290
|
+
|
|
291
|
+
export function errorInvalidInvariantId(invariantId: string): MigrationToolsError {
|
|
292
|
+
return new MigrationToolsError('MIGRATION.INVALID_INVARIANT_ID', 'Invalid invariantId', {
|
|
293
|
+
why: `invariantId ${JSON.stringify(invariantId)} is invalid. Ids must be non-empty and contain no whitespace or control characters (including Unicode whitespace like NBSP); other content (kebab-case, camelCase, namespaced, Unicode letters) is allowed.`,
|
|
294
|
+
fix: 'Pick an invariantId without spaces, tabs, newlines, or control characters — e.g. "backfill-user-phone", "users/backfill-phone", or "BackfillUserPhone".',
|
|
295
|
+
details: { invariantId },
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function errorDuplicateInvariantInEdge(invariantId: string): MigrationToolsError {
|
|
300
|
+
return new MigrationToolsError(
|
|
301
|
+
'MIGRATION.DUPLICATE_INVARIANT_IN_EDGE',
|
|
302
|
+
'Duplicate invariantId on a single migration',
|
|
303
|
+
{
|
|
304
|
+
why: `invariantId "${invariantId}" is declared by more than one dataTransform on the same migration. The marker stores invariants as a set and the routing layer treats them as edge-level, so two ops cannot share a routing identity.`,
|
|
305
|
+
fix: 'Rename one of the conflicting dataTransform invariantIds, or drop invariantId on the op that does not need to be routing-visible.',
|
|
306
|
+
details: { invariantId },
|
|
307
|
+
},
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function errorProvidedInvariantsMismatch(
|
|
312
|
+
filePath: string,
|
|
313
|
+
stored: readonly string[],
|
|
314
|
+
derived: readonly string[],
|
|
315
|
+
): MigrationToolsError {
|
|
316
|
+
const storedSet = new Set(stored);
|
|
317
|
+
const derivedSet = new Set(derived);
|
|
318
|
+
const missing = [...derivedSet].filter((id) => !storedSet.has(id));
|
|
319
|
+
const extra = [...storedSet].filter((id) => !derivedSet.has(id));
|
|
320
|
+
// When sets agree but arrays don't, the only difference is ordering — call
|
|
321
|
+
// it out so the reader doesn't stare at two visually-identical arrays.
|
|
322
|
+
// Canonical providedInvariants is sorted ascending; a manifest with the
|
|
323
|
+
// same ids in a different order is still a mismatch (the hash check would
|
|
324
|
+
// also fail), but the human-readable diagnostic is otherwise unhelpful.
|
|
325
|
+
const orderingOnly = missing.length === 0 && extra.length === 0;
|
|
326
|
+
const why = orderingOnly
|
|
327
|
+
? `migration.json at "${filePath}" stores providedInvariants ${JSON.stringify(stored)}, but the canonical value derived from ops.json is ${JSON.stringify(derived)} — same ids, different order. Canonical providedInvariants is sorted ascending.`
|
|
328
|
+
: `migration.json at "${filePath}" stores providedInvariants ${JSON.stringify(stored)}, but the value derived from ops.json is ${JSON.stringify(derived)}. The manifest copy was likely hand-edited without re-emitting.`;
|
|
329
|
+
return new MigrationToolsError(
|
|
330
|
+
'MIGRATION.PROVIDED_INVARIANTS_MISMATCH',
|
|
331
|
+
'providedInvariants on migration.json disagrees with ops.json',
|
|
332
|
+
{
|
|
333
|
+
why,
|
|
334
|
+
fix: reemitHint(dirname(filePath), 'or restore the directory from version control.'),
|
|
335
|
+
details: { filePath, stored, derived, difference: { missing, extra } },
|
|
336
|
+
},
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Wire-shape edge surfaced through the JSON envelope's
|
|
342
|
+
* `meta.structuralPath` of `MIGRATION.NO_INVARIANT_PATH`. Slim by design —
|
|
343
|
+
* authoring metadata (`createdAt`, `labels`) lives on `MigrationEdge` but
|
|
344
|
+
* is intentionally dropped here so the envelope stays stable across
|
|
345
|
+
* graph-internal refactors.
|
|
346
|
+
*
|
|
347
|
+
* Stability: any field added here is part of the public CLI JSON contract.
|
|
348
|
+
* Callers (CLI consumers, agents) must be able to treat
|
|
349
|
+
* `(dirName, migrationHash, from, to, invariants)` as the canonical shape.
|
|
350
|
+
*/
|
|
351
|
+
export interface NoInvariantPathStructuralEdge {
|
|
352
|
+
readonly dirName: string;
|
|
353
|
+
readonly migrationHash: string;
|
|
354
|
+
readonly from: string;
|
|
355
|
+
readonly to: string;
|
|
356
|
+
readonly invariants: readonly string[];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function errorNoInvariantPath(args: {
|
|
360
|
+
readonly refName?: string;
|
|
361
|
+
readonly required: readonly string[];
|
|
362
|
+
readonly missing: readonly string[];
|
|
363
|
+
readonly structuralPath: readonly NoInvariantPathStructuralEdge[];
|
|
364
|
+
}): MigrationToolsError {
|
|
365
|
+
const { refName, required, missing, structuralPath } = args;
|
|
366
|
+
const refClause = refName ? `Ref "${refName}"` : 'Target';
|
|
367
|
+
const missingList = missing.map((id) => JSON.stringify(id)).join(', ');
|
|
368
|
+
const requiredList = required.map((id) => JSON.stringify(id)).join(', ');
|
|
369
|
+
return new MigrationToolsError(
|
|
370
|
+
'MIGRATION.NO_INVARIANT_PATH',
|
|
371
|
+
'No path covers the required invariants',
|
|
372
|
+
{
|
|
373
|
+
why: `${refClause} requires invariants the reachable path doesn't cover. required=[${requiredList}], missing=[${missingList}].`,
|
|
374
|
+
fix: 'Add a migration on the path that runs `dataTransform({ invariantId: "<id>", … })` for each missing invariant, or retarget the ref to a hash whose path already provides them.',
|
|
375
|
+
details: {
|
|
376
|
+
required,
|
|
377
|
+
missing,
|
|
378
|
+
structuralPath,
|
|
379
|
+
...ifDefined('refName', refName),
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function errorUnknownInvariant(args: {
|
|
386
|
+
readonly refName?: string;
|
|
387
|
+
readonly unknown: readonly string[];
|
|
388
|
+
readonly declared: readonly string[];
|
|
389
|
+
}): MigrationToolsError {
|
|
390
|
+
const { refName, unknown, declared } = args;
|
|
391
|
+
const refClause = refName ? `Ref "${refName}" declares` : 'Declares';
|
|
392
|
+
const unknownList = unknown.map((id) => JSON.stringify(id)).join(', ');
|
|
393
|
+
return new MigrationToolsError(
|
|
394
|
+
'MIGRATION.UNKNOWN_INVARIANT',
|
|
395
|
+
'Ref declares invariants no migration in the graph provides',
|
|
396
|
+
{
|
|
397
|
+
why: `${refClause} invariants no migration in the graph provides. unknown=[${unknownList}].`,
|
|
398
|
+
fix: 'Either the ref has a typo, or the declaring migration has not been authored/attested yet. Re-check the ref file and the migrations directory.',
|
|
399
|
+
details: {
|
|
400
|
+
unknown,
|
|
401
|
+
declared,
|
|
402
|
+
...ifDefined('refName', refName),
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export function errorMigrationHashMismatch(
|
|
409
|
+
dir: string,
|
|
410
|
+
storedHash: string,
|
|
411
|
+
computedHash: string,
|
|
412
|
+
): MigrationToolsError {
|
|
413
|
+
// Render a cwd-relative path in the human-readable diagnostic so users
|
|
414
|
+
// running CLI commands from the project root see a familiar short path.
|
|
415
|
+
// Keep the absolute path in `details.dir` for machine consumers.
|
|
416
|
+
const relativeDir = relative(process.cwd(), dir);
|
|
417
|
+
return new MigrationToolsError('MIGRATION.HASH_MISMATCH', 'Migration package is corrupt', {
|
|
418
|
+
why: `Stored migrationHash "${storedHash}" does not match the recomputed hash "${computedHash}" for "${relativeDir}". The migration.json or ops.json has been edited or partially written since emit.`,
|
|
419
|
+
fix: reemitHint(dir, 'or restore the directory from version control.'),
|
|
420
|
+
details: { dir, storedHash, computedHash },
|
|
421
|
+
});
|
|
422
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type DeclaredExtensionEntry,
|
|
3
|
+
type LayoutViolation,
|
|
4
|
+
type LoadAggregateError,
|
|
5
|
+
type LoadAggregateInput,
|
|
6
|
+
type LoadAggregateOutput,
|
|
7
|
+
loadContractSpaceAggregate,
|
|
8
|
+
} from '../aggregate/loader';
|
|
9
|
+
export type { ContractMarkerRecordLike } from '../aggregate/marker-types';
|
|
10
|
+
export {
|
|
11
|
+
type AggregateCurrentDBState,
|
|
12
|
+
type AggregateMigrationEdgeRef,
|
|
13
|
+
type AggregatePerSpacePlan,
|
|
14
|
+
type AggregatePlannerError,
|
|
15
|
+
type AggregatePlannerInput,
|
|
16
|
+
type AggregatePlannerOutput,
|
|
17
|
+
type AggregatePlannerSuccess,
|
|
18
|
+
type CallerPolicy,
|
|
19
|
+
planAggregate,
|
|
20
|
+
} from '../aggregate/planner';
|
|
21
|
+
export { projectSchemaToSpace } from '../aggregate/project-schema-to-space';
|
|
22
|
+
export {
|
|
23
|
+
type GraphWalkOutcome,
|
|
24
|
+
type GraphWalkStrategyInputs,
|
|
25
|
+
graphWalkStrategy,
|
|
26
|
+
} from '../aggregate/strategies/graph-walk';
|
|
27
|
+
export type {
|
|
28
|
+
ContractSpaceAggregate,
|
|
29
|
+
ContractSpaceMember,
|
|
30
|
+
HydratedMigrationGraph,
|
|
31
|
+
} from '../aggregate/types';
|
|
32
|
+
export {
|
|
33
|
+
type AggregateVerifierError,
|
|
34
|
+
type AggregateVerifierInput,
|
|
35
|
+
type AggregateVerifierOutput,
|
|
36
|
+
type AggregateVerifierSuccess,
|
|
37
|
+
type MarkerCheckResult,
|
|
38
|
+
type MarkerCheckSection,
|
|
39
|
+
type OrphanElement,
|
|
40
|
+
type SchemaCheckSection,
|
|
41
|
+
verifyAggregate,
|
|
42
|
+
} from '../aggregate/verifier';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { MigrationEdge, MigrationGraph } from '../graph';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { deriveProvidedInvariants, validateInvariantId } from '../invariants';
|
package/src/exports/io.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export {
|
|
2
2
|
copyFilesWithRename,
|
|
3
3
|
formatMigrationDirName,
|
|
4
|
+
materialiseExtensionMigrationPackageIfMissing,
|
|
5
|
+
materialiseMigrationPackage,
|
|
4
6
|
readMigrationPackage,
|
|
5
7
|
readMigrationsDir,
|
|
6
|
-
|
|
8
|
+
writeMigrationMetadata,
|
|
7
9
|
writeMigrationOps,
|
|
8
10
|
writeMigrationPackage,
|
|
9
11
|
} from '../io';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { MigrationHints, MigrationMetadata } from '../metadata';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { PathDecision } from '../
|
|
1
|
+
export type { PathDecision } from '../migration-graph';
|
|
2
2
|
export {
|
|
3
3
|
detectCycles,
|
|
4
4
|
detectOrphans,
|
|
@@ -6,6 +6,7 @@ export {
|
|
|
6
6
|
findLeaf,
|
|
7
7
|
findPath,
|
|
8
8
|
findPathWithDecision,
|
|
9
|
+
findPathWithInvariants,
|
|
9
10
|
findReachableLeaves,
|
|
10
11
|
reconstructGraph,
|
|
11
|
-
} from '../
|
|
12
|
+
} from '../migration-graph';
|
package/src/exports/migration.ts
CHANGED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export {
|
|
2
|
+
assertDescriptorSelfConsistency,
|
|
3
|
+
type DescriptorSelfConsistencyInputs,
|
|
4
|
+
} from '../assert-descriptor-self-consistency';
|
|
5
|
+
export {
|
|
6
|
+
type ComputeExtensionSpaceApplyPathInputs,
|
|
7
|
+
computeExtensionSpaceApplyPath,
|
|
8
|
+
type ExtensionSpaceApplyPathOutcome,
|
|
9
|
+
} from '../compute-extension-space-apply-path';
|
|
10
|
+
export type { SpaceApplyInput } from '../concatenate-space-apply-inputs';
|
|
11
|
+
export { contractSpaceFromJson } from '../contract-space-from-json';
|
|
12
|
+
export {
|
|
13
|
+
type ContractSpaceArtefactInputs,
|
|
14
|
+
emitContractSpaceArtefacts,
|
|
15
|
+
} from '../emit-contract-space-artefacts';
|
|
16
|
+
export {
|
|
17
|
+
type DiskContractSpaceState,
|
|
18
|
+
gatherDiskContractSpaceState,
|
|
19
|
+
} from '../gather-disk-contract-space-state';
|
|
20
|
+
export {
|
|
21
|
+
planAllSpaces,
|
|
22
|
+
type SpacePlanInput,
|
|
23
|
+
type SpacePlanOutput,
|
|
24
|
+
} from '../plan-all-spaces';
|
|
25
|
+
export { readContractSpaceContract } from '../read-contract-space-contract';
|
|
26
|
+
export {
|
|
27
|
+
type ContractSpaceHeadRef,
|
|
28
|
+
readContractSpaceHeadRef,
|
|
29
|
+
} from '../read-contract-space-head-ref';
|
|
30
|
+
export {
|
|
31
|
+
APP_SPACE_ID,
|
|
32
|
+
assertValidSpaceId,
|
|
33
|
+
isValidSpaceId,
|
|
34
|
+
spaceMigrationDirectory,
|
|
35
|
+
type ValidSpaceId,
|
|
36
|
+
} from '../space-layout';
|
|
37
|
+
export {
|
|
38
|
+
type ContractSpaceHeadRecord,
|
|
39
|
+
listContractSpaceDirectories,
|
|
40
|
+
type SpaceMarkerRecord,
|
|
41
|
+
type SpaceVerifierViolation,
|
|
42
|
+
type VerifyContractSpacesInputs,
|
|
43
|
+
type VerifyContractSpacesResult,
|
|
44
|
+
verifyContractSpaces,
|
|
45
|
+
} from '../verify-contract-spaces';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { readContractSpaceHeadRef } from './read-contract-space-head-ref';
|
|
2
|
+
import { APP_SPACE_ID } from './space-layout';
|
|
3
|
+
import {
|
|
4
|
+
type ContractSpaceHeadRecord,
|
|
5
|
+
listContractSpaceDirectories,
|
|
6
|
+
} from './verify-contract-spaces';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Disk-side inputs to {@link import('./verify-contract-spaces').verifyContractSpaces}
|
|
10
|
+
* — gathered without touching the live database. The caller composes
|
|
11
|
+
* this with the marker rows it reads from the runtime to invoke the
|
|
12
|
+
* verifier.
|
|
13
|
+
*/
|
|
14
|
+
export interface DiskContractSpaceState {
|
|
15
|
+
/** Contract-space directory names observed under `<projectMigrationsDir>/`. */
|
|
16
|
+
readonly spaceDirsOnDisk: readonly string[];
|
|
17
|
+
/** Head-ref `(hash, invariants)` per extension space. */
|
|
18
|
+
readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Read the on-disk state the per-space verifier needs:
|
|
23
|
+
*
|
|
24
|
+
* - The list of contract-space directories under
|
|
25
|
+
* `<projectMigrationsDir>/` (via
|
|
26
|
+
* {@link import('./verify-contract-spaces').listContractSpaceDirectories}).
|
|
27
|
+
* - The on-disk head ref `(hash, invariants)` for each declared extension space
|
|
28
|
+
* (via {@link readContractSpaceHeadRef}; missing on-disk artefacts are simply
|
|
29
|
+
* omitted — the verifier reports them as `declaredButUnmigrated`).
|
|
30
|
+
*
|
|
31
|
+
* Synchronous in spirit but async due to filesystem reads. Reads only
|
|
32
|
+
* the user's repo. **Does not import any extension descriptor module.**
|
|
33
|
+
*
|
|
34
|
+
* Composition convention: pure target-agnostic primitive in
|
|
35
|
+
* `1-framework`; the SQL family (and any future target family) wires
|
|
36
|
+
* it into its `dbInit` / `verify` flows alongside its own marker-row
|
|
37
|
+
* read before invoking `verifyContractSpaces`.
|
|
38
|
+
*/
|
|
39
|
+
export async function gatherDiskContractSpaceState(args: {
|
|
40
|
+
readonly projectMigrationsDir: string;
|
|
41
|
+
/**
|
|
42
|
+
* Set of space ids the project declares: `'app'` plus each entry in
|
|
43
|
+
* `extensionPacks` whose descriptor exposes a `contractSpace`. The
|
|
44
|
+
* helper reads on-disk head data only for the extension members.
|
|
45
|
+
*/
|
|
46
|
+
readonly loadedSpaceIds: ReadonlySet<string>;
|
|
47
|
+
}): Promise<DiskContractSpaceState> {
|
|
48
|
+
const { projectMigrationsDir, loadedSpaceIds } = args;
|
|
49
|
+
|
|
50
|
+
const spaceDirsOnDisk = await listContractSpaceDirectories(projectMigrationsDir);
|
|
51
|
+
|
|
52
|
+
const headRefsBySpace = new Map<string, ContractSpaceHeadRecord>();
|
|
53
|
+
for (const spaceId of loadedSpaceIds) {
|
|
54
|
+
if (spaceId === APP_SPACE_ID) continue;
|
|
55
|
+
const head = await readContractSpaceHeadRef(projectMigrationsDir, spaceId);
|
|
56
|
+
if (head !== null) {
|
|
57
|
+
headRefsBySpace.set(spaceId, head);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { spaceDirsOnDisk, headRefsBySpace };
|
|
62
|
+
}
|
package/src/graph-ops.ts
CHANGED
|
@@ -3,13 +3,18 @@ import { Queue } from './queue';
|
|
|
3
3
|
/**
|
|
4
4
|
* One step of a BFS traversal.
|
|
5
5
|
*
|
|
6
|
-
* `parent` and `incomingEdge` are `null` for start
|
|
7
|
-
* reached via any edge. For every other
|
|
8
|
-
* by which this
|
|
6
|
+
* `parent` and `incomingEdge` are `null` for start states — they were not
|
|
7
|
+
* reached via any edge. For every other state they record the predecessor
|
|
8
|
+
* state and the edge by which this state was first reached.
|
|
9
|
+
*
|
|
10
|
+
* `state` is the BFS state, most often a string (graph node identifier) but
|
|
11
|
+
* can be a composite object. The string overload keeps the common case
|
|
12
|
+
* ergonomic; the generic overload accepts a caller-supplied `key` function
|
|
13
|
+
* that produces a stable equality key for dedup.
|
|
9
14
|
*/
|
|
10
|
-
export interface BfsStep<E> {
|
|
11
|
-
readonly
|
|
12
|
-
readonly parent:
|
|
15
|
+
export interface BfsStep<S, E> {
|
|
16
|
+
readonly state: S;
|
|
17
|
+
readonly parent: S | null;
|
|
13
18
|
readonly incomingEdge: E | null;
|
|
14
19
|
}
|
|
15
20
|
|
|
@@ -17,48 +22,70 @@ export interface BfsStep<E> {
|
|
|
17
22
|
* Generic breadth-first traversal.
|
|
18
23
|
*
|
|
19
24
|
* Direction (forward/reverse) is expressed by the caller's `neighbours`
|
|
20
|
-
* closure: return `{ next, edge }` pairs where `next` is the
|
|
25
|
+
* closure: return `{ next, edge }` pairs where `next` is the state to visit
|
|
21
26
|
* next and `edge` is the edge that connects them. Callers that don't need
|
|
22
27
|
* path reconstruction can ignore the `parent`/`incomingEdge` fields of each
|
|
23
28
|
* yielded step.
|
|
24
29
|
*
|
|
30
|
+
* Ordering — when the result needs to be deterministic (path-finding) the
|
|
31
|
+
* caller is responsible for sorting inside `neighbours`; this generator
|
|
32
|
+
* does not impose an ordering hook of its own. State-dependent orderings
|
|
33
|
+
* have full access to the source state inside the closure.
|
|
34
|
+
*
|
|
25
35
|
* Stops are intrinsic — callers `break` out of the `for..of` loop when
|
|
26
36
|
* they've found what they're looking for.
|
|
27
|
-
*
|
|
28
|
-
* `ordering`, if provided, controls the order in which neighbours of each
|
|
29
|
-
* node are enqueued. Only matters for path-finding: a deterministic ordering
|
|
30
|
-
* makes BFS return a deterministic shortest path when multiple exist.
|
|
31
37
|
*/
|
|
32
|
-
export function
|
|
38
|
+
export function bfs<E>(
|
|
33
39
|
starts: Iterable<string>,
|
|
34
|
-
neighbours: (
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
neighbours: (state: string) => Iterable<{ next: string; edge: E }>,
|
|
41
|
+
): Generator<BfsStep<string, E>>;
|
|
42
|
+
export function bfs<S, E>(
|
|
43
|
+
starts: Iterable<S>,
|
|
44
|
+
neighbours: (state: S) => Iterable<{ next: S; edge: E }>,
|
|
45
|
+
key: (state: S) => string,
|
|
46
|
+
): Generator<BfsStep<S, E>>;
|
|
47
|
+
export function* bfs<S, E>(
|
|
48
|
+
starts: Iterable<S>,
|
|
49
|
+
neighbours: (state: S) => Iterable<{ next: S; edge: E }>,
|
|
50
|
+
// Identity default for the string overload. TypeScript can't express
|
|
51
|
+
// "default applies only when S = string", so this cast bridges the
|
|
52
|
+
// generic implementation signature to the public overloads — which
|
|
53
|
+
// guarantee `key` is omitted only when S = string at the call site.
|
|
54
|
+
key: (state: S) => string = (state) => state as unknown as string,
|
|
55
|
+
): Generator<BfsStep<S, E>> {
|
|
56
|
+
// Queue entries carry the state alongside its key so we don't recompute
|
|
57
|
+
// key() twice per visit (once on dedup, once on parent lookup). Composite
|
|
58
|
+
// keys can be non-trivial to compute; string-overload callers pay nothing
|
|
59
|
+
// since key() is identity there.
|
|
60
|
+
interface Entry {
|
|
61
|
+
readonly state: S;
|
|
62
|
+
readonly key: string;
|
|
63
|
+
}
|
|
37
64
|
const visited = new Set<string>();
|
|
38
|
-
const parentMap = new Map<string, { parent:
|
|
39
|
-
const queue = new Queue<
|
|
65
|
+
const parentMap = new Map<string, { parent: S; edge: E }>();
|
|
66
|
+
const queue = new Queue<Entry>();
|
|
40
67
|
for (const start of starts) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
68
|
+
const k = key(start);
|
|
69
|
+
if (!visited.has(k)) {
|
|
70
|
+
visited.add(k);
|
|
71
|
+
queue.push({ state: start, key: k });
|
|
44
72
|
}
|
|
45
73
|
}
|
|
46
74
|
while (!queue.isEmpty) {
|
|
47
|
-
const current = queue.shift();
|
|
48
|
-
const parentInfo = parentMap.get(
|
|
75
|
+
const { state: current, key: curKey } = queue.shift();
|
|
76
|
+
const parentInfo = parentMap.get(curKey);
|
|
49
77
|
yield {
|
|
50
|
-
|
|
78
|
+
state: current,
|
|
51
79
|
parent: parentInfo?.parent ?? null,
|
|
52
80
|
incomingEdge: parentInfo?.edge ?? null,
|
|
53
81
|
};
|
|
54
82
|
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
queue.push(next);
|
|
83
|
+
for (const { next, edge } of neighbours(current)) {
|
|
84
|
+
const k = key(next);
|
|
85
|
+
if (!visited.has(k)) {
|
|
86
|
+
visited.add(k);
|
|
87
|
+
parentMap.set(k, { parent: current, edge });
|
|
88
|
+
queue.push({ state: next, key: k });
|
|
62
89
|
}
|
|
63
90
|
}
|
|
64
91
|
}
|