@prisma-next/migration-tools 0.5.0-dev.1 → 0.5.0-dev.10

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 (76) hide show
  1. package/README.md +32 -20
  2. package/dist/{constants-BRi0X7B_.mjs → constants-WVGVMOdu.mjs} +1 -1
  3. package/dist/{constants-BRi0X7B_.mjs.map → constants-WVGVMOdu.mjs.map} +1 -1
  4. package/dist/{errors-BKbRGCJM.mjs → errors-CZ9JD4sd.mjs} +50 -21
  5. package/dist/errors-CZ9JD4sd.mjs.map +1 -0
  6. package/dist/exports/constants.mjs +1 -1
  7. package/dist/exports/dag.d.mts +4 -3
  8. package/dist/exports/dag.d.mts.map +1 -1
  9. package/dist/exports/dag.mjs +15 -15
  10. package/dist/exports/dag.mjs.map +1 -1
  11. package/dist/exports/{types.d.mts → errors.d.mts} +6 -8
  12. package/dist/exports/errors.d.mts.map +1 -0
  13. package/dist/exports/errors.mjs +3 -0
  14. package/dist/exports/graph.d.mts +2 -0
  15. package/dist/exports/graph.mjs +1 -0
  16. package/dist/exports/hash.d.mts +52 -0
  17. package/dist/exports/hash.d.mts.map +1 -0
  18. package/dist/exports/hash.mjs +3 -0
  19. package/dist/exports/io.d.mts +7 -6
  20. package/dist/exports/io.d.mts.map +1 -1
  21. package/dist/exports/io.mjs +156 -2
  22. package/dist/exports/io.mjs.map +1 -0
  23. package/dist/exports/metadata.d.mts +2 -0
  24. package/dist/exports/metadata.mjs +1 -0
  25. package/dist/exports/migration-ts.mjs +1 -1
  26. package/dist/exports/migration.d.mts +13 -10
  27. package/dist/exports/migration.d.mts.map +1 -1
  28. package/dist/exports/migration.mjs +20 -21
  29. package/dist/exports/migration.mjs.map +1 -1
  30. package/dist/exports/package.d.mts +2 -0
  31. package/dist/exports/package.mjs +1 -0
  32. package/dist/exports/refs.d.mts +11 -5
  33. package/dist/exports/refs.d.mts.map +1 -1
  34. package/dist/exports/refs.mjs +106 -30
  35. package/dist/exports/refs.mjs.map +1 -1
  36. package/dist/graph-HiqjZROg.d.mts +22 -0
  37. package/dist/graph-HiqjZROg.d.mts.map +1 -0
  38. package/dist/hash-BNWumjn7.mjs +76 -0
  39. package/dist/hash-BNWumjn7.mjs.map +1 -0
  40. package/dist/metadata-DDa5L-uD.d.mts +45 -0
  41. package/dist/metadata-DDa5L-uD.d.mts.map +1 -0
  42. package/dist/package-BJ5KAEcD.d.mts +21 -0
  43. package/dist/package-BJ5KAEcD.d.mts.map +1 -0
  44. package/package.json +23 -11
  45. package/src/dag.ts +19 -18
  46. package/src/errors.ts +57 -15
  47. package/src/exports/errors.ts +1 -0
  48. package/src/exports/graph.ts +1 -0
  49. package/src/exports/hash.ts +2 -0
  50. package/src/exports/io.ts +1 -1
  51. package/src/exports/metadata.ts +1 -0
  52. package/src/exports/package.ts +1 -0
  53. package/src/exports/refs.ts +10 -2
  54. package/src/graph.ts +19 -0
  55. package/src/hash.ts +91 -0
  56. package/src/io.ts +32 -20
  57. package/src/metadata.ts +36 -0
  58. package/src/migration-base.ts +32 -28
  59. package/src/package.ts +18 -0
  60. package/src/refs.ts +148 -37
  61. package/dist/attestation-DtF8tEOM.mjs +0 -65
  62. package/dist/attestation-DtF8tEOM.mjs.map +0 -1
  63. package/dist/errors-BKbRGCJM.mjs.map +0 -1
  64. package/dist/exports/attestation.d.mts +0 -37
  65. package/dist/exports/attestation.d.mts.map +0 -1
  66. package/dist/exports/attestation.mjs +0 -4
  67. package/dist/exports/types.d.mts.map +0 -1
  68. package/dist/exports/types.mjs +0 -3
  69. package/dist/io-CCnYsUHU.mjs +0 -153
  70. package/dist/io-CCnYsUHU.mjs.map +0 -1
  71. package/dist/types-DyGXcWWp.d.mts +0 -71
  72. package/dist/types-DyGXcWWp.d.mts.map +0 -1
  73. package/src/attestation.ts +0 -81
  74. package/src/exports/attestation.ts +0 -2
  75. package/src/exports/types.ts +0 -10
  76. package/src/types.ts +0 -66
package/src/refs.ts CHANGED
@@ -1,14 +1,19 @@
1
- import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
1
+ import { mkdir, readdir, readFile, rename, rmdir, unlink, writeFile } from 'node:fs/promises';
2
2
  import { type } from 'arktype';
3
- import { dirname, join } from 'pathe';
3
+ import { dirname, join, relative } from 'pathe';
4
4
  import {
5
+ errorInvalidRefFile,
5
6
  errorInvalidRefName,
6
- errorInvalidRefs,
7
7
  errorInvalidRefValue,
8
8
  MigrationToolsError,
9
9
  } from './errors';
10
10
 
11
- export type Refs = Readonly<Record<string, string>>;
11
+ export interface RefEntry {
12
+ readonly hash: string;
13
+ readonly invariants: readonly string[];
14
+ }
15
+
16
+ export type Refs = Readonly<Record<string, RefEntry>>;
12
17
 
13
18
  const REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;
14
19
  const REF_VALUE_PATTERN = /^sha256:(empty|[0-9a-f]{64})$/;
@@ -25,22 +30,40 @@ export function validateRefValue(value: string): boolean {
25
30
  return REF_VALUE_PATTERN.test(value);
26
31
  }
27
32
 
28
- const RefsSchema = type('Record<string, string>').narrow((refs, ctx) => {
29
- for (const [key, value] of Object.entries(refs)) {
30
- if (!validateRefName(key)) return ctx.mustBe(`valid ref names (invalid: "${key}")`);
31
- if (!validateRefValue(value))
32
- return ctx.mustBe(`valid contract hashes (invalid value for "${key}": "${value}")`);
33
- }
33
+ const RefEntrySchema = type({
34
+ hash: 'string',
35
+ invariants: 'string[]',
36
+ }).narrow((entry, ctx) => {
37
+ if (!validateRefValue(entry.hash))
38
+ return ctx.mustBe(`a valid contract hash (got "${entry.hash}")`);
34
39
  return true;
35
40
  });
36
41
 
37
- export async function readRefs(refsPath: string): Promise<Refs> {
42
+ function refFilePath(refsDir: string, name: string): string {
43
+ return join(refsDir, `${name}.json`);
44
+ }
45
+
46
+ function refNameFromPath(refsDir: string, filePath: string): string {
47
+ const rel = relative(refsDir, filePath);
48
+ return rel.replace(/\.json$/, '');
49
+ }
50
+
51
+ export async function readRef(refsDir: string, name: string): Promise<RefEntry> {
52
+ if (!validateRefName(name)) {
53
+ throw errorInvalidRefName(name);
54
+ }
55
+
56
+ const filePath = refFilePath(refsDir, name);
38
57
  let raw: string;
39
58
  try {
40
- raw = await readFile(refsPath, 'utf-8');
59
+ raw = await readFile(filePath, 'utf-8');
41
60
  } catch (error) {
42
61
  if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
43
- return {};
62
+ throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref "${name}"`, {
63
+ why: `No ref file found at "${filePath}".`,
64
+ fix: `Create the ref with: prisma-next migration ref set ${name} <hash>`,
65
+ details: { refName: name, filePath },
66
+ });
44
67
  }
45
68
  throw error;
46
69
  }
@@ -49,54 +72,142 @@ export async function readRefs(refsPath: string): Promise<Refs> {
49
72
  try {
50
73
  parsed = JSON.parse(raw);
51
74
  } catch {
52
- throw errorInvalidRefs(refsPath, 'Failed to parse as JSON');
75
+ throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');
53
76
  }
54
77
 
55
- const result = RefsSchema(parsed);
78
+ const result = RefEntrySchema(parsed);
56
79
  if (result instanceof type.errors) {
57
- throw errorInvalidRefs(refsPath, result.summary);
80
+ throw errorInvalidRefFile(filePath, result.summary);
58
81
  }
59
82
 
60
83
  return result;
61
84
  }
62
85
 
63
- export async function writeRefs(refsPath: string, refs: Refs): Promise<void> {
64
- for (const [key, value] of Object.entries(refs)) {
65
- if (!validateRefName(key)) {
66
- throw errorInvalidRefName(key);
86
+ export async function readRefs(refsDir: string): Promise<Refs> {
87
+ let entries: string[];
88
+ try {
89
+ entries = await readdir(refsDir, { recursive: true, encoding: 'utf-8' });
90
+ } catch (error) {
91
+ if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
92
+ return {};
93
+ }
94
+ throw error;
95
+ }
96
+
97
+ const jsonFiles = entries.filter((entry) => entry.endsWith('.json'));
98
+ const refs: Record<string, RefEntry> = {};
99
+
100
+ for (const jsonFile of jsonFiles) {
101
+ const filePath = join(refsDir, jsonFile);
102
+ const name = refNameFromPath(refsDir, filePath);
103
+
104
+ let raw: string;
105
+ try {
106
+ raw = await readFile(filePath, 'utf-8');
107
+ } catch (error) {
108
+ // Tolerate the TOCTOU race between `readdir` and `readFile` (ENOENT) and
109
+ // benign EISDIR if a directory happens to end in `.json`. Anything else
110
+ // (EACCES, EIO, EMFILE, …) is a real failure and propagates so the CLI
111
+ // surfaces it rather than silently dropping the ref.
112
+ const code = error instanceof Error ? (error as { code?: string }).code : undefined;
113
+ if (code === 'ENOENT' || code === 'EISDIR') {
114
+ continue;
115
+ }
116
+ throw error;
117
+ }
118
+
119
+ let parsed: unknown;
120
+ try {
121
+ parsed = JSON.parse(raw);
122
+ } catch {
123
+ throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');
67
124
  }
68
- if (!validateRefValue(value)) {
69
- throw errorInvalidRefValue(value);
125
+
126
+ const result = RefEntrySchema(parsed);
127
+ if (result instanceof type.errors) {
128
+ throw errorInvalidRefFile(filePath, result.summary);
70
129
  }
130
+
131
+ refs[name] = result;
71
132
  }
72
133
 
73
- const sorted = Object.fromEntries(Object.entries(refs).sort(([a], [b]) => a.localeCompare(b)));
134
+ return refs;
135
+ }
136
+
137
+ export async function writeRef(refsDir: string, name: string, entry: RefEntry): Promise<void> {
138
+ if (!validateRefName(name)) {
139
+ throw errorInvalidRefName(name);
140
+ }
141
+ if (!validateRefValue(entry.hash)) {
142
+ throw errorInvalidRefValue(entry.hash);
143
+ }
74
144
 
75
- const dir = dirname(refsPath);
145
+ const filePath = refFilePath(refsDir, name);
146
+ const dir = dirname(filePath);
76
147
  await mkdir(dir, { recursive: true });
77
148
 
78
- const tmpPath = join(dir, `.refs.json.${Date.now()}.tmp`);
79
- await writeFile(tmpPath, `${JSON.stringify(sorted, null, 2)}\n`);
80
- await rename(tmpPath, refsPath);
149
+ const tmpPath = join(dir, `.${name.split('/').pop()}.json.${Date.now()}.tmp`);
150
+ await writeFile(
151
+ tmpPath,
152
+ `${JSON.stringify({ hash: entry.hash, invariants: [...entry.invariants] }, null, 2)}\n`,
153
+ );
154
+ await rename(tmpPath, filePath);
81
155
  }
82
156
 
83
- export function resolveRef(refs: Refs, name: string): string {
157
+ export async function deleteRef(refsDir: string, name: string): Promise<void> {
84
158
  if (!validateRefName(name)) {
85
159
  throw errorInvalidRefName(name);
86
160
  }
87
161
 
88
- const hash = refs[name];
89
- if (hash === undefined) {
162
+ const filePath = refFilePath(refsDir, name);
163
+ try {
164
+ await unlink(filePath);
165
+ } catch (error) {
166
+ if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
167
+ throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref "${name}"`, {
168
+ why: `No ref file found at "${filePath}".`,
169
+ fix: 'Run `prisma-next migration ref list` to see available refs.',
170
+ details: { refName: name, filePath },
171
+ });
172
+ }
173
+ throw error;
174
+ }
175
+
176
+ // Clean empty parent directories up to refsDir. Stop walking on the expected
177
+ // "directory has siblings" signal (ENOTEMPTY on Linux, EEXIST on some BSDs)
178
+ // and on ENOENT (concurrent removal). Anything else (EACCES, EIO, …) is a
179
+ // real failure and propagates.
180
+ let dir = dirname(filePath);
181
+ while (dir !== refsDir && dir.startsWith(refsDir)) {
182
+ try {
183
+ await rmdir(dir);
184
+ dir = dirname(dir);
185
+ } catch (error) {
186
+ const code = error instanceof Error ? (error as { code?: string }).code : undefined;
187
+ if (code === 'ENOTEMPTY' || code === 'EEXIST' || code === 'ENOENT') {
188
+ break;
189
+ }
190
+ throw error;
191
+ }
192
+ }
193
+ }
194
+
195
+ export function resolveRef(refs: Refs, name: string): RefEntry {
196
+ if (!validateRefName(name)) {
197
+ throw errorInvalidRefName(name);
198
+ }
199
+
200
+ // Object.hasOwn gate: plain-object `refs` would otherwise let
201
+ // `refs['constructor']` return Object.prototype.constructor and bypass the
202
+ // UNKNOWN_REF throw. validateRefName accepts `"constructor"` as a name shape.
203
+ if (!Object.hasOwn(refs, name)) {
90
204
  throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref "${name}"`, {
91
- why: `No ref named "${name}" exists in refs.json.`,
92
- fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: set the "${name}" key in migrations/refs.json.`,
205
+ why: `No ref named "${name}" exists.`,
206
+ fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: prisma-next migration ref set ${name} <hash>`,
93
207
  details: { refName: name, availableRefs: Object.keys(refs) },
94
208
  });
95
209
  }
96
210
 
97
- if (!validateRefValue(hash)) {
98
- throw errorInvalidRefValue(hash);
99
- }
100
-
101
- return hash;
211
+ // biome-ignore lint/style/noNonNullAssertion: Object.hasOwn gate above guarantees this is defined
212
+ return refs[name]!;
102
213
  }
@@ -1,65 +0,0 @@
1
- import { r as readMigrationPackage } from "./io-CCnYsUHU.mjs";
2
- import { createHash } from "node:crypto";
3
-
4
- //#region src/canonicalize-json.ts
5
- function sortKeys(value) {
6
- if (value === null || typeof value !== "object") return value;
7
- if (Array.isArray(value)) return value.map(sortKeys);
8
- const sorted = {};
9
- for (const key of Object.keys(value).sort()) sorted[key] = sortKeys(value[key]);
10
- return sorted;
11
- }
12
- function canonicalizeJson(value) {
13
- return JSON.stringify(sortKeys(value));
14
- }
15
-
16
- //#endregion
17
- //#region src/attestation.ts
18
- function sha256Hex(input) {
19
- return createHash("sha256").update(input).digest("hex");
20
- }
21
- /**
22
- * Content-addressed migration identity over (manifest envelope sans
23
- * contracts/hints, ops). See ADR 199 "Storage-only migration identity"
24
- * for the rationale: contracts are anchored separately by the
25
- * storage-hash bookends inside the envelope; planner hints are advisory
26
- * and must not affect identity.
27
- *
28
- * The `migrationId` field on the manifest is stripped before hashing so
29
- * the function can be used both at write time (when no id exists yet)
30
- * and at verify time (rehashing an already-attested manifest).
31
- */
32
- function computeMigrationId(manifest, ops) {
33
- const { migrationId: _migrationId, signature: _signature, fromContract: _fromContract, toContract: _toContract, hints: _hints, ...strippedMeta } = manifest;
34
- return `sha256:${sha256Hex(canonicalizeJson([canonicalizeJson(strippedMeta), canonicalizeJson(ops)].map(sha256Hex)))}`;
35
- }
36
- /**
37
- * Re-hash an on-disk migration bundle and compare against the stored
38
- * `migrationId`. Returns `{ ok: true }` when the package is internally
39
- * consistent (manifest + ops still produce the recorded id), or
40
- * `{ ok: false, reason: 'mismatch', stored, computed }` when they do
41
- * not — typically a sign of FS corruption, partial writes, or a
42
- * post-emit hand edit.
43
- */
44
- function verifyMigrationBundle(bundle) {
45
- const computed = computeMigrationId(bundle.manifest, bundle.ops);
46
- if (bundle.manifest.migrationId === computed) return {
47
- ok: true,
48
- storedMigrationId: bundle.manifest.migrationId,
49
- computedMigrationId: computed
50
- };
51
- return {
52
- ok: false,
53
- reason: "mismatch",
54
- storedMigrationId: bundle.manifest.migrationId,
55
- computedMigrationId: computed
56
- };
57
- }
58
- /** Convenience wrapper: read the package from disk then verify it. */
59
- async function verifyMigration(dir) {
60
- return verifyMigrationBundle(await readMigrationPackage(dir));
61
- }
62
-
63
- //#endregion
64
- export { verifyMigration as n, verifyMigrationBundle as r, computeMigrationId as t };
65
- //# sourceMappingURL=attestation-DtF8tEOM.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"attestation-DtF8tEOM.mjs","names":["sorted: Record<string, unknown>"],"sources":["../src/canonicalize-json.ts","../src/attestation.ts"],"sourcesContent":["function sortKeys(value: unknown): unknown {\n if (value === null || typeof value !== 'object') {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(sortKeys);\n }\n const sorted: Record<string, unknown> = {};\n for (const key of Object.keys(value).sort()) {\n sorted[key] = sortKeys((value as Record<string, unknown>)[key]);\n }\n return sorted;\n}\n\nexport function canonicalizeJson(value: unknown): string {\n return JSON.stringify(sortKeys(value));\n}\n","import { createHash } from 'node:crypto';\nimport { canonicalizeJson } from './canonicalize-json';\nimport { readMigrationPackage } from './io';\nimport type { MigrationBundle, MigrationManifest, MigrationOps } from './types';\n\nexport interface VerifyResult {\n readonly ok: boolean;\n readonly reason?: 'mismatch';\n readonly storedMigrationId?: string;\n readonly computedMigrationId?: string;\n}\n\nfunction sha256Hex(input: string): string {\n return createHash('sha256').update(input).digest('hex');\n}\n\n/**\n * Content-addressed migration identity over (manifest envelope sans\n * contracts/hints, ops). See ADR 199 \"Storage-only migration identity\"\n * for the rationale: contracts are anchored separately by the\n * storage-hash bookends inside the envelope; planner hints are advisory\n * and must not affect identity.\n *\n * The `migrationId` field on the manifest is stripped before hashing so\n * the function can be used both at write time (when no id exists yet)\n * and at verify time (rehashing an already-attested manifest).\n */\nexport function computeMigrationId(\n manifest: Omit<MigrationManifest, 'migrationId'> & { readonly migrationId?: string },\n ops: MigrationOps,\n): string {\n const {\n migrationId: _migrationId,\n signature: _signature,\n fromContract: _fromContract,\n toContract: _toContract,\n hints: _hints,\n ...strippedMeta\n } = manifest;\n\n const canonicalManifest = canonicalizeJson(strippedMeta);\n const canonicalOps = canonicalizeJson(ops);\n\n const partHashes = [canonicalManifest, canonicalOps].map(sha256Hex);\n const hash = sha256Hex(canonicalizeJson(partHashes));\n\n return `sha256:${hash}`;\n}\n\n/**\n * Re-hash an on-disk migration bundle and compare against the stored\n * `migrationId`. Returns `{ ok: true }` when the package is internally\n * consistent (manifest + ops still produce the recorded id), or\n * `{ ok: false, reason: 'mismatch', stored, computed }` when they do\n * not — typically a sign of FS corruption, partial writes, or a\n * post-emit hand edit.\n */\nexport function verifyMigrationBundle(bundle: MigrationBundle): VerifyResult {\n const computed = computeMigrationId(bundle.manifest, bundle.ops);\n\n if (bundle.manifest.migrationId === computed) {\n return {\n ok: true,\n storedMigrationId: bundle.manifest.migrationId,\n computedMigrationId: computed,\n };\n }\n\n return {\n ok: false,\n reason: 'mismatch',\n storedMigrationId: bundle.manifest.migrationId,\n computedMigrationId: computed,\n };\n}\n\n/** Convenience wrapper: read the package from disk then verify it. */\nexport async function verifyMigration(dir: string): Promise<VerifyResult> {\n const pkg = await readMigrationPackage(dir);\n return verifyMigrationBundle(pkg);\n}\n"],"mappings":";;;;AAAA,SAAS,SAAS,OAAyB;AACzC,KAAI,UAAU,QAAQ,OAAO,UAAU,SACrC,QAAO;AAET,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,IAAI,SAAS;CAE5B,MAAMA,SAAkC,EAAE;AAC1C,MAAK,MAAM,OAAO,OAAO,KAAK,MAAM,CAAC,MAAM,CACzC,QAAO,OAAO,SAAU,MAAkC,KAAK;AAEjE,QAAO;;AAGT,SAAgB,iBAAiB,OAAwB;AACvD,QAAO,KAAK,UAAU,SAAS,MAAM,CAAC;;;;;ACHxC,SAAS,UAAU,OAAuB;AACxC,QAAO,WAAW,SAAS,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;;;;;;;;;;;;;AAczD,SAAgB,mBACd,UACA,KACQ;CACR,MAAM,EACJ,aAAa,cACb,WAAW,YACX,cAAc,eACd,YAAY,aACZ,OAAO,QACP,GAAG,iBACD;AAQJ,QAAO,UAFM,UAAU,iBADJ,CAHO,iBAAiB,aAAa,EACnC,iBAAiB,IAAI,CAEU,CAAC,IAAI,UAAU,CAChB,CAAC;;;;;;;;;;AAatD,SAAgB,sBAAsB,QAAuC;CAC3E,MAAM,WAAW,mBAAmB,OAAO,UAAU,OAAO,IAAI;AAEhE,KAAI,OAAO,SAAS,gBAAgB,SAClC,QAAO;EACL,IAAI;EACJ,mBAAmB,OAAO,SAAS;EACnC,qBAAqB;EACtB;AAGH,QAAO;EACL,IAAI;EACJ,QAAQ;EACR,mBAAmB,OAAO,SAAS;EACnC,qBAAqB;EACtB;;;AAIH,eAAsB,gBAAgB,KAAoC;AAExE,QAAO,sBADK,MAAM,qBAAqB,IAAI,CACV"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors-BKbRGCJM.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Structured error for migration tooling operations.\n *\n * Follows the NAMESPACE.SUBCODE convention from ADR 027. All codes live under\n * the MIGRATION namespace. These are tooling-time errors (file I/O, attestation,\n * migration history reconstruction), distinct from the runtime MIGRATION.* codes for apply-time\n * failures (PRECHECK_FAILED, POSTCHECK_FAILED, etc.).\n *\n * Fields:\n * - code: Stable machine-readable code (MIGRATION.SUBCODE)\n * - category: Always 'MIGRATION'\n * - why: Explains the cause in plain language\n * - fix: Actionable remediation step\n * - details: Machine-readable structured data for agents\n */\nexport class MigrationToolsError extends Error {\n readonly code: string;\n readonly category = 'MIGRATION' as const;\n readonly why: string;\n readonly fix: string;\n readonly details: Record<string, unknown> | undefined;\n\n constructor(\n code: string,\n summary: string,\n options: {\n readonly why: string;\n readonly fix: string;\n readonly details?: Record<string, unknown>;\n },\n ) {\n super(summary);\n this.name = 'MigrationToolsError';\n this.code = code;\n this.why = options.why;\n this.fix = options.fix;\n this.details = options.details;\n }\n\n static is(error: unknown): error is MigrationToolsError {\n if (!(error instanceof Error)) return false;\n const candidate = error as MigrationToolsError;\n return candidate.name === 'MigrationToolsError' && typeof candidate.code === 'string';\n }\n}\n\nexport function errorDirectoryExists(dir: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.DIR_EXISTS', 'Migration directory already exists', {\n why: `The directory \"${dir}\" already exists. Each migration must have a unique directory.`,\n fix: 'Use --name to pick a different name, or delete the existing directory and re-run.',\n details: { dir },\n });\n}\n\nexport function errorMissingFile(file: string, dir: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.FILE_MISSING', `Missing ${file}`, {\n why: `Expected \"${file}\" in \"${dir}\" but the file does not exist.`,\n fix: 'Ensure the migration directory contains both migration.json and ops.json. If the directory is corrupt, delete it and re-run migration plan.',\n details: { file, dir },\n });\n}\n\nexport function errorInvalidJson(filePath: string, parseError: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_JSON', 'Invalid JSON in migration file', {\n why: `Failed to parse \"${filePath}\": ${parseError}`,\n fix: 'Fix the JSON syntax error, or delete the migration directory and re-run migration plan.',\n details: { filePath, parseError },\n });\n}\n\nexport function errorInvalidManifest(filePath: string, reason: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_MANIFEST', 'Invalid migration manifest', {\n why: `Manifest at \"${filePath}\" is invalid: ${reason}`,\n fix: 'Ensure the manifest has all required fields (from, to, kind, toContract). If corrupt, delete and re-plan.',\n details: { filePath, reason },\n });\n}\n\nexport function errorInvalidSlug(slug: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_NAME', 'Invalid migration name', {\n why: `The slug \"${slug}\" contains no valid characters after sanitization (only a-z, 0-9 are kept).`,\n fix: 'Provide a name with at least one alphanumeric character, e.g. --name add_users.',\n details: { slug },\n });\n}\n\nexport function errorInvalidDestName(destName: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_DEST_NAME', 'Invalid copy destination name', {\n why: `The destination name \"${destName}\" must be a single path segment (no \"..\" or directory separators).`,\n fix: 'Use a simple file name such as \"contract.json\" for each destination in the copy list.',\n details: { destName },\n });\n}\n\nexport function errorSameSourceAndTarget(dirName: string, hash: string): MigrationToolsError {\n return new MigrationToolsError(\n 'MIGRATION.SAME_SOURCE_AND_TARGET',\n 'Migration has same source and target',\n {\n why: `Migration \"${dirName}\" has from === to === \"${hash}\". A migration must transition between two different contract states.`,\n fix: 'Delete the invalid migration directory and re-run migration plan.',\n details: { dirName, hash },\n },\n );\n}\n\nexport function errorAmbiguousTarget(\n branchTips: readonly string[],\n context?: {\n divergencePoint: string;\n branches: readonly {\n tip: string;\n edges: readonly { dirName: string; from: string; to: string }[];\n }[];\n },\n): MigrationToolsError {\n const divergenceInfo = context\n ? `\\nDivergence point: ${context.divergencePoint}\\nBranches:\\n${context.branches.map((b) => ` → ${b.tip} (${b.edges.length} edge(s): ${b.edges.map((e) => e.dirName).join(' → ') || 'direct'})`).join('\\n')}`\n : '';\n return new MigrationToolsError('MIGRATION.AMBIGUOUS_TARGET', 'Ambiguous migration target', {\n why: `The migration history has diverged into multiple branches: ${branchTips.join(', ')}. This typically happens when two developers plan migrations from the same starting point.${divergenceInfo}`,\n fix: 'Use `migration ref set <name> <hash>` to target a specific branch, delete one of the conflicting migration directories and re-run `migration plan`, or use --from <hash> to explicitly select a starting point.',\n details: {\n branchTips,\n ...(context ? { divergencePoint: context.divergencePoint, branches: context.branches } : {}),\n },\n });\n}\n\nexport function errorNoInitialMigration(nodes: readonly string[]): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.NO_INITIAL_MIGRATION', 'No initial migration found', {\n why: `No migration starts from the empty contract state (known hashes: ${nodes.join(', ')}). At least one migration must originate from the empty state.`,\n fix: 'Inspect the migrations directory for corrupted migration.json files. At least one migration must start from the empty contract hash.',\n details: { nodes },\n });\n}\n\nexport function errorInvalidRefs(refsPath: string, reason: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REFS', 'Invalid refs.json', {\n why: `refs.json at \"${refsPath}\" is invalid: ${reason}`,\n fix: 'Ensure refs.json is a flat object mapping valid ref names to contract hash strings.',\n details: { path: refsPath, reason },\n });\n}\n\nexport function errorInvalidRefFile(filePath: string, reason: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REF_FILE', 'Invalid ref file', {\n why: `Ref file at \"${filePath}\" is invalid: ${reason}`,\n fix: 'Ensure the ref file contains valid JSON with { \"hash\": \"sha256:<64 hex chars>\", \"invariants\": [\"...\"] }.',\n details: { path: filePath, reason },\n });\n}\n\nexport function errorInvalidRefName(refName: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REF_NAME', 'Invalid ref name', {\n why: `Ref name \"${refName}\" is invalid. Names must be lowercase alphanumeric with hyphens or forward slashes (no \".\" or \"..\" segments).`,\n fix: `Use a valid ref name (e.g., \"staging\", \"envs/production\").`,\n details: { refName },\n });\n}\n\nexport function errorNoTarget(reachableHashes: readonly string[]): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.NO_TARGET', 'No migration target could be resolved', {\n why: `The migration history contains cycles and no target can be resolved automatically (reachable hashes: ${reachableHashes.join(', ')}). This typically happens after rollback migrations (e.g., C1→C2→C1).`,\n fix: 'Use --from <hash> to specify the planning origin explicitly.',\n details: { reachableHashes },\n });\n}\n\nexport function errorInvalidRefValue(value: string): MigrationToolsError {\n return new MigrationToolsError('MIGRATION.INVALID_REF_VALUE', 'Invalid ref value', {\n why: `Ref value \"${value}\" is not a valid contract hash. Values must be in the format \"sha256:<64 hex chars>\" or \"sha256:empty\".`,\n fix: 'Use a valid storage hash from `prisma-next contract emit` output or an existing migration.',\n details: { value },\n });\n}\n\nexport function errorDuplicateMigrationId(migrationId: string): MigrationToolsError {\n return new MigrationToolsError(\n 'MIGRATION.DUPLICATE_MIGRATION_ID',\n 'Duplicate migrationId in migration graph',\n {\n why: `Multiple migrations share migrationId \"${migrationId}\". Each migration must have a unique content-addressed identity.`,\n fix: 'Regenerate one of the conflicting migrations so each migrationId is unique, then re-run migration commands.',\n details: { migrationId },\n },\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAeA,IAAa,sBAAb,cAAyC,MAAM;CAC7C,AAAS;CACT,AAAS,WAAW;CACpB,AAAS;CACT,AAAS;CACT,AAAS;CAET,YACE,MACA,SACA,SAKA;AACA,QAAM,QAAQ;AACd,OAAK,OAAO;AACZ,OAAK,OAAO;AACZ,OAAK,MAAM,QAAQ;AACnB,OAAK,MAAM,QAAQ;AACnB,OAAK,UAAU,QAAQ;;CAGzB,OAAO,GAAG,OAA8C;AACtD,MAAI,EAAE,iBAAiB,OAAQ,QAAO;EACtC,MAAM,YAAY;AAClB,SAAO,UAAU,SAAS,yBAAyB,OAAO,UAAU,SAAS;;;AAIjF,SAAgB,qBAAqB,KAAkC;AACrE,QAAO,IAAI,oBAAoB,wBAAwB,sCAAsC;EAC3F,KAAK,kBAAkB,IAAI;EAC3B,KAAK;EACL,SAAS,EAAE,KAAK;EACjB,CAAC;;AAGJ,SAAgB,iBAAiB,MAAc,KAAkC;AAC/E,QAAO,IAAI,oBAAoB,0BAA0B,WAAW,QAAQ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AAK,KAAK,CAAC,4FAA4F;EACrL,KAAK;EACL,SAAS;GACP;GACA,GAAI,UAAU;IAAE,iBAAiB,QAAQ;IAAiB,UAAU,QAAQ;IAAU,GAAG,EAAE;GAC5F;EACF,CAAC;;AAGJ,SAAgB,wBAAwB,OAA+C;AACrF,QAAO,IAAI,oBAAoB,kCAAkC,8BAA8B;EAC7F,KAAK,oEAAoE,MAAM,KAAK,KAAK,CAAC;EAC1F,KAAK;EACL,SAAS,EAAE,OAAO;EACnB,CAAC;;AAGJ,SAAgB,iBAAiB,UAAkB,QAAqC;AACtF,QAAO,IAAI,oBAAoB,0BAA0B,qBAAqB;EAC5E,KAAK,iBAAiB,SAAS,gBAAgB;EAC/C,KAAK;EACL,SAAS;GAAE,MAAM;GAAU;GAAQ;EACpC,CAAC;;AAWJ,SAAgB,oBAAoB,SAAsC;AACxE,QAAO,IAAI,oBAAoB,8BAA8B,oBAAoB;EAC/E,KAAK,aAAa,QAAQ;EAC1B,KAAK;EACL,SAAS,EAAE,SAAS;EACrB,CAAC;;AAGJ,SAAgB,cAAc,iBAAyD;AACrF,QAAO,IAAI,oBAAoB,uBAAuB,yCAAyC;EAC7F,KAAK,wGAAwG,gBAAgB,KAAK,KAAK,CAAC;EACxI,KAAK;EACL,SAAS,EAAE,iBAAiB;EAC7B,CAAC;;AAGJ,SAAgB,qBAAqB,OAAoC;AACvE,QAAO,IAAI,oBAAoB,+BAA+B,qBAAqB;EACjF,KAAK,cAAc,MAAM;EACzB,KAAK;EACL,SAAS,EAAE,OAAO;EACnB,CAAC;;AAGJ,SAAgB,0BAA0B,aAA0C;AAClF,QAAO,IAAI,oBACT,oCACA,4CACA;EACE,KAAK,0CAA0C,YAAY;EAC3D,KAAK;EACL,SAAS,EAAE,aAAa;EACzB,CACF"}
@@ -1,37 +0,0 @@
1
- import { a as MigrationManifest, o as MigrationOps, t as MigrationBundle } from "../types-DyGXcWWp.mjs";
2
-
3
- //#region src/attestation.d.ts
4
- interface VerifyResult {
5
- readonly ok: boolean;
6
- readonly reason?: 'mismatch';
7
- readonly storedMigrationId?: string;
8
- readonly computedMigrationId?: string;
9
- }
10
- /**
11
- * Content-addressed migration identity over (manifest envelope sans
12
- * contracts/hints, ops). See ADR 199 "Storage-only migration identity"
13
- * for the rationale: contracts are anchored separately by the
14
- * storage-hash bookends inside the envelope; planner hints are advisory
15
- * and must not affect identity.
16
- *
17
- * The `migrationId` field on the manifest is stripped before hashing so
18
- * the function can be used both at write time (when no id exists yet)
19
- * and at verify time (rehashing an already-attested manifest).
20
- */
21
- declare function computeMigrationId(manifest: Omit<MigrationManifest, 'migrationId'> & {
22
- readonly migrationId?: string;
23
- }, ops: MigrationOps): string;
24
- /**
25
- * Re-hash an on-disk migration bundle and compare against the stored
26
- * `migrationId`. Returns `{ ok: true }` when the package is internally
27
- * consistent (manifest + ops still produce the recorded id), or
28
- * `{ ok: false, reason: 'mismatch', stored, computed }` when they do
29
- * not — typically a sign of FS corruption, partial writes, or a
30
- * post-emit hand edit.
31
- */
32
- declare function verifyMigrationBundle(bundle: MigrationBundle): VerifyResult;
33
- /** Convenience wrapper: read the package from disk then verify it. */
34
- declare function verifyMigration(dir: string): Promise<VerifyResult>;
35
- //#endregion
36
- export { type VerifyResult, computeMigrationId, verifyMigration, verifyMigrationBundle };
37
- //# sourceMappingURL=attestation.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"attestation.d.mts","names":[],"sources":["../../src/attestation.ts"],"sourcesContent":[],"mappings":";;;UAKiB,YAAA;;EAAA,SAAA,MAAY,CAAA,EAAA,UAAA;EAsBb,SAAA,iBAAkB,CAAA,EAAA,MAAA;EACjB,SAAA,mBAAA,CAAA,EAAA,MAAA;;;;AA6BjB;AAoBA;;;;;;;;iBAlDgB,kBAAA,WACJ,KAAK;;QACV;;;;;;;;;iBA4BS,qBAAA,SAA8B,kBAAkB;;iBAoB1C,eAAA,eAA8B,QAAQ"}
@@ -1,4 +0,0 @@
1
- import "../io-CCnYsUHU.mjs";
2
- import { n as verifyMigration, r as verifyMigrationBundle, t as computeMigrationId } from "../attestation-DtF8tEOM.mjs";
3
-
4
- export { computeMigrationId, verifyMigration, verifyMigrationBundle };
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.mts","names":[],"sources":["../../src/errors.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;;;;;;;;;;;;cAAa,mBAAA,SAA4B,KAAA;;;;;oBAKrB;;;;uBAQK;;sCAWa"}
@@ -1,3 +0,0 @@
1
- import { t as MigrationToolsError } from "../errors-BKbRGCJM.mjs";
2
-
3
- export { MigrationToolsError };
@@ -1,153 +0,0 @@
1
- import { a as errorInvalidDestName, d as errorInvalidSlug, f as errorMissingFile, o as errorInvalidJson, r as errorDirectoryExists, s as errorInvalidManifest } from "./errors-BKbRGCJM.mjs";
2
- import { copyFile, mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
3
- import { type } from "arktype";
4
- import { basename, dirname, join } from "pathe";
5
-
6
- //#region src/io.ts
7
- const MANIFEST_FILE = "migration.json";
8
- const OPS_FILE = "ops.json";
9
- const MAX_SLUG_LENGTH = 64;
10
- function hasErrnoCode(error, code) {
11
- return error instanceof Error && error.code === code;
12
- }
13
- const MigrationManifestSchema = type({
14
- from: "string",
15
- to: "string",
16
- migrationId: "string",
17
- kind: "'regular' | 'baseline'",
18
- fromContract: "object | null",
19
- toContract: "object",
20
- hints: type({
21
- used: "string[]",
22
- applied: "string[]",
23
- plannerVersion: "string"
24
- }),
25
- labels: "string[]",
26
- "authorship?": type({
27
- "author?": "string",
28
- "email?": "string"
29
- }),
30
- "signature?": type({
31
- keyId: "string",
32
- value: "string"
33
- }).or("null"),
34
- createdAt: "string"
35
- });
36
- const MigrationOpsSchema = type({
37
- id: "string",
38
- label: "string",
39
- operationClass: "'additive' | 'widening' | 'destructive' | 'data'"
40
- }).array();
41
- async function writeMigrationPackage(dir, manifest, ops) {
42
- await mkdir(dirname(dir), { recursive: true });
43
- try {
44
- await mkdir(dir);
45
- } catch (error) {
46
- if (hasErrnoCode(error, "EEXIST")) throw errorDirectoryExists(dir);
47
- throw error;
48
- }
49
- await writeFile(join(dir, MANIFEST_FILE), JSON.stringify(manifest, null, 2), { flag: "wx" });
50
- await writeFile(join(dir, OPS_FILE), JSON.stringify(ops, null, 2), { flag: "wx" });
51
- }
52
- /**
53
- * Copy a list of files into `destDir`, optionally renaming each one.
54
- *
55
- * The destination directory is created (with `recursive: true`) if it
56
- * does not already exist. Each source path is copied byte-for-byte into
57
- * `destDir/<destName>`; missing sources throw `ENOENT`. The helper is
58
- * intentionally generic: callers own the list of files (e.g. a contract
59
- * emitter's emitted output) and the naming convention (e.g. renaming
60
- * the destination contract to `end-contract.*` and the source contract
61
- * to `start-contract.*`).
62
- */
63
- async function copyFilesWithRename(destDir, files) {
64
- await mkdir(destDir, { recursive: true });
65
- for (const file of files) {
66
- if (basename(file.destName) !== file.destName) throw errorInvalidDestName(file.destName);
67
- await copyFile(file.sourcePath, join(destDir, file.destName));
68
- }
69
- }
70
- async function writeMigrationManifest(dir, manifest) {
71
- await writeFile(join(dir, MANIFEST_FILE), `${JSON.stringify(manifest, null, 2)}\n`);
72
- }
73
- async function writeMigrationOps(dir, ops) {
74
- await writeFile(join(dir, OPS_FILE), `${JSON.stringify(ops, null, 2)}\n`);
75
- }
76
- async function readMigrationPackage(dir) {
77
- const manifestPath = join(dir, MANIFEST_FILE);
78
- const opsPath = join(dir, OPS_FILE);
79
- let manifestRaw;
80
- try {
81
- manifestRaw = await readFile(manifestPath, "utf-8");
82
- } catch (error) {
83
- if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile(MANIFEST_FILE, dir);
84
- throw error;
85
- }
86
- let opsRaw;
87
- try {
88
- opsRaw = await readFile(opsPath, "utf-8");
89
- } catch (error) {
90
- if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile(OPS_FILE, dir);
91
- throw error;
92
- }
93
- let manifest;
94
- try {
95
- manifest = JSON.parse(manifestRaw);
96
- } catch (e) {
97
- throw errorInvalidJson(manifestPath, e instanceof Error ? e.message : String(e));
98
- }
99
- let ops;
100
- try {
101
- ops = JSON.parse(opsRaw);
102
- } catch (e) {
103
- throw errorInvalidJson(opsPath, e instanceof Error ? e.message : String(e));
104
- }
105
- validateManifest(manifest, manifestPath);
106
- validateOps(ops, opsPath);
107
- return {
108
- dirName: basename(dir),
109
- dirPath: dir,
110
- manifest,
111
- ops
112
- };
113
- }
114
- function validateManifest(manifest, filePath) {
115
- const result = MigrationManifestSchema(manifest);
116
- if (result instanceof type.errors) throw errorInvalidManifest(filePath, result.summary);
117
- }
118
- function validateOps(ops, filePath) {
119
- const result = MigrationOpsSchema(ops);
120
- if (result instanceof type.errors) throw errorInvalidManifest(filePath, result.summary);
121
- }
122
- async function readMigrationsDir(migrationsRoot) {
123
- let entries;
124
- try {
125
- entries = await readdir(migrationsRoot);
126
- } catch (error) {
127
- if (hasErrnoCode(error, "ENOENT")) return [];
128
- throw error;
129
- }
130
- const packages = [];
131
- for (const entry of entries.sort()) {
132
- const entryPath = join(migrationsRoot, entry);
133
- if (!(await stat(entryPath)).isDirectory()) continue;
134
- const manifestPath = join(entryPath, MANIFEST_FILE);
135
- try {
136
- await stat(manifestPath);
137
- } catch {
138
- continue;
139
- }
140
- packages.push(await readMigrationPackage(entryPath));
141
- }
142
- return packages;
143
- }
144
- function formatMigrationDirName(timestamp, slug) {
145
- const sanitized = slug.toLowerCase().replace(/[^a-z0-9]/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "");
146
- if (sanitized.length === 0) throw errorInvalidSlug(slug);
147
- const truncated = sanitized.slice(0, MAX_SLUG_LENGTH);
148
- return `${timestamp.getUTCFullYear()}${String(timestamp.getUTCMonth() + 1).padStart(2, "0")}${String(timestamp.getUTCDate()).padStart(2, "0")}T${String(timestamp.getUTCHours()).padStart(2, "0")}${String(timestamp.getUTCMinutes()).padStart(2, "0")}_${truncated}`;
149
- }
150
-
151
- //#endregion
152
- export { writeMigrationManifest as a, readMigrationsDir as i, formatMigrationDirName as n, writeMigrationOps as o, readMigrationPackage as r, writeMigrationPackage as s, copyFilesWithRename as t };
153
- //# sourceMappingURL=io-CCnYsUHU.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"io-CCnYsUHU.mjs","names":["manifestRaw: string","opsRaw: string","manifest: MigrationManifest","ops: MigrationOps","entries: string[]","packages: MigrationBundle[]"],"sources":["../src/io.ts"],"sourcesContent":["import { copyFile, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';\nimport { type } from 'arktype';\nimport { basename, dirname, join } from 'pathe';\nimport {\n errorDirectoryExists,\n errorInvalidDestName,\n errorInvalidJson,\n errorInvalidManifest,\n errorInvalidSlug,\n errorMissingFile,\n} from './errors';\nimport type { MigrationBundle, MigrationManifest, MigrationOps } from './types';\n\nconst MANIFEST_FILE = 'migration.json';\nconst OPS_FILE = 'ops.json';\nconst MAX_SLUG_LENGTH = 64;\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\nconst MigrationHintsSchema = type({\n used: 'string[]',\n applied: 'string[]',\n plannerVersion: 'string',\n});\n\nconst MigrationManifestSchema = type({\n from: 'string',\n to: 'string',\n migrationId: 'string',\n kind: \"'regular' | 'baseline'\",\n fromContract: 'object | null',\n toContract: 'object',\n hints: MigrationHintsSchema,\n labels: 'string[]',\n 'authorship?': type({\n 'author?': 'string',\n 'email?': 'string',\n }),\n 'signature?': type({\n keyId: 'string',\n value: 'string',\n }).or('null'),\n createdAt: 'string',\n});\n\nconst MigrationOpSchema = type({\n id: 'string',\n label: 'string',\n operationClass: \"'additive' | 'widening' | 'destructive' | 'data'\",\n});\n\n// Intentionally shallow: operation-specific payload validation is owned by planner/runner layers.\nconst MigrationOpsSchema = MigrationOpSchema.array();\n\nexport async function writeMigrationPackage(\n dir: string,\n manifest: MigrationManifest,\n ops: MigrationOps,\n): Promise<void> {\n await mkdir(dirname(dir), { recursive: true });\n\n try {\n await mkdir(dir);\n } catch (error) {\n if (hasErrnoCode(error, 'EEXIST')) {\n throw errorDirectoryExists(dir);\n }\n throw error;\n }\n\n await writeFile(join(dir, MANIFEST_FILE), JSON.stringify(manifest, null, 2), { flag: 'wx' });\n await writeFile(join(dir, OPS_FILE), JSON.stringify(ops, null, 2), { flag: 'wx' });\n}\n\n/**\n * Copy a list of files into `destDir`, optionally renaming each one.\n *\n * The destination directory is created (with `recursive: true`) if it\n * does not already exist. Each source path is copied byte-for-byte into\n * `destDir/<destName>`; missing sources throw `ENOENT`. The helper is\n * intentionally generic: callers own the list of files (e.g. a contract\n * emitter's emitted output) and the naming convention (e.g. renaming\n * the destination contract to `end-contract.*` and the source contract\n * to `start-contract.*`).\n */\nexport async function copyFilesWithRename(\n destDir: string,\n files: readonly { readonly sourcePath: string; readonly destName: string }[],\n): Promise<void> {\n await mkdir(destDir, { recursive: true });\n for (const file of files) {\n if (basename(file.destName) !== file.destName) {\n throw errorInvalidDestName(file.destName);\n }\n await copyFile(file.sourcePath, join(destDir, file.destName));\n }\n}\n\nexport async function writeMigrationManifest(\n dir: string,\n manifest: MigrationManifest,\n): Promise<void> {\n await writeFile(join(dir, MANIFEST_FILE), `${JSON.stringify(manifest, null, 2)}\\n`);\n}\n\nexport async function writeMigrationOps(dir: string, ops: MigrationOps): Promise<void> {\n await writeFile(join(dir, OPS_FILE), `${JSON.stringify(ops, null, 2)}\\n`);\n}\n\nexport async function readMigrationPackage(dir: string): Promise<MigrationBundle> {\n const manifestPath = join(dir, MANIFEST_FILE);\n const opsPath = join(dir, OPS_FILE);\n\n let manifestRaw: string;\n try {\n manifestRaw = await readFile(manifestPath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile(MANIFEST_FILE, dir);\n }\n throw error;\n }\n\n let opsRaw: string;\n try {\n opsRaw = await readFile(opsPath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile(OPS_FILE, dir);\n }\n throw error;\n }\n\n let manifest: MigrationManifest;\n try {\n manifest = JSON.parse(manifestRaw);\n } catch (e) {\n throw errorInvalidJson(manifestPath, e instanceof Error ? e.message : String(e));\n }\n\n let ops: MigrationOps;\n try {\n ops = JSON.parse(opsRaw);\n } catch (e) {\n throw errorInvalidJson(opsPath, e instanceof Error ? e.message : String(e));\n }\n\n validateManifest(manifest, manifestPath);\n validateOps(ops, opsPath);\n\n return {\n dirName: basename(dir),\n dirPath: dir,\n manifest,\n ops,\n };\n}\n\nfunction validateManifest(\n manifest: unknown,\n filePath: string,\n): asserts manifest is MigrationManifest {\n const result = MigrationManifestSchema(manifest);\n if (result instanceof type.errors) {\n throw errorInvalidManifest(filePath, result.summary);\n }\n}\n\nfunction validateOps(ops: unknown, filePath: string): asserts ops is MigrationOps {\n const result = MigrationOpsSchema(ops);\n if (result instanceof type.errors) {\n throw errorInvalidManifest(filePath, result.summary);\n }\n}\n\nexport async function readMigrationsDir(\n migrationsRoot: string,\n): Promise<readonly MigrationBundle[]> {\n let entries: string[];\n try {\n entries = await readdir(migrationsRoot);\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return [];\n }\n throw error;\n }\n\n const packages: MigrationBundle[] = [];\n\n for (const entry of entries.sort()) {\n const entryPath = join(migrationsRoot, entry);\n const entryStat = await stat(entryPath);\n if (!entryStat.isDirectory()) continue;\n\n const manifestPath = join(entryPath, MANIFEST_FILE);\n try {\n await stat(manifestPath);\n } catch {\n continue; // skip non-migration directories\n }\n\n packages.push(await readMigrationPackage(entryPath));\n }\n\n return packages;\n}\n\nexport function formatMigrationDirName(timestamp: Date, slug: string): string {\n const sanitized = slug\n .toLowerCase()\n .replace(/[^a-z0-9]/g, '_')\n .replace(/_+/g, '_')\n .replace(/^_|_$/g, '');\n\n if (sanitized.length === 0) {\n throw errorInvalidSlug(slug);\n }\n\n const truncated = sanitized.slice(0, MAX_SLUG_LENGTH);\n\n const y = timestamp.getUTCFullYear();\n const mo = String(timestamp.getUTCMonth() + 1).padStart(2, '0');\n const d = String(timestamp.getUTCDate()).padStart(2, '0');\n const h = String(timestamp.getUTCHours()).padStart(2, '0');\n const mi = String(timestamp.getUTCMinutes()).padStart(2, '0');\n\n return `${y}${mo}${d}T${h}${mi}_${truncated}`;\n}\n"],"mappings":";;;;;;AAaA,MAAM,gBAAgB;AACtB,MAAM,WAAW;AACjB,MAAM,kBAAkB;AAExB,SAAS,aAAa,OAAgB,MAAuB;AAC3D,QAAO,iBAAiB,SAAU,MAA4B,SAAS;;AASzE,MAAM,0BAA0B,KAAK;CACnC,MAAM;CACN,IAAI;CACJ,aAAa;CACb,MAAM;CACN,cAAc;CACd,YAAY;CACZ,OAb2B,KAAK;EAChC,MAAM;EACN,SAAS;EACT,gBAAgB;EACjB,CAAC;CAUA,QAAQ;CACR,eAAe,KAAK;EAClB,WAAW;EACX,UAAU;EACX,CAAC;CACF,cAAc,KAAK;EACjB,OAAO;EACP,OAAO;EACR,CAAC,CAAC,GAAG,OAAO;CACb,WAAW;CACZ,CAAC;AASF,MAAM,qBAPoB,KAAK;CAC7B,IAAI;CACJ,OAAO;CACP,gBAAgB;CACjB,CAAC,CAG2C,OAAO;AAEpD,eAAsB,sBACpB,KACA,UACA,KACe;AACf,OAAM,MAAM,QAAQ,IAAI,EAAE,EAAE,WAAW,MAAM,CAAC;AAE9C,KAAI;AACF,QAAM,MAAM,IAAI;UACT,OAAO;AACd,MAAI,aAAa,OAAO,SAAS,CAC/B,OAAM,qBAAqB,IAAI;AAEjC,QAAM;;AAGR,OAAM,UAAU,KAAK,KAAK,cAAc,EAAE,KAAK,UAAU,UAAU,MAAM,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAC5F,OAAM,UAAU,KAAK,KAAK,SAAS,EAAE,KAAK,UAAU,KAAK,MAAM,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;;;;;;;;;;;;;AAcpF,eAAsB,oBACpB,SACA,OACe;AACf,OAAM,MAAM,SAAS,EAAE,WAAW,MA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AAG"}
@@ -1,71 +0,0 @@
1
- import { Contract } from "@prisma-next/contract/types";
2
- import { MigrationPlanOperation } from "@prisma-next/framework-components/control";
3
-
4
- //#region src/types.d.ts
5
- interface MigrationHints {
6
- readonly used: readonly string[];
7
- readonly applied: readonly string[];
8
- readonly plannerVersion: string;
9
- }
10
- /**
11
- * On-disk migration manifest. Every migration is content-addressed: the
12
- * `migrationId` is a hash over the manifest envelope plus the operations
13
- * list, computed at write time. There is no draft state — a migration
14
- * directory either exists with a fully attested manifest or it does not.
15
- *
16
- * When the planner cannot lower an operation because of an unfilled
17
- * `placeholder(...)` slot, the migration is still written with
18
- * `migrationId` hashed over `ops: []`. Re-running self-emit after the
19
- * user fills the placeholder produces a *different* `migrationId`
20
- * (committed to the real ops); this is intentional.
21
- */
22
- interface MigrationManifest {
23
- readonly migrationId: string;
24
- readonly from: string;
25
- readonly to: string;
26
- readonly kind: 'regular' | 'baseline';
27
- readonly fromContract: Contract | null;
28
- readonly toContract: Contract;
29
- readonly hints: MigrationHints;
30
- readonly labels: readonly string[];
31
- readonly authorship?: {
32
- readonly author?: string;
33
- readonly email?: string;
34
- };
35
- readonly signature?: {
36
- readonly keyId: string;
37
- readonly value: string;
38
- } | null;
39
- readonly createdAt: string;
40
- }
41
- type MigrationOps = readonly MigrationPlanOperation[];
42
- /**
43
- * An on-disk migration directory containing a manifest and operations.
44
- */
45
- interface MigrationBundle {
46
- readonly dirName: string;
47
- readonly dirPath: string;
48
- readonly manifest: MigrationManifest;
49
- readonly ops: MigrationOps;
50
- }
51
- /**
52
- * An entry in the migration graph. All on-disk migrations are attested,
53
- * so `migrationId` is always a string.
54
- */
55
- interface MigrationChainEntry {
56
- readonly from: string;
57
- readonly to: string;
58
- readonly migrationId: string;
59
- readonly dirName: string;
60
- readonly createdAt: string;
61
- readonly labels: readonly string[];
62
- }
63
- interface MigrationGraph {
64
- readonly nodes: ReadonlySet<string>;
65
- readonly forwardChain: ReadonlyMap<string, readonly MigrationChainEntry[]>;
66
- readonly reverseChain: ReadonlyMap<string, readonly MigrationChainEntry[]>;
67
- readonly migrationById: ReadonlyMap<string, MigrationChainEntry>;
68
- }
69
- //#endregion
70
- export { MigrationManifest as a, MigrationHints as i, MigrationChainEntry as n, MigrationOps as o, MigrationGraph as r, MigrationBundle as t };
71
- //# sourceMappingURL=types-DyGXcWWp.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-DyGXcWWp.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;UAGiB,cAAA;;EAAA,SAAA,OAAA,EAAc,SAAA,MAAA,EAAA;EAkBd,SAAA,cAAiB,EAAA,MAAA;;;;;AAclC;AAKA;AAWA;AASA;;;;;;AAI8C,UA3C7B,iBAAA,CA2C6B;EAApB,SAAA,WAAA,EAAA,MAAA;EAAW,SAAA,IAAA,EAAA,MAAA;;;yBAtCZ;uBACF;kBACL;;;;;;;;;;;;KAON,YAAA,YAAwB;;;;UAKnB,eAAA;;;qBAGI;gBACL;;;;;;UAOC,mBAAA;;;;;;;;UASA,cAAA;kBACC;yBACO,6BAA6B;yBAC7B,6BAA6B;0BAC5B,oBAAoB"}