@prisma-next/migration-tools 0.5.0-dev.6 → 0.5.0-dev.60
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-BQEHsaEx.mjs} +1 -1
- package/dist/{constants-BRi0X7B_.mjs.map → constants-BQEHsaEx.mjs.map} +1 -1
- package/dist/errors-CfmjBeK0.mjs +272 -0
- package/dist/errors-CfmjBeK0.mjs.map +1 -0
- package/dist/exports/constants.mjs +1 -1
- package/dist/exports/errors.d.mts +63 -0
- package/dist/exports/errors.d.mts.map +1 -0
- package/dist/exports/errors.mjs +3 -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 +3 -0
- package/dist/exports/invariants.d.mts +24 -0
- package/dist/exports/invariants.d.mts.map +1 -0
- package/dist/exports/invariants.mjs +4 -0
- package/dist/exports/io.d.mts +7 -6
- package/dist/exports/io.d.mts.map +1 -1
- package/dist/exports/io.mjs +162 -2
- package/dist/exports/io.mjs.map +1 -0
- package/dist/exports/metadata.d.mts +2 -0
- package/dist/exports/metadata.mjs +1 -0
- package/dist/exports/migration-graph.d.mts +124 -0
- package/dist/exports/migration-graph.d.mts.map +1 -0
- package/dist/exports/migration-graph.mjs +526 -0
- package/dist/exports/migration-graph.mjs.map +1 -0
- package/dist/exports/migration-ts.mjs +1 -1
- package/dist/exports/migration.d.mts +15 -14
- package/dist/exports/migration.d.mts.map +1 -1
- package/dist/exports/migration.mjs +69 -41
- package/dist/exports/migration.mjs.map +1 -1
- package/dist/exports/package.d.mts +2 -0
- package/dist/exports/package.mjs +1 -0
- package/dist/exports/refs.mjs +2 -2
- package/dist/graph-BHPv-9Gl.d.mts +28 -0
- package/dist/graph-BHPv-9Gl.d.mts.map +1 -0
- package/dist/hash-BARZdVgW.mjs +76 -0
- package/dist/hash-BARZdVgW.mjs.map +1 -0
- package/dist/invariants-30VA65sB.mjs +42 -0
- package/dist/invariants-30VA65sB.mjs.map +1 -0
- package/dist/metadata-BP1cmU7Z.d.mts +50 -0
- package/dist/metadata-BP1cmU7Z.d.mts.map +1 -0
- package/dist/op-schema-DZKFua46.mjs +14 -0
- package/dist/op-schema-DZKFua46.mjs.map +1 -0
- package/dist/package-5HCCg0z-.d.mts +21 -0
- package/dist/package-5HCCg0z-.d.mts.map +1 -0
- package/package.json +31 -14
- package/src/errors.ts +210 -17
- package/src/exports/errors.ts +7 -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 +1 -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 +1 -0
- package/src/graph-ops.ts +57 -30
- package/src/graph.ts +25 -0
- package/src/hash.ts +91 -0
- package/src/invariants.ts +45 -0
- package/src/io.ts +57 -31
- package/src/metadata.ts +41 -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 +18 -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
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { MigrationPlanOperation } from '@prisma-next/framework-components/control';
|
|
2
|
+
import { errorDuplicateInvariantInEdge, errorInvalidInvariantId } from './errors';
|
|
3
|
+
import type { MigrationOps } from './package';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hygiene check for `invariantId`. Rejects empty values plus any
|
|
7
|
+
* whitespace or control character (including Unicode whitespace like
|
|
8
|
+
* NBSP and em space, which are visually identical to ASCII space and
|
|
9
|
+
* routinely sneak in via paste).
|
|
10
|
+
*/
|
|
11
|
+
export function validateInvariantId(invariantId: string): boolean {
|
|
12
|
+
if (invariantId.length === 0) return false;
|
|
13
|
+
return !/[\p{Cc}\p{White_Space}]/u.test(invariantId);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Walk a migration's operations and produce its `providedInvariants`
|
|
18
|
+
* aggregate: the sorted, deduplicated list of `invariantId`s declared
|
|
19
|
+
* by data-transform ops. Ops without `operationClass === 'data'` are
|
|
20
|
+
* skipped; data ops without an `invariantId` are skipped.
|
|
21
|
+
*
|
|
22
|
+
* Throws `MIGRATION.INVALID_INVARIANT_ID` on a malformed id and
|
|
23
|
+
* `MIGRATION.DUPLICATE_INVARIANT_IN_EDGE` on duplicates.
|
|
24
|
+
*/
|
|
25
|
+
export function deriveProvidedInvariants(ops: MigrationOps): readonly string[] {
|
|
26
|
+
const seen = new Set<string>();
|
|
27
|
+
for (const op of ops) {
|
|
28
|
+
const invariantId = readInvariantId(op);
|
|
29
|
+
if (invariantId === undefined) continue;
|
|
30
|
+
if (!validateInvariantId(invariantId)) {
|
|
31
|
+
throw errorInvalidInvariantId(invariantId);
|
|
32
|
+
}
|
|
33
|
+
if (seen.has(invariantId)) {
|
|
34
|
+
throw errorDuplicateInvariantInEdge(invariantId);
|
|
35
|
+
}
|
|
36
|
+
seen.add(invariantId);
|
|
37
|
+
}
|
|
38
|
+
return [...seen].sort();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function readInvariantId(op: MigrationPlanOperation): string | undefined {
|
|
42
|
+
if (op.operationClass !== 'data') return undefined;
|
|
43
|
+
const candidate = (op as { invariantId?: unknown }).invariantId;
|
|
44
|
+
return typeof candidate === 'string' ? candidate : undefined;
|
|
45
|
+
}
|
package/src/io.ts
CHANGED
|
@@ -7,9 +7,15 @@ import {
|
|
|
7
7
|
errorInvalidJson,
|
|
8
8
|
errorInvalidManifest,
|
|
9
9
|
errorInvalidSlug,
|
|
10
|
+
errorMigrationHashMismatch,
|
|
10
11
|
errorMissingFile,
|
|
12
|
+
errorProvidedInvariantsMismatch,
|
|
11
13
|
} from './errors';
|
|
12
|
-
import
|
|
14
|
+
import { verifyMigrationHash } from './hash';
|
|
15
|
+
import { deriveProvidedInvariants } from './invariants';
|
|
16
|
+
import type { MigrationMetadata } from './metadata';
|
|
17
|
+
import { MigrationOpsSchema } from './op-schema';
|
|
18
|
+
import type { MigrationOps, MigrationPackage } from './package';
|
|
13
19
|
|
|
14
20
|
const MANIFEST_FILE = 'migration.json';
|
|
15
21
|
const OPS_FILE = 'ops.json';
|
|
@@ -25,15 +31,16 @@ const MigrationHintsSchema = type({
|
|
|
25
31
|
plannerVersion: 'string',
|
|
26
32
|
});
|
|
27
33
|
|
|
28
|
-
const
|
|
29
|
-
|
|
34
|
+
const MigrationMetadataSchema = type({
|
|
35
|
+
'+': 'reject',
|
|
36
|
+
from: 'string > 0 | null',
|
|
30
37
|
to: 'string',
|
|
31
|
-
|
|
32
|
-
kind: "'regular' | 'baseline'",
|
|
38
|
+
migrationHash: 'string',
|
|
33
39
|
fromContract: 'object | null',
|
|
34
40
|
toContract: 'object',
|
|
35
41
|
hints: MigrationHintsSchema,
|
|
36
42
|
labels: 'string[]',
|
|
43
|
+
providedInvariants: 'string[]',
|
|
37
44
|
'authorship?': type({
|
|
38
45
|
'author?': 'string',
|
|
39
46
|
'email?': 'string',
|
|
@@ -45,18 +52,9 @@ const MigrationManifestSchema = type({
|
|
|
45
52
|
createdAt: 'string',
|
|
46
53
|
});
|
|
47
54
|
|
|
48
|
-
const MigrationOpSchema = type({
|
|
49
|
-
id: 'string',
|
|
50
|
-
label: 'string',
|
|
51
|
-
operationClass: "'additive' | 'widening' | 'destructive' | 'data'",
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Intentionally shallow: operation-specific payload validation is owned by planner/runner layers.
|
|
55
|
-
const MigrationOpsSchema = MigrationOpSchema.array();
|
|
56
|
-
|
|
57
55
|
export async function writeMigrationPackage(
|
|
58
56
|
dir: string,
|
|
59
|
-
|
|
57
|
+
metadata: MigrationMetadata,
|
|
60
58
|
ops: MigrationOps,
|
|
61
59
|
): Promise<void> {
|
|
62
60
|
await mkdir(dirname(dir), { recursive: true });
|
|
@@ -70,7 +68,9 @@ export async function writeMigrationPackage(
|
|
|
70
68
|
throw error;
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
await writeFile(join(dir, MANIFEST_FILE), JSON.stringify(
|
|
71
|
+
await writeFile(join(dir, MANIFEST_FILE), JSON.stringify(metadata, null, 2), {
|
|
72
|
+
flag: 'wx',
|
|
73
|
+
});
|
|
74
74
|
await writeFile(join(dir, OPS_FILE), JSON.stringify(ops, null, 2), { flag: 'wx' });
|
|
75
75
|
}
|
|
76
76
|
|
|
@@ -98,18 +98,18 @@ export async function copyFilesWithRename(
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
export async function
|
|
101
|
+
export async function writeMigrationMetadata(
|
|
102
102
|
dir: string,
|
|
103
|
-
|
|
103
|
+
metadata: MigrationMetadata,
|
|
104
104
|
): Promise<void> {
|
|
105
|
-
await writeFile(join(dir, MANIFEST_FILE), `${JSON.stringify(
|
|
105
|
+
await writeFile(join(dir, MANIFEST_FILE), `${JSON.stringify(metadata, null, 2)}\n`);
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
export async function writeMigrationOps(dir: string, ops: MigrationOps): Promise<void> {
|
|
109
109
|
await writeFile(join(dir, OPS_FILE), `${JSON.stringify(ops, null, 2)}\n`);
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
export async function readMigrationPackage(dir: string): Promise<
|
|
112
|
+
export async function readMigrationPackage(dir: string): Promise<MigrationPackage> {
|
|
113
113
|
const manifestPath = join(dir, MANIFEST_FILE);
|
|
114
114
|
const opsPath = join(dir, OPS_FILE);
|
|
115
115
|
|
|
@@ -133,9 +133,9 @@ export async function readMigrationPackage(dir: string): Promise<MigrationBundle
|
|
|
133
133
|
throw error;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
let
|
|
136
|
+
let metadata: MigrationMetadata;
|
|
137
137
|
try {
|
|
138
|
-
|
|
138
|
+
metadata = JSON.parse(manifestRaw);
|
|
139
139
|
} catch (e) {
|
|
140
140
|
throw errorInvalidJson(manifestPath, e instanceof Error ? e.message : String(e));
|
|
141
141
|
}
|
|
@@ -147,22 +147,48 @@ export async function readMigrationPackage(dir: string): Promise<MigrationBundle
|
|
|
147
147
|
throw errorInvalidJson(opsPath, e instanceof Error ? e.message : String(e));
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
validateMetadata(metadata, manifestPath);
|
|
151
151
|
validateOps(ops, opsPath);
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
// Re-derive before the hash check so format/duplicate diagnostics
|
|
154
|
+
// fire with their dedicated codes rather than as a generic hash mismatch.
|
|
155
|
+
const derivedInvariants = deriveProvidedInvariants(ops);
|
|
156
|
+
if (!arraysEqual(metadata.providedInvariants, derivedInvariants)) {
|
|
157
|
+
throw errorProvidedInvariantsMismatch(
|
|
158
|
+
manifestPath,
|
|
159
|
+
metadata.providedInvariants,
|
|
160
|
+
derivedInvariants,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const pkg: MigrationPackage = {
|
|
154
165
|
dirName: basename(dir),
|
|
155
166
|
dirPath: dir,
|
|
156
|
-
|
|
167
|
+
metadata,
|
|
157
168
|
ops,
|
|
158
169
|
};
|
|
170
|
+
|
|
171
|
+
const verification = verifyMigrationHash(pkg);
|
|
172
|
+
if (!verification.ok) {
|
|
173
|
+
throw errorMigrationHashMismatch(dir, verification.storedHash, verification.computedHash);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return pkg;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function arraysEqual(a: readonly string[], b: readonly string[]): boolean {
|
|
180
|
+
if (a.length !== b.length) return false;
|
|
181
|
+
for (let i = 0; i < a.length; i++) {
|
|
182
|
+
if (a[i] !== b[i]) return false;
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
159
185
|
}
|
|
160
186
|
|
|
161
|
-
function
|
|
162
|
-
|
|
187
|
+
function validateMetadata(
|
|
188
|
+
metadata: unknown,
|
|
163
189
|
filePath: string,
|
|
164
|
-
): asserts
|
|
165
|
-
const result =
|
|
190
|
+
): asserts metadata is MigrationMetadata {
|
|
191
|
+
const result = MigrationMetadataSchema(metadata);
|
|
166
192
|
if (result instanceof type.errors) {
|
|
167
193
|
throw errorInvalidManifest(filePath, result.summary);
|
|
168
194
|
}
|
|
@@ -177,7 +203,7 @@ function validateOps(ops: unknown, filePath: string): asserts ops is MigrationOp
|
|
|
177
203
|
|
|
178
204
|
export async function readMigrationsDir(
|
|
179
205
|
migrationsRoot: string,
|
|
180
|
-
): Promise<readonly
|
|
206
|
+
): Promise<readonly MigrationPackage[]> {
|
|
181
207
|
let entries: string[];
|
|
182
208
|
try {
|
|
183
209
|
entries = await readdir(migrationsRoot);
|
|
@@ -188,7 +214,7 @@ export async function readMigrationsDir(
|
|
|
188
214
|
throw error;
|
|
189
215
|
}
|
|
190
216
|
|
|
191
|
-
const packages:
|
|
217
|
+
const packages: MigrationPackage[] = [];
|
|
192
218
|
|
|
193
219
|
for (const entry of entries.sort()) {
|
|
194
220
|
const entryPath = join(migrationsRoot, entry);
|
package/src/metadata.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Contract } from '@prisma-next/contract/types';
|
|
2
|
+
|
|
3
|
+
export interface MigrationHints {
|
|
4
|
+
readonly used: readonly string[];
|
|
5
|
+
readonly applied: readonly string[];
|
|
6
|
+
readonly plannerVersion: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* In-memory migration metadata envelope. Every migration is content-addressed:
|
|
11
|
+
* the `migrationHash` is a hash over the metadata envelope plus the operations
|
|
12
|
+
* list, computed at write time. There is no draft state — a migration
|
|
13
|
+
* directory either exists with fully attested metadata or it does not.
|
|
14
|
+
*
|
|
15
|
+
* When the planner cannot lower an operation because of an unfilled
|
|
16
|
+
* `placeholder(...)` slot, the migration is still written with `migrationHash`
|
|
17
|
+
* hashed over `ops: []`. Re-running self-emit after the user fills the
|
|
18
|
+
* placeholder produces a *different* `migrationHash` (committed to the real
|
|
19
|
+
* ops); this is intentional.
|
|
20
|
+
*
|
|
21
|
+
* The on-disk JSON shape in `migration.json` matches this type field-for-field
|
|
22
|
+
* — `JSON.stringify(metadata, null, 2)` is the canonical writer output.
|
|
23
|
+
*/
|
|
24
|
+
export interface MigrationMetadata {
|
|
25
|
+
readonly migrationHash: string;
|
|
26
|
+
readonly from: string | null;
|
|
27
|
+
readonly to: string;
|
|
28
|
+
readonly fromContract: Contract | null;
|
|
29
|
+
readonly toContract: Contract;
|
|
30
|
+
readonly hints: MigrationHints;
|
|
31
|
+
readonly labels: readonly string[];
|
|
32
|
+
/**
|
|
33
|
+
* Sorted, deduplicated list of `invariantId`s declared by the
|
|
34
|
+
* migration's data-transform ops. Always present; an empty array
|
|
35
|
+
* means the migration has no routing-visible data transforms.
|
|
36
|
+
*/
|
|
37
|
+
readonly providedInvariants: readonly string[];
|
|
38
|
+
readonly authorship?: { readonly author?: string; readonly email?: string };
|
|
39
|
+
readonly signature?: { readonly keyId: string; readonly value: string } | null;
|
|
40
|
+
readonly createdAt: string;
|
|
41
|
+
}
|
package/src/migration-base.ts
CHANGED
|
@@ -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 {
|
|
12
|
-
import
|
|
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
|
|
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
|
-
|
|
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`
|
|
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
|
|
135
|
-
readonly
|
|
123
|
+
readonly metadata: MigrationMetadata;
|
|
124
|
+
readonly metadataJson: string;
|
|
136
125
|
}
|
|
137
126
|
|
|
138
127
|
/**
|
|
139
|
-
* Build the attested
|
|
140
|
-
* operations list, and the previously-scaffolded
|
|
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
|
|
147
|
-
* change as the author iterates. When no
|
|
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
|
|
138
|
+
* schema-conformant record so the resulting package can still be read,
|
|
150
139
|
* verified, and applied.
|
|
151
140
|
*
|
|
152
|
-
* The `
|
|
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
|
|
144
|
+
function buildAttestedMetadata(
|
|
156
145
|
meta: MigrationMeta,
|
|
157
146
|
ops: MigrationOps,
|
|
158
|
-
existing: Partial<
|
|
159
|
-
):
|
|
160
|
-
|
|
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
|
|
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 `
|
|
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
|
|
178
|
-
return { ...
|
|
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
|
|
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
|
|
232
|
+
* scaffolded metadata, when one exists on disk) to the in-memory
|
|
199
233
|
* artifacts that downstream tooling persists. Owns metadata validation,
|
|
200
|
-
*
|
|
201
|
-
* content-addressed `
|
|
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` / `
|
|
237
|
+
* `opsJson` / `metadataJson`).
|
|
204
238
|
*/
|
|
205
239
|
export function buildMigrationArtifacts(
|
|
206
240
|
instance: Migration,
|
|
207
|
-
existing: Partial<
|
|
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
|
|
261
|
+
const metadata = buildAttestedMetadata(parsed, ops, existing);
|
|
221
262
|
|
|
222
263
|
return {
|
|
223
264
|
opsJson: JSON.stringify(ops, null, 2),
|
|
224
|
-
|
|
225
|
-
|
|
265
|
+
metadata,
|
|
266
|
+
metadataJson: JSON.stringify(metadata, null, 2),
|
|
226
267
|
};
|
|
227
268
|
}
|