@prisma-next/migration-tools 0.4.1 → 0.4.2
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/dist/{attestation-DtF8tEOM.mjs → attestation-BnzTb0Qp.mjs} +2 -2
- package/dist/{attestation-DtF8tEOM.mjs.map → attestation-BnzTb0Qp.mjs.map} +1 -1
- package/dist/{errors-BKbRGCJM.mjs → errors-BmiSgz1j.mjs} +7 -7
- package/dist/{errors-BKbRGCJM.mjs.map → errors-BmiSgz1j.mjs.map} +1 -1
- package/dist/exports/attestation.mjs +2 -2
- package/dist/exports/dag.mjs +1 -1
- package/dist/exports/io.mjs +1 -1
- package/dist/exports/migration-ts.d.mts +5 -1
- package/dist/exports/migration-ts.d.mts.map +1 -1
- package/dist/exports/migration-ts.mjs +5 -1
- package/dist/exports/migration-ts.mjs.map +1 -1
- package/dist/exports/migration.d.mts +47 -17
- package/dist/exports/migration.d.mts.map +1 -1
- package/dist/exports/migration.mjs +58 -75
- package/dist/exports/migration.mjs.map +1 -1
- package/dist/exports/refs.d.mts +11 -5
- package/dist/exports/refs.d.mts.map +1 -1
- package/dist/exports/refs.mjs +106 -30
- package/dist/exports/refs.mjs.map +1 -1
- package/dist/exports/types.mjs +1 -1
- package/dist/{io-CCnYsUHU.mjs → io-Cd6GLyjK.mjs} +2 -2
- package/dist/{io-CCnYsUHU.mjs.map → io-Cd6GLyjK.mjs.map} +1 -1
- package/package.json +4 -4
- package/src/exports/migration.ts +8 -1
- package/src/exports/refs.ts +10 -2
- package/src/migration-base.ts +80 -90
- package/src/migration-ts.ts +5 -1
- package/src/refs.ts +148 -37
package/dist/exports/refs.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { c as
|
|
2
|
-
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
1
|
+
import { c as errorInvalidRefFile, l as errorInvalidRefName, t as MigrationToolsError, u as errorInvalidRefValue } from "../errors-BmiSgz1j.mjs";
|
|
2
|
+
import { mkdir, readFile, readdir, rename, rmdir, unlink, writeFile } from "node:fs/promises";
|
|
3
3
|
import { type } from "arktype";
|
|
4
|
-
import { dirname, join } from "pathe";
|
|
4
|
+
import { dirname, join, relative } from "pathe";
|
|
5
5
|
|
|
6
6
|
//#region src/refs.ts
|
|
7
7
|
const REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;
|
|
@@ -16,58 +16,134 @@ function validateRefName(name) {
|
|
|
16
16
|
function validateRefValue(value) {
|
|
17
17
|
return REF_VALUE_PATTERN.test(value);
|
|
18
18
|
}
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
19
|
+
const RefEntrySchema = type({
|
|
20
|
+
hash: "string",
|
|
21
|
+
invariants: "string[]"
|
|
22
|
+
}).narrow((entry, ctx) => {
|
|
23
|
+
if (!validateRefValue(entry.hash)) return ctx.mustBe(`a valid contract hash (got "${entry.hash}")`);
|
|
24
24
|
return true;
|
|
25
25
|
});
|
|
26
|
-
|
|
26
|
+
function refFilePath(refsDir, name) {
|
|
27
|
+
return join(refsDir, `${name}.json`);
|
|
28
|
+
}
|
|
29
|
+
function refNameFromPath(refsDir, filePath) {
|
|
30
|
+
return relative(refsDir, filePath).replace(/\.json$/, "");
|
|
31
|
+
}
|
|
32
|
+
async function readRef(refsDir, name) {
|
|
33
|
+
if (!validateRefName(name)) throw errorInvalidRefName(name);
|
|
34
|
+
const filePath = refFilePath(refsDir, name);
|
|
27
35
|
let raw;
|
|
28
36
|
try {
|
|
29
|
-
raw = await readFile(
|
|
37
|
+
raw = await readFile(filePath, "utf-8");
|
|
30
38
|
} catch (error) {
|
|
31
|
-
if (error instanceof Error && error.code === "ENOENT")
|
|
39
|
+
if (error instanceof Error && error.code === "ENOENT") throw new MigrationToolsError("MIGRATION.UNKNOWN_REF", `Unknown ref "${name}"`, {
|
|
40
|
+
why: `No ref file found at "${filePath}".`,
|
|
41
|
+
fix: `Create the ref with: prisma-next migration ref set ${name} <hash>`,
|
|
42
|
+
details: {
|
|
43
|
+
refName: name,
|
|
44
|
+
filePath
|
|
45
|
+
}
|
|
46
|
+
});
|
|
32
47
|
throw error;
|
|
33
48
|
}
|
|
34
49
|
let parsed;
|
|
35
50
|
try {
|
|
36
51
|
parsed = JSON.parse(raw);
|
|
37
52
|
} catch {
|
|
38
|
-
throw
|
|
53
|
+
throw errorInvalidRefFile(filePath, "Failed to parse as JSON");
|
|
39
54
|
}
|
|
40
|
-
const result =
|
|
41
|
-
if (result instanceof type.errors) throw
|
|
55
|
+
const result = RefEntrySchema(parsed);
|
|
56
|
+
if (result instanceof type.errors) throw errorInvalidRefFile(filePath, result.summary);
|
|
42
57
|
return result;
|
|
43
58
|
}
|
|
44
|
-
async function
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
async function readRefs(refsDir) {
|
|
60
|
+
let entries;
|
|
61
|
+
try {
|
|
62
|
+
entries = await readdir(refsDir, {
|
|
63
|
+
recursive: true,
|
|
64
|
+
encoding: "utf-8"
|
|
65
|
+
});
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (error instanceof Error && error.code === "ENOENT") return {};
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
const jsonFiles = entries.filter((entry) => entry.endsWith(".json"));
|
|
71
|
+
const refs = {};
|
|
72
|
+
for (const jsonFile of jsonFiles) {
|
|
73
|
+
const filePath = join(refsDir, jsonFile);
|
|
74
|
+
const name = refNameFromPath(refsDir, filePath);
|
|
75
|
+
let raw;
|
|
76
|
+
try {
|
|
77
|
+
raw = await readFile(filePath, "utf-8");
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const code = error instanceof Error ? error.code : void 0;
|
|
80
|
+
if (code === "ENOENT" || code === "EISDIR") continue;
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
let parsed;
|
|
84
|
+
try {
|
|
85
|
+
parsed = JSON.parse(raw);
|
|
86
|
+
} catch {
|
|
87
|
+
throw errorInvalidRefFile(filePath, "Failed to parse as JSON");
|
|
88
|
+
}
|
|
89
|
+
const result = RefEntrySchema(parsed);
|
|
90
|
+
if (result instanceof type.errors) throw errorInvalidRefFile(filePath, result.summary);
|
|
91
|
+
refs[name] = result;
|
|
48
92
|
}
|
|
49
|
-
|
|
50
|
-
|
|
93
|
+
return refs;
|
|
94
|
+
}
|
|
95
|
+
async function writeRef(refsDir, name, entry) {
|
|
96
|
+
if (!validateRefName(name)) throw errorInvalidRefName(name);
|
|
97
|
+
if (!validateRefValue(entry.hash)) throw errorInvalidRefValue(entry.hash);
|
|
98
|
+
const filePath = refFilePath(refsDir, name);
|
|
99
|
+
const dir = dirname(filePath);
|
|
51
100
|
await mkdir(dir, { recursive: true });
|
|
52
|
-
const tmpPath = join(dir,
|
|
53
|
-
await writeFile(tmpPath, `${JSON.stringify(
|
|
54
|
-
|
|
101
|
+
const tmpPath = join(dir, `.${name.split("/").pop()}.json.${Date.now()}.tmp`);
|
|
102
|
+
await writeFile(tmpPath, `${JSON.stringify({
|
|
103
|
+
hash: entry.hash,
|
|
104
|
+
invariants: [...entry.invariants]
|
|
105
|
+
}, null, 2)}\n`);
|
|
106
|
+
await rename(tmpPath, filePath);
|
|
107
|
+
}
|
|
108
|
+
async function deleteRef(refsDir, name) {
|
|
109
|
+
if (!validateRefName(name)) throw errorInvalidRefName(name);
|
|
110
|
+
const filePath = refFilePath(refsDir, name);
|
|
111
|
+
try {
|
|
112
|
+
await unlink(filePath);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (error instanceof Error && error.code === "ENOENT") throw new MigrationToolsError("MIGRATION.UNKNOWN_REF", `Unknown ref "${name}"`, {
|
|
115
|
+
why: `No ref file found at "${filePath}".`,
|
|
116
|
+
fix: "Run `prisma-next migration ref list` to see available refs.",
|
|
117
|
+
details: {
|
|
118
|
+
refName: name,
|
|
119
|
+
filePath
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
let dir = dirname(filePath);
|
|
125
|
+
while (dir !== refsDir && dir.startsWith(refsDir)) try {
|
|
126
|
+
await rmdir(dir);
|
|
127
|
+
dir = dirname(dir);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
const code = error instanceof Error ? error.code : void 0;
|
|
130
|
+
if (code === "ENOTEMPTY" || code === "EEXIST" || code === "ENOENT") break;
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
55
133
|
}
|
|
56
134
|
function resolveRef(refs, name) {
|
|
57
135
|
if (!validateRefName(name)) throw errorInvalidRefName(name);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
fix: `Available refs: ${Object.keys(refs).join(", ") || "(none)"}. Create a ref with: set the "${name}" key in migrations/refs.json.`,
|
|
136
|
+
if (!Object.hasOwn(refs, name)) throw new MigrationToolsError("MIGRATION.UNKNOWN_REF", `Unknown ref "${name}"`, {
|
|
137
|
+
why: `No ref named "${name}" exists.`,
|
|
138
|
+
fix: `Available refs: ${Object.keys(refs).join(", ") || "(none)"}. Create a ref with: prisma-next migration ref set ${name} <hash>`,
|
|
62
139
|
details: {
|
|
63
140
|
refName: name,
|
|
64
141
|
availableRefs: Object.keys(refs)
|
|
65
142
|
}
|
|
66
143
|
});
|
|
67
|
-
|
|
68
|
-
return hash;
|
|
144
|
+
return refs[name];
|
|
69
145
|
}
|
|
70
146
|
|
|
71
147
|
//#endregion
|
|
72
|
-
export { readRefs, resolveRef, validateRefName, validateRefValue,
|
|
148
|
+
export { deleteRef, readRef, readRefs, resolveRef, validateRefName, validateRefValue, writeRef };
|
|
73
149
|
//# sourceMappingURL=refs.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refs.mjs","names":["raw: string","parsed: unknown"],"sources":["../../src/refs.ts"],"sourcesContent":["import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';\nimport { type } from 'arktype';\nimport { dirname, join } from 'pathe';\nimport {\n errorInvalidRefName,\n errorInvalidRefs,\n errorInvalidRefValue,\n MigrationToolsError,\n} from './errors';\n\nexport type Refs = Readonly<Record<string, string>>;\n\nconst REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;\nconst REF_VALUE_PATTERN = /^sha256:(empty|[0-9a-f]{64})$/;\n\nexport function validateRefName(name: string): boolean {\n if (name.length === 0) return false;\n if (name.includes('..')) return false;\n if (name.includes('//')) return false;\n if (name.startsWith('.')) return false;\n return REF_NAME_PATTERN.test(name);\n}\n\nexport function validateRefValue(value: string): boolean {\n return REF_VALUE_PATTERN.test(value);\n}\n\nconst RefsSchema = type('Record<string, string>').narrow((refs, ctx) => {\n for (const [key, value] of Object.entries(refs)) {\n if (!validateRefName(key)) return ctx.mustBe(`valid ref names (invalid: \"${key}\")`);\n if (!validateRefValue(value))\n return ctx.mustBe(`valid contract hashes (invalid value for \"${key}\": \"${value}\")`);\n }\n return true;\n});\n\nexport async function readRefs(refsPath: string): Promise<Refs> {\n let raw: string;\n try {\n raw = await readFile(refsPath, 'utf-8');\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return {};\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n throw errorInvalidRefs(refsPath, 'Failed to parse as JSON');\n }\n\n const result = RefsSchema(parsed);\n if (result instanceof type.errors) {\n throw errorInvalidRefs(refsPath, result.summary);\n }\n\n return result;\n}\n\nexport async function writeRefs(refsPath: string, refs: Refs): Promise<void> {\n for (const [key, value] of Object.entries(refs)) {\n if (!validateRefName(key)) {\n throw errorInvalidRefName(key);\n }\n if (!validateRefValue(value)) {\n throw errorInvalidRefValue(value);\n }\n }\n\n const sorted = Object.fromEntries(Object.entries(refs).sort(([a], [b]) => a.localeCompare(b)));\n\n const dir = dirname(refsPath);\n await mkdir(dir, { recursive: true });\n\n const tmpPath = join(dir, `.refs.json.${Date.now()}.tmp`);\n await writeFile(tmpPath, `${JSON.stringify(sorted, null, 2)}\\n`);\n await rename(tmpPath, refsPath);\n}\n\nexport function resolveRef(refs: Refs, name: string): string {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const hash = refs[name];\n if (hash === undefined) {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref named \"${name}\" exists in refs.json.`,\n fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: set the \"${name}\" key in migrations/refs.json.`,\n details: { refName: name, availableRefs: Object.keys(refs) },\n });\n }\n\n if (!validateRefValue(hash)) {\n throw errorInvalidRefValue(hash);\n }\n\n return hash;\n}\n"],"mappings":";;;;;;AAYA,MAAM,mBAAmB;AACzB,MAAM,oBAAoB;AAE1B,SAAgB,gBAAgB,MAAuB;AACrD,KAAI,KAAK,WAAW,EAAG,QAAO;AAC9B,KAAI,KAAK,SAAS,KAAK,CAAE,QAAO;AAChC,KAAI,KAAK,SAAS,KAAK,CAAE,QAAO;AAChC,KAAI,KAAK,WAAW,IAAI,CAAE,QAAO;AACjC,QAAO,iBAAiB,KAAK,KAAK;;AAGpC,SAAgB,iBAAiB,OAAwB;AACvD,QAAO,kBAAkB,KAAK,MAAM;;AAGtC,MAAM,aAAa,KAAK,yBAAyB,CAAC,QAAQ,MAAM,QAAQ;AACtE,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,KAAK,EAAE;AAC/C,MAAI,CAAC,gBAAgB,IAAI,CAAE,QAAO,IAAI,OAAO,8BAA8B,IAAI,IAAI;AACnF,MAAI,CAAC,iBAAiB,MAAM,CAC1B,QAAO,IAAI,OAAO,6CAA6C,IAAI,MAAM,MAAM,IAAI;;AAEvF,QAAO;EACP;AAEF,eAAsB,SAAS,UAAiC;CAC9D,IAAIA;AACJ,KAAI;AACF,QAAM,MAAM,SAAS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AAK,KAAK;GAAE;EAC7D,CAAC;AAGJ,KAAI,CAAC,iBAAiB,KAAK,CACzB,OAAM,qBAAqB,KAAK;AAGlC,QAAO"}
|
|
1
|
+
{"version":3,"file":"refs.mjs","names":["raw: string","parsed: unknown","entries: string[]","refs: Record<string, RefEntry>"],"sources":["../../src/refs.ts"],"sourcesContent":["import { mkdir, readdir, readFile, rename, rmdir, unlink, writeFile } from 'node:fs/promises';\nimport { type } from 'arktype';\nimport { dirname, join, relative } from 'pathe';\nimport {\n errorInvalidRefFile,\n errorInvalidRefName,\n errorInvalidRefValue,\n MigrationToolsError,\n} from './errors';\n\nexport interface RefEntry {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\nexport type Refs = Readonly<Record<string, RefEntry>>;\n\nconst REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;\nconst REF_VALUE_PATTERN = /^sha256:(empty|[0-9a-f]{64})$/;\n\nexport function validateRefName(name: string): boolean {\n if (name.length === 0) return false;\n if (name.includes('..')) return false;\n if (name.includes('//')) return false;\n if (name.startsWith('.')) return false;\n return REF_NAME_PATTERN.test(name);\n}\n\nexport function validateRefValue(value: string): boolean {\n return REF_VALUE_PATTERN.test(value);\n}\n\nconst RefEntrySchema = type({\n hash: 'string',\n invariants: 'string[]',\n}).narrow((entry, ctx) => {\n if (!validateRefValue(entry.hash))\n return ctx.mustBe(`a valid contract hash (got \"${entry.hash}\")`);\n return true;\n});\n\nfunction refFilePath(refsDir: string, name: string): string {\n return join(refsDir, `${name}.json`);\n}\n\nfunction refNameFromPath(refsDir: string, filePath: string): string {\n const rel = relative(refsDir, filePath);\n return rel.replace(/\\.json$/, '');\n}\n\nexport async function readRef(refsDir: string, name: string): Promise<RefEntry> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const filePath = refFilePath(refsDir, name);\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref file found at \"${filePath}\".`,\n fix: `Create the ref with: prisma-next migration ref set ${name} <hash>`,\n details: { refName: name, filePath },\n });\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');\n }\n\n const result = RefEntrySchema(parsed);\n if (result instanceof type.errors) {\n throw errorInvalidRefFile(filePath, result.summary);\n }\n\n return result;\n}\n\nexport async function readRefs(refsDir: string): Promise<Refs> {\n let entries: string[];\n try {\n entries = await readdir(refsDir, { recursive: true, encoding: 'utf-8' });\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return {};\n }\n throw error;\n }\n\n const jsonFiles = entries.filter((entry) => entry.endsWith('.json'));\n const refs: Record<string, RefEntry> = {};\n\n for (const jsonFile of jsonFiles) {\n const filePath = join(refsDir, jsonFile);\n const name = refNameFromPath(refsDir, filePath);\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n // Tolerate the TOCTOU race between `readdir` and `readFile` (ENOENT) and\n // benign EISDIR if a directory happens to end in `.json`. Anything else\n // (EACCES, EIO, EMFILE, …) is a real failure and propagates so the CLI\n // surfaces it rather than silently dropping the ref.\n const code = error instanceof Error ? (error as { code?: string }).code : undefined;\n if (code === 'ENOENT' || code === 'EISDIR') {\n continue;\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');\n }\n\n const result = RefEntrySchema(parsed);\n if (result instanceof type.errors) {\n throw errorInvalidRefFile(filePath, result.summary);\n }\n\n refs[name] = result;\n }\n\n return refs;\n}\n\nexport async function writeRef(refsDir: string, name: string, entry: RefEntry): Promise<void> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n if (!validateRefValue(entry.hash)) {\n throw errorInvalidRefValue(entry.hash);\n }\n\n const filePath = refFilePath(refsDir, name);\n const dir = dirname(filePath);\n await mkdir(dir, { recursive: true });\n\n const tmpPath = join(dir, `.${name.split('/').pop()}.json.${Date.now()}.tmp`);\n await writeFile(\n tmpPath,\n `${JSON.stringify({ hash: entry.hash, invariants: [...entry.invariants] }, null, 2)}\\n`,\n );\n await rename(tmpPath, filePath);\n}\n\nexport async function deleteRef(refsDir: string, name: string): Promise<void> {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n const filePath = refFilePath(refsDir, name);\n try {\n await unlink(filePath);\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref file found at \"${filePath}\".`,\n fix: 'Run `prisma-next migration ref list` to see available refs.',\n details: { refName: name, filePath },\n });\n }\n throw error;\n }\n\n // Clean empty parent directories up to refsDir. Stop walking on the expected\n // \"directory has siblings\" signal (ENOTEMPTY on Linux, EEXIST on some BSDs)\n // and on ENOENT (concurrent removal). Anything else (EACCES, EIO, …) is a\n // real failure and propagates.\n let dir = dirname(filePath);\n while (dir !== refsDir && dir.startsWith(refsDir)) {\n try {\n await rmdir(dir);\n dir = dirname(dir);\n } catch (error) {\n const code = error instanceof Error ? (error as { code?: string }).code : undefined;\n if (code === 'ENOTEMPTY' || code === 'EEXIST' || code === 'ENOENT') {\n break;\n }\n throw error;\n }\n }\n}\n\nexport function resolveRef(refs: Refs, name: string): RefEntry {\n if (!validateRefName(name)) {\n throw errorInvalidRefName(name);\n }\n\n // Object.hasOwn gate: plain-object `refs` would otherwise let\n // `refs['constructor']` return Object.prototype.constructor and bypass the\n // UNKNOWN_REF throw. validateRefName accepts `\"constructor\"` as a name shape.\n if (!Object.hasOwn(refs, name)) {\n throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref \"${name}\"`, {\n why: `No ref named \"${name}\" exists.`,\n fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: prisma-next migration ref set ${name} <hash>`,\n details: { refName: name, availableRefs: Object.keys(refs) },\n });\n }\n\n // biome-ignore lint/style/noNonNullAssertion: Object.hasOwn gate above guarantees this is defined\n return refs[name]!;\n}\n"],"mappings":";;;;;;AAiBA,MAAM,mBAAmB;AACzB,MAAM,oBAAoB;AAE1B,SAAgB,gBAAgB,MAAuB;AACrD,KAAI,KAAK,WAAW,EAAG,QAAO;AAC9B,KAAI,KAAK,SAAS,KAAK,CAAE,QAAO;AAChC,KAAI,KAAK,SAAS,KAAK,CAAE,QAAO;AAChC,KAAI,KAAK,WAAW,IAAI,CAAE,QAAO;AACjC,QAAO,iBAAiB,KAAK,KAAK;;AAGpC,SAAgB,iBAAiB,OAAwB;AACvD,QAAO,kBAAkB,KAAK,MAAM;;AAGtC,MAAM,iBAAiB,KAAK;CAC1B,MAAM;CACN,YAAY;CACb,CAAC,CAAC,QAAQ,OAAO,QAAQ;AACxB,KAAI,CAAC,iBAAiB,MAAM,KAAK,CAC/B,QAAO,IAAI,OAAO,+BAA+B,MAAM,KAAK,IAAI;AAClE,QAAO;EACP;AAEF,SAAS,YAAY,SAAiB,MAAsB;AAC1D,QAAO,KAAK,SAAS,GAAG,KAAK,OAAO;;AAGtC,SAAS,gBAAgB,SAAiB,UAA0B;AAElE,QADY,SAAS,SAAS,SAAS,CAC5B,QAAQ,WAAW,GAAG;;AAGnC,eAAsB,QAAQ,SAAiB,MAAiC;AAC9E,KAAI,CAAC,gBAAgB,KAAK,CACxB,OAAM,oBAAoB,KAAK;CAGjC,MAAM,WAAW,YAAY,SAAS,KAAK;CAC3C,IAAIA;AACJ,KAAI;AACF,QAAM,MAAM,SAAS,UAAU,QAAQ;UAChC,OAAO;AACd,MAAI,iBAAiB,SAAU,MAA4B,SAAS,SAClE,OAAM,IAAI,oBAAoB,yBAAyB,gB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CAC5B,OAAM,IAAI,oBAAoB,yBAAyB,gBAAgB,KAAK,IAAI;EAC9E,KAAK,iBAAiB,KAAK;EAC3B,KAAK,mBAAmB,OAAO,KAAK,KAAK,CAAC,KAAK,KAAK,IAAI,SAAS,qDAAqD,KAAK;EAC3H,SAAS;GAAE,SAAS;GAAM,eAAe,OAAO,KAAK,KAAK;GAAE;EAC7D,CAAC;AAIJ,QAAO,KAAK"}
|
package/dist/exports/types.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as errorInvalidDestName, d as errorInvalidSlug, f as errorMissingFile, o as errorInvalidJson, r as errorDirectoryExists, s as errorInvalidManifest } from "./errors-
|
|
1
|
+
import { a as errorInvalidDestName, d as errorInvalidSlug, f as errorMissingFile, o as errorInvalidJson, r as errorDirectoryExists, s as errorInvalidManifest } from "./errors-BmiSgz1j.mjs";
|
|
2
2
|
import { copyFile, mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
3
3
|
import { type } from "arktype";
|
|
4
4
|
import { basename, dirname, join } from "pathe";
|
|
@@ -150,4 +150,4 @@ function formatMigrationDirName(timestamp, slug) {
|
|
|
150
150
|
|
|
151
151
|
//#endregion
|
|
152
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-
|
|
153
|
+
//# sourceMappingURL=io-Cd6GLyjK.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"io-
|
|
1
|
+
{"version":3,"file":"io-Cd6GLyjK.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,IA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OAAO,SAAS,CAC/B,QAAO,EAAE;AAEX,QAAM;;CAGR,MAAMC,WAA8B,EAAE;AAEtC,MAAK,MAAM,SAAS,QAAQ,MAAM,EAAE;EAClC,MAAM,YAAY,KAAK,gBAAgB,MAAM;AAE7C,MAAI,EADc,MAAM,KAAK,UAAU,EACxB,aAAa,CAAE;EAE9B,MAAM,eAAe,KAAK,WAAW,cAAc;AACnD,MAAI;AACF,SAAM,KAAK,aAAa;UAClB;AACN;;AAGF,WAAS,KAAK,MAAM,qBAAqB,UAAU,CAAC;;AAGtD,QAAO;;AAGT,SAAgB,uBAAuB,WAAiB,MAAsB;CAC5E,MAAM,YAAY,KACf,aAAa,CACb,QAAQ,cAAc,IAAI,CAC1B,QAAQ,OAAO,IAAI,CACnB,QAAQ,UAAU,GAAG;AAExB,KAAI,UAAU,WAAW,EACvB,OAAM,iBAAiB,KAAK;CAG9B,MAAM,YAAY,UAAU,MAAM,GAAG,gBAAgB;AAQrD,QAAO,GANG,UAAU,gBAAgB,GACzB,OAAO,UAAU,aAAa,GAAG,EAAE,CAAC,SAAS,GAAG,IAAI,GACrD,OAAO,UAAU,YAAY,CAAC,CAAC,SAAS,GAAG,IAAI,CAIpC,GAHX,OAAO,UAAU,aAAa,CAAC,CAAC,SAAS,GAAG,IAAI,GAC/C,OAAO,UAAU,eAAe,CAAC,CAAC,SAAS,GAAG,IAAI,CAE9B,GAAG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/migration-tools",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "On-disk migration persistence, attestation, and chain reconstruction for Prisma Next",
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
"arktype": "^2.1.29",
|
|
9
9
|
"pathe": "^2.0.3",
|
|
10
10
|
"prettier": "^3.6.2",
|
|
11
|
-
"@prisma-next/contract": "0.4.
|
|
12
|
-
"@prisma-next/framework-components": "0.4.
|
|
13
|
-
"@prisma-next/utils": "0.4.
|
|
11
|
+
"@prisma-next/contract": "0.4.2",
|
|
12
|
+
"@prisma-next/framework-components": "0.4.2",
|
|
13
|
+
"@prisma-next/utils": "0.4.2"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"tsdown": "0.18.4",
|
package/src/exports/migration.ts
CHANGED
package/src/exports/refs.ts
CHANGED
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
export type { Refs } from '../refs';
|
|
2
|
-
export {
|
|
1
|
+
export type { RefEntry, Refs } from '../refs';
|
|
2
|
+
export {
|
|
3
|
+
deleteRef,
|
|
4
|
+
readRef,
|
|
5
|
+
readRefs,
|
|
6
|
+
resolveRef,
|
|
7
|
+
validateRefName,
|
|
8
|
+
validateRefValue,
|
|
9
|
+
writeRef,
|
|
10
|
+
} from '../refs';
|
package/src/migration-base.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import type { Contract } from '@prisma-next/contract/types';
|
|
4
4
|
import type {
|
|
5
|
+
ControlStack,
|
|
5
6
|
MigrationPlan,
|
|
6
7
|
MigrationPlanOperation,
|
|
7
8
|
} from '@prisma-next/framework-components/control';
|
|
8
9
|
import { ifDefined } from '@prisma-next/utils/defined';
|
|
9
10
|
import { type } from 'arktype';
|
|
10
|
-
import { dirname, join } from 'pathe';
|
|
11
11
|
import { computeMigrationId } from './attestation';
|
|
12
12
|
import type { MigrationHints, MigrationManifest, MigrationOps } from './types';
|
|
13
13
|
|
|
@@ -34,11 +34,29 @@ const MigrationMetaSchema = type({
|
|
|
34
34
|
* every migration must implement — `migration.json` is required for a
|
|
35
35
|
* migration to be valid.
|
|
36
36
|
*/
|
|
37
|
-
export abstract class Migration<
|
|
38
|
-
|
|
37
|
+
export abstract class Migration<
|
|
38
|
+
TOperation extends MigrationPlanOperation = MigrationPlanOperation,
|
|
39
|
+
TFamilyId extends string = string,
|
|
40
|
+
TTargetId extends string = string,
|
|
41
|
+
> implements MigrationPlan
|
|
39
42
|
{
|
|
40
43
|
abstract readonly targetId: string;
|
|
41
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Assembled `ControlStack` injected by the orchestrator (`runMigration`).
|
|
47
|
+
*
|
|
48
|
+
* Subclasses (e.g. `PostgresMigration`) read the stack to materialize their
|
|
49
|
+
* adapter once per instance. Optional at the abstract level so unit tests can
|
|
50
|
+
* construct `Migration` instances purely for `operations` / `describe`
|
|
51
|
+
* assertions without needing a real stack; concrete subclasses that need the
|
|
52
|
+
* stack at runtime should narrow the parameter to required.
|
|
53
|
+
*/
|
|
54
|
+
protected readonly stack: ControlStack<TFamilyId, TTargetId> | undefined;
|
|
55
|
+
|
|
56
|
+
constructor(stack?: ControlStack<TFamilyId, TTargetId>) {
|
|
57
|
+
this.stack = stack;
|
|
58
|
+
}
|
|
59
|
+
|
|
42
60
|
/**
|
|
43
61
|
* Ordered list of operations this migration performs.
|
|
44
62
|
*
|
|
@@ -66,54 +84,30 @@ export abstract class Migration<TOperation extends MigrationPlanOperation = Migr
|
|
|
66
84
|
get destination(): { readonly storageHash: string } {
|
|
67
85
|
return { storageHash: this.describe().to };
|
|
68
86
|
}
|
|
87
|
+
}
|
|
69
88
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const metaFilename = fileURLToPath(importMetaUrl);
|
|
87
|
-
const argv1 = process.argv[1];
|
|
88
|
-
if (!argv1) return;
|
|
89
|
-
|
|
90
|
-
let isEntrypoint: boolean;
|
|
91
|
-
try {
|
|
92
|
-
isEntrypoint = realpathSync(metaFilename) === realpathSync(argv1);
|
|
93
|
-
} catch {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (!isEntrypoint) return;
|
|
97
|
-
|
|
98
|
-
const args = process.argv.slice(2);
|
|
99
|
-
|
|
100
|
-
if (args.includes('--help')) {
|
|
101
|
-
printHelp();
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const dryRun = args.includes('--dry-run');
|
|
106
|
-
const migrationDir = dirname(metaFilename);
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
serializeMigration(MigrationClass, migrationDir, dryRun);
|
|
110
|
-
} catch (err) {
|
|
111
|
-
process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
|
|
112
|
-
process.exitCode = 1;
|
|
113
|
-
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns true when `import.meta.url` resolves to the same file that was
|
|
91
|
+
* invoked as the node entrypoint (`process.argv[1]`). Used by
|
|
92
|
+
* `MigrationCLI.run` (in `@prisma-next/cli/migration-cli`) to no-op when
|
|
93
|
+
* the migration module is being imported (e.g. by another script) rather
|
|
94
|
+
* than executed directly.
|
|
95
|
+
*/
|
|
96
|
+
export function isDirectEntrypoint(importMetaUrl: string): boolean {
|
|
97
|
+
const metaFilename = fileURLToPath(importMetaUrl);
|
|
98
|
+
const argv1 = process.argv[1];
|
|
99
|
+
if (!argv1) return false;
|
|
100
|
+
try {
|
|
101
|
+
return realpathSync(metaFilename) === realpathSync(argv1);
|
|
102
|
+
} catch {
|
|
103
|
+
return false;
|
|
114
104
|
}
|
|
115
105
|
}
|
|
116
106
|
|
|
107
|
+
export function printMigrationHelp(): void {
|
|
108
|
+
printHelp();
|
|
109
|
+
}
|
|
110
|
+
|
|
117
111
|
function printHelp(): void {
|
|
118
112
|
process.stdout.write(
|
|
119
113
|
[
|
|
@@ -128,10 +122,25 @@ function printHelp(): void {
|
|
|
128
122
|
}
|
|
129
123
|
|
|
130
124
|
/**
|
|
131
|
-
*
|
|
125
|
+
* In-memory artifacts produced from a `Migration` instance: the
|
|
126
|
+
* serialized `ops.json` body, the `migration.json` manifest object, and
|
|
127
|
+
* its serialized form. Returned by `buildMigrationArtifacts` so callers
|
|
128
|
+
* (today: `MigrationCLI.run` in `@prisma-next/cli/migration-cli`) can
|
|
129
|
+
* decide how to persist them — write to disk, print in dry-run, ship
|
|
130
|
+
* over the wire — without coupling artifact construction to file I/O.
|
|
131
|
+
*/
|
|
132
|
+
export interface MigrationArtifacts {
|
|
133
|
+
readonly opsJson: string;
|
|
134
|
+
readonly manifest: MigrationManifest;
|
|
135
|
+
readonly manifestJson: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Build the attested manifest from `describe()`-derived metadata, the
|
|
140
|
+
* operations list, and the previously-scaffolded manifest (if any).
|
|
132
141
|
*
|
|
133
|
-
* When a `migration.json` already exists
|
|
134
|
-
*
|
|
142
|
+
* When a `migration.json` already exists for this package (the common
|
|
143
|
+
* case: it was scaffolded by `migration plan`), preserve the contract
|
|
135
144
|
* bookends, hints, labels, and `createdAt` set there — those fields are
|
|
136
145
|
* owned by the CLI scaffolder, not the authored class. Only the
|
|
137
146
|
* `describe()`-derived fields (`from`, `to`, `kind`) and the operations
|
|
@@ -144,12 +153,10 @@ function printHelp(): void {
|
|
|
144
153
|
* the on-disk artifacts are always fully attested.
|
|
145
154
|
*/
|
|
146
155
|
function buildAttestedManifest(
|
|
147
|
-
migrationDir: string,
|
|
148
156
|
meta: MigrationMeta,
|
|
149
157
|
ops: MigrationOps,
|
|
158
|
+
existing: Partial<MigrationManifest> | null,
|
|
150
159
|
): MigrationManifest {
|
|
151
|
-
const existing = readExistingManifest(join(migrationDir, 'migration.json'));
|
|
152
|
-
|
|
153
160
|
const baseManifest: Omit<MigrationManifest, 'migrationId'> = {
|
|
154
161
|
from: meta.from,
|
|
155
162
|
to: meta.to,
|
|
@@ -186,52 +193,35 @@ function normalizeHints(existing: MigrationHints | undefined): MigrationHints {
|
|
|
186
193
|
};
|
|
187
194
|
}
|
|
188
195
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
function serializeMigration(
|
|
204
|
-
MigrationClass: new () => Migration,
|
|
205
|
-
migrationDir: string,
|
|
206
|
-
dryRun: boolean,
|
|
207
|
-
): void {
|
|
208
|
-
const instance = new MigrationClass();
|
|
209
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Pure conversion from a `Migration` instance (plus the previously
|
|
198
|
+
* scaffolded manifest, when one exists on disk) to the in-memory
|
|
199
|
+
* artifacts that downstream tooling persists. Owns metadata validation,
|
|
200
|
+
* manifest synthesis/preservation, hint normalization, and the
|
|
201
|
+
* content-addressed `migrationId` computation, but performs no file I/O
|
|
202
|
+
* — callers handle reads (to source `existing`) and writes (to persist
|
|
203
|
+
* `opsJson` / `manifestJson`).
|
|
204
|
+
*/
|
|
205
|
+
export function buildMigrationArtifacts(
|
|
206
|
+
instance: Migration,
|
|
207
|
+
existing: Partial<MigrationManifest> | null,
|
|
208
|
+
): MigrationArtifacts {
|
|
210
209
|
const ops = instance.operations;
|
|
211
|
-
|
|
212
210
|
if (!Array.isArray(ops)) {
|
|
213
211
|
throw new Error('operations must be an array');
|
|
214
212
|
}
|
|
215
213
|
|
|
216
|
-
const serializedOps = JSON.stringify(ops, null, 2);
|
|
217
|
-
|
|
218
214
|
const rawMeta: unknown = instance.describe();
|
|
219
215
|
const parsed = MigrationMetaSchema(rawMeta);
|
|
220
216
|
if (parsed instanceof type.errors) {
|
|
221
217
|
throw new Error(`describe() returned invalid metadata: ${parsed.summary}`);
|
|
222
218
|
}
|
|
223
219
|
|
|
224
|
-
const manifest = buildAttestedManifest(
|
|
220
|
+
const manifest = buildAttestedManifest(parsed, ops, existing);
|
|
225
221
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
writeFileSync(join(migrationDir, 'ops.json'), serializedOps);
|
|
234
|
-
writeFileSync(join(migrationDir, 'migration.json'), JSON.stringify(manifest, null, 2));
|
|
235
|
-
|
|
236
|
-
process.stdout.write(`Wrote ops.json + migration.json to ${migrationDir}\n`);
|
|
222
|
+
return {
|
|
223
|
+
opsJson: JSON.stringify(ops, null, 2),
|
|
224
|
+
manifest,
|
|
225
|
+
manifestJson: JSON.stringify(manifest, null, 2),
|
|
226
|
+
};
|
|
237
227
|
}
|
package/src/migration-ts.ts
CHANGED
|
@@ -17,7 +17,11 @@ const MIGRATION_TS_FILE = 'migration.ts';
|
|
|
17
17
|
* Writes a pre-rendered `migration.ts` source string to the given package
|
|
18
18
|
* directory. If the source begins with a shebang, the file is written with
|
|
19
19
|
* executable permissions (0o755) so it can be run directly via
|
|
20
|
-
* `./migration.ts`
|
|
20
|
+
* `./migration.ts` — the rendered scaffold ends with
|
|
21
|
+
* `MigrationCLI.run(import.meta.url, M)` from
|
|
22
|
+
* `@prisma-next/cli/migration-cli` (re-exported by the postgres facade),
|
|
23
|
+
* which guards on the entrypoint and serializes when the file is the main
|
|
24
|
+
* module.
|
|
21
25
|
*
|
|
22
26
|
* The source is run through prettier before writing so migration renderers
|
|
23
27
|
* can produce structurally-correct but loosely-indented source and rely on
|