@prisma-next/migration-tools 0.4.1 → 0.5.0-dev.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.
@@ -11,7 +11,11 @@
11
11
  * Writes a pre-rendered `migration.ts` source string to the given package
12
12
  * directory. If the source begins with a shebang, the file is written with
13
13
  * executable permissions (0o755) so it can be run directly via
14
- * `./migration.ts` by the authoring class's `Migration.run(...)` guard.
14
+ * `./migration.ts` the rendered scaffold ends with
15
+ * `MigrationCLI.run(import.meta.url, M)` from
16
+ * `@prisma-next/cli/migration-cli` (re-exported by the postgres facade),
17
+ * which guards on the entrypoint and serializes when the file is the main
18
+ * module.
15
19
  *
16
20
  * The source is run through prettier before writing so migration renderers
17
21
  * can produce structurally-correct but loosely-indented source and rely on
@@ -1 +1 @@
1
- {"version":3,"file":"migration-ts.d.mts","names":[],"sources":["../../src/migration-ts.ts","../../src/runtime-detection.ts"],"sourcesContent":[],"mappings":";;AA0BA;AAsBA;;;;AChDA;AAEA;AAMA;;;;;;;;;;;iBDkBsB,gBAAA,uCAAuD;;;;iBAsBvD,cAAA,sBAAoC;;;KChD9C,eAAA;AD0BU,iBCxBN,qBAAA,CAAA,CDwBoE,ECxB3C,eDwB2C;AAsB9D,iBCxCN,cAAA,CDwC0C,OAAO,ECxCzB,eDwCyB,CAAA,EAAA,MAAA"}
1
+ {"version":3,"file":"migration-ts.d.mts","names":[],"sources":["../../src/migration-ts.ts","../../src/runtime-detection.ts"],"sourcesContent":[],"mappings":";;AA8BA;AAsBA;;;;ACpDA;AAEA;AAMA;;;;;;;;;;;;;;;iBDsBsB,gBAAA,uCAAuD;;;;iBAsBvD,cAAA,sBAAoC;;;KCpD9C,eAAA;AD8BU,iBC5BN,qBAAA,CAAA,CD4BoE,EC5B3C,eD4B2C;AAsB9D,iBC5CN,cAAA,CD4C0C,OAAO,EC5CzB,eD4CyB,CAAA,EAAA,MAAA"}
@@ -16,7 +16,11 @@ const MIGRATION_TS_FILE = "migration.ts";
16
16
  * Writes a pre-rendered `migration.ts` source string to the given package
17
17
  * directory. If the source begins with a shebang, the file is written with
18
18
  * executable permissions (0o755) so it can be run directly via
19
- * `./migration.ts` by the authoring class's `Migration.run(...)` guard.
19
+ * `./migration.ts` the rendered scaffold ends with
20
+ * `MigrationCLI.run(import.meta.url, M)` from
21
+ * `@prisma-next/cli/migration-cli` (re-exported by the postgres facade),
22
+ * which guards on the entrypoint and serializes when the file is the main
23
+ * module.
20
24
  *
21
25
  * The source is run through prettier before writing so migration renderers
22
26
  * can produce structurally-correct but loosely-indented source and rely on
@@ -1 +1 @@
1
- {"version":3,"file":"migration-ts.mjs","names":[],"sources":["../../src/migration-ts.ts","../../src/runtime-detection.ts"],"sourcesContent":["/**\n * Utilities for reading/writing `migration.ts` files.\n *\n * Rendering migration.ts source is the target's responsibility — the CLI\n * obtains source strings from a planner's `plan.renderTypeScript()`. The\n * helper here is limited to file I/O: writing the returned source with the\n * right executable bit and probing for existence.\n */\n\nimport { stat, writeFile } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { format } from 'prettier';\n\nconst MIGRATION_TS_FILE = 'migration.ts';\n\n/**\n * Writes a pre-rendered `migration.ts` source string to the given package\n * directory. If the source begins with a shebang, the file is written with\n * executable permissions (0o755) so it can be run directly via\n * `./migration.ts` by the authoring class's `Migration.run(...)` guard.\n *\n * The source is run through prettier before writing so migration renderers\n * can produce structurally-correct but loosely-indented source and rely on\n * a single canonical format on disk. Matches what `@prisma-next/emitter`\n * already does for generated `contract.d.ts`.\n */\nexport async function writeMigrationTs(packageDir: string, content: string): Promise<void> {\n const formatted = await formatMigrationTsSource(content);\n const isExecutable = formatted.startsWith('#!');\n await writeFile(\n join(packageDir, MIGRATION_TS_FILE),\n formatted,\n isExecutable ? { mode: 0o755 } : undefined,\n );\n}\n\nasync function formatMigrationTsSource(source: string): Promise<string> {\n return format(source, {\n parser: 'typescript',\n singleQuote: true,\n semi: true,\n printWidth: 100,\n });\n}\n\n/**\n * Checks whether a migration.ts file exists in the package directory.\n */\nexport async function hasMigrationTs(packageDir: string): Promise<boolean> {\n try {\n const s = await stat(join(packageDir, MIGRATION_TS_FILE));\n return s.isFile();\n } catch {\n return false;\n }\n}\n","export type ScaffoldRuntime = 'node' | 'bun' | 'deno';\n\nexport function detectScaffoldRuntime(): ScaffoldRuntime {\n if (typeof (globalThis as { Bun?: unknown }).Bun !== 'undefined') return 'bun';\n if (typeof (globalThis as { Deno?: unknown }).Deno !== 'undefined') return 'deno';\n return 'node';\n}\n\nexport function shebangLineFor(runtime: ScaffoldRuntime): string {\n switch (runtime) {\n case 'bun':\n return '#!/usr/bin/env -S bun';\n case 'deno':\n return '#!/usr/bin/env -S deno run -A';\n case 'node':\n return '#!/usr/bin/env -S node';\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAM,oBAAoB;;;;;;;;;;;;AAa1B,eAAsB,iBAAiB,YAAoB,SAAgC;CACzF,MAAM,YAAY,MAAM,wBAAwB,QAAQ;CACxD,MAAM,eAAe,UAAU,WAAW,KAAK;AAC/C,OAAM,UACJ,KAAK,YAAY,kBAAkB,EACnC,WACA,eAAe,EAAE,MAAM,KAAO,GAAG,OAClC;;AAGH,eAAe,wBAAwB,QAAiC;AACtE,QAAO,OAAO,QAAQ;EACpB,QAAQ;EACR,aAAa;EACb,MAAM;EACN,YAAY;EACb,CAAC;;;;;AAMJ,eAAsB,eAAe,YAAsC;AACzE,KAAI;AAEF,UADU,MAAM,KAAK,KAAK,YAAY,kBAAkB,CAAC,EAChD,QAAQ;SACX;AACN,SAAO;;;;;;ACnDX,SAAgB,wBAAyC;AACvD,KAAI,OAAQ,WAAiC,QAAQ,YAAa,QAAO;AACzE,KAAI,OAAQ,WAAkC,SAAS,YAAa,QAAO;AAC3E,QAAO;;AAGT,SAAgB,eAAe,SAAkC;AAC/D,SAAQ,SAAR;EACE,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,OACH,QAAO"}
1
+ {"version":3,"file":"migration-ts.mjs","names":[],"sources":["../../src/migration-ts.ts","../../src/runtime-detection.ts"],"sourcesContent":["/**\n * Utilities for reading/writing `migration.ts` files.\n *\n * Rendering migration.ts source is the target's responsibility — the CLI\n * obtains source strings from a planner's `plan.renderTypeScript()`. The\n * helper here is limited to file I/O: writing the returned source with the\n * right executable bit and probing for existence.\n */\n\nimport { stat, writeFile } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { format } from 'prettier';\n\nconst MIGRATION_TS_FILE = 'migration.ts';\n\n/**\n * Writes a pre-rendered `migration.ts` source string to the given package\n * directory. If the source begins with a shebang, the file is written with\n * executable permissions (0o755) so it can be run directly via\n * `./migration.ts` the rendered scaffold ends with\n * `MigrationCLI.run(import.meta.url, M)` from\n * `@prisma-next/cli/migration-cli` (re-exported by the postgres facade),\n * which guards on the entrypoint and serializes when the file is the main\n * module.\n *\n * The source is run through prettier before writing so migration renderers\n * can produce structurally-correct but loosely-indented source and rely on\n * a single canonical format on disk. Matches what `@prisma-next/emitter`\n * already does for generated `contract.d.ts`.\n */\nexport async function writeMigrationTs(packageDir: string, content: string): Promise<void> {\n const formatted = await formatMigrationTsSource(content);\n const isExecutable = formatted.startsWith('#!');\n await writeFile(\n join(packageDir, MIGRATION_TS_FILE),\n formatted,\n isExecutable ? { mode: 0o755 } : undefined,\n );\n}\n\nasync function formatMigrationTsSource(source: string): Promise<string> {\n return format(source, {\n parser: 'typescript',\n singleQuote: true,\n semi: true,\n printWidth: 100,\n });\n}\n\n/**\n * Checks whether a migration.ts file exists in the package directory.\n */\nexport async function hasMigrationTs(packageDir: string): Promise<boolean> {\n try {\n const s = await stat(join(packageDir, MIGRATION_TS_FILE));\n return s.isFile();\n } catch {\n return false;\n }\n}\n","export type ScaffoldRuntime = 'node' | 'bun' | 'deno';\n\nexport function detectScaffoldRuntime(): ScaffoldRuntime {\n if (typeof (globalThis as { Bun?: unknown }).Bun !== 'undefined') return 'bun';\n if (typeof (globalThis as { Deno?: unknown }).Deno !== 'undefined') return 'deno';\n return 'node';\n}\n\nexport function shebangLineFor(runtime: ScaffoldRuntime): string {\n switch (runtime) {\n case 'bun':\n return '#!/usr/bin/env -S bun';\n case 'deno':\n return '#!/usr/bin/env -S deno run -A';\n case 'node':\n return '#!/usr/bin/env -S node';\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAM,oBAAoB;;;;;;;;;;;;;;;;AAiB1B,eAAsB,iBAAiB,YAAoB,SAAgC;CACzF,MAAM,YAAY,MAAM,wBAAwB,QAAQ;CACxD,MAAM,eAAe,UAAU,WAAW,KAAK;AAC/C,OAAM,UACJ,KAAK,YAAY,kBAAkB,EACnC,WACA,eAAe,EAAE,MAAM,KAAO,GAAG,OAClC;;AAGH,eAAe,wBAAwB,QAAiC;AACtE,QAAO,OAAO,QAAQ;EACpB,QAAQ;EACR,aAAa;EACb,MAAM;EACN,YAAY;EACb,CAAC;;;;;AAMJ,eAAsB,eAAe,YAAsC;AACzE,KAAI;AAEF,UADU,MAAM,KAAK,KAAK,YAAY,kBAAkB,CAAC,EAChD,QAAQ;SACX;AACN,SAAO;;;;;;ACvDX,SAAgB,wBAAyC;AACvD,KAAI,OAAQ,WAAiC,QAAQ,YAAa,QAAO;AACzE,KAAI,OAAQ,WAAkC,SAAS,YAAa,QAAO;AAC3E,QAAO;;AAGT,SAAgB,eAAe,SAAkC;AAC/D,SAAQ,SAAR;EACE,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,OACH,QAAO"}
@@ -1,4 +1,5 @@
1
- import { MigrationPlan, MigrationPlanOperation } from "@prisma-next/framework-components/control";
1
+ import { a as MigrationManifest } from "../types-DyGXcWWp.mjs";
2
+ import { ControlStack, MigrationPlan, MigrationPlanOperation } from "@prisma-next/framework-components/control";
2
3
 
3
4
  //#region src/migration-base.d.ts
4
5
  interface MigrationMeta {
@@ -16,8 +17,19 @@ interface MigrationMeta {
16
17
  * every migration must implement — `migration.json` is required for a
17
18
  * migration to be valid.
18
19
  */
19
- declare abstract class Migration<TOperation extends MigrationPlanOperation = MigrationPlanOperation> implements MigrationPlan {
20
+ declare abstract class Migration<TOperation extends MigrationPlanOperation = MigrationPlanOperation, TFamilyId extends string = string, TTargetId extends string = string> implements MigrationPlan {
20
21
  abstract readonly targetId: string;
22
+ /**
23
+ * Assembled `ControlStack` injected by the orchestrator (`runMigration`).
24
+ *
25
+ * Subclasses (e.g. `PostgresMigration`) read the stack to materialize their
26
+ * adapter once per instance. Optional at the abstract level so unit tests can
27
+ * construct `Migration` instances purely for `operations` / `describe`
28
+ * assertions without needing a real stack; concrete subclasses that need the
29
+ * stack at runtime should narrow the parameter to required.
30
+ */
31
+ protected readonly stack: ControlStack<TFamilyId, TTargetId> | undefined;
32
+ constructor(stack?: ControlStack<TFamilyId, TTargetId>);
21
33
  /**
22
34
  * Ordered list of operations this migration performs.
23
35
  *
@@ -37,21 +49,39 @@ declare abstract class Migration<TOperation extends MigrationPlanOperation = Mig
37
49
  get destination(): {
38
50
  readonly storageHash: string;
39
51
  };
40
- /**
41
- * Entrypoint guard for migration files. When called at module scope,
42
- * detects whether the file is being run directly (e.g. `node migration.ts`)
43
- * and if so, serializes the migration plan to `ops.json` and
44
- * `migration.json` in the same directory. When the file is imported by
45
- * another module, this is a no-op.
46
- *
47
- * Usage (at module scope, after the class definition):
48
- *
49
- * class MyMigration extends Migration { ... }
50
- * export default MyMigration;
51
- * Migration.run(import.meta.url, MyMigration);
52
- */
53
- static run(importMetaUrl: string, MigrationClass: new () => Migration): void;
54
52
  }
53
+ /**
54
+ * Returns true when `import.meta.url` resolves to the same file that was
55
+ * invoked as the node entrypoint (`process.argv[1]`). Used by
56
+ * `MigrationCLI.run` (in `@prisma-next/cli/migration-cli`) to no-op when
57
+ * the migration module is being imported (e.g. by another script) rather
58
+ * than executed directly.
59
+ */
60
+ declare function isDirectEntrypoint(importMetaUrl: string): boolean;
61
+ declare function printMigrationHelp(): void;
62
+ /**
63
+ * In-memory artifacts produced from a `Migration` instance: the
64
+ * serialized `ops.json` body, the `migration.json` manifest object, and
65
+ * its serialized form. Returned by `buildMigrationArtifacts` so callers
66
+ * (today: `MigrationCLI.run` in `@prisma-next/cli/migration-cli`) can
67
+ * decide how to persist them — write to disk, print in dry-run, ship
68
+ * over the wire — without coupling artifact construction to file I/O.
69
+ */
70
+ interface MigrationArtifacts {
71
+ readonly opsJson: string;
72
+ readonly manifest: MigrationManifest;
73
+ readonly manifestJson: string;
74
+ }
75
+ /**
76
+ * Pure conversion from a `Migration` instance (plus the previously
77
+ * scaffolded manifest, when one exists on disk) to the in-memory
78
+ * artifacts that downstream tooling persists. Owns metadata validation,
79
+ * manifest synthesis/preservation, hint normalization, and the
80
+ * content-addressed `migrationId` computation, but performs no file I/O
81
+ * — callers handle reads (to source `existing`) and writes (to persist
82
+ * `opsJson` / `manifestJson`).
83
+ */
84
+ declare function buildMigrationArtifacts(instance: Migration, existing: Partial<MigrationManifest> | null): MigrationArtifacts;
55
85
  //#endregion
56
- export { Migration, type MigrationMeta };
86
+ export { Migration, type MigrationArtifacts, type MigrationMeta, buildMigrationArtifacts, isDirectEntrypoint, printMigrationHelp };
57
87
  //# sourceMappingURL=migration.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"migration.d.mts","names":[],"sources":["../../src/migration-base.ts"],"sourcesContent":[],"mappings":";;;UAaiB,aAAA;;EAAA,SAAA,EAAA,EAAA,MAAa;EAuBR,SAAA,IAAS,CAAA,EAAA,SAAA,GAAA,UAAA;EAAoB,SAAA,MAAA,CAAA,EAAA,SAAA,MAAA,EAAA;;;;;;;;;;;uBAA7B,6BAA6B,yBAAyB,mCAC/D;;;;;;;;sCAUyB;;;;;;uBAOf;;;;;;;;;;;;;;;;;;;;8DA4BuC"}
1
+ {"version":3,"file":"migration.d.mts","names":[],"sources":["../../src/migration-base.ts"],"sourcesContent":[],"mappings":";;;;UAaiB,aAAA;;EAAA,SAAA,EAAA,EAAA,MAAa;EAuBR,SAAA,IAAS,CAAA,EAAA,SAAA,GAAA,UAAA;EACV,SAAA,MAAA,CAAA,EAAA,SAAA,MAAA,EAAA;;;;;;;;;;;AAGK,uBAJJ,SAII,CAAA,mBAHL,sBAGK,GAHoB,sBAGpB,EAAA,kBAAA,MAAA,GAAA,MAAA,EAAA,kBAAA,MAAA,GAAA,MAAA,CAAA,YAAb,aAAa,CAAA;EAuDV,kBAAA,QAAkB,EAAA,MAAA;EAWlB;AAyBhB;AAyEA;;;;;;;4BAvJ4B,aAAa,WAAW;sBAE9B,aAAa,WAAW;;;;;;;sCAUR;;;;;;uBAOf;;;;;;;;;;;;;;;iBAuBP,kBAAA;iBAWA,kBAAA,CAAA;;;;;;;;;UAyBC,kBAAA;;qBAEI;;;;;;;;;;;;iBAuEL,uBAAA,WACJ,qBACA,QAAQ,4BACjB"}
@@ -1,9 +1,8 @@
1
1
  import "../io-CCnYsUHU.mjs";
2
2
  import { t as computeMigrationId } from "../attestation-DtF8tEOM.mjs";
3
3
  import { type } from "arktype";
4
- import { dirname, join } from "pathe";
5
4
  import { ifDefined } from "@prisma-next/utils/defined";
6
- import { readFileSync, realpathSync, writeFileSync } from "node:fs";
5
+ import { realpathSync } from "node:fs";
7
6
  import { fileURLToPath } from "node:url";
8
7
 
9
8
  //#region src/migration-base.ts
@@ -23,6 +22,19 @@ const MigrationMetaSchema = type({
23
22
  * migration to be valid.
24
23
  */
25
24
  var Migration = class {
25
+ /**
26
+ * Assembled `ControlStack` injected by the orchestrator (`runMigration`).
27
+ *
28
+ * Subclasses (e.g. `PostgresMigration`) read the stack to materialize their
29
+ * adapter once per instance. Optional at the abstract level so unit tests can
30
+ * construct `Migration` instances purely for `operations` / `describe`
31
+ * assertions without needing a real stack; concrete subclasses that need the
32
+ * stack at runtime should narrow the parameter to required.
33
+ */
34
+ stack;
35
+ constructor(stack) {
36
+ this.stack = stack;
37
+ }
26
38
  get origin() {
27
39
  const from = this.describe().from;
28
40
  return from === "" ? null : { storageHash: from };
@@ -30,46 +42,27 @@ var Migration = class {
30
42
  get destination() {
31
43
  return { storageHash: this.describe().to };
32
44
  }
33
- /**
34
- * Entrypoint guard for migration files. When called at module scope,
35
- * detects whether the file is being run directly (e.g. `node migration.ts`)
36
- * and if so, serializes the migration plan to `ops.json` and
37
- * `migration.json` in the same directory. When the file is imported by
38
- * another module, this is a no-op.
39
- *
40
- * Usage (at module scope, after the class definition):
41
- *
42
- * class MyMigration extends Migration { ... }
43
- * export default MyMigration;
44
- * Migration.run(import.meta.url, MyMigration);
45
- */
46
- static run(importMetaUrl, MigrationClass) {
47
- if (!importMetaUrl) return;
48
- const metaFilename = fileURLToPath(importMetaUrl);
49
- const argv1 = process.argv[1];
50
- if (!argv1) return;
51
- let isEntrypoint;
52
- try {
53
- isEntrypoint = realpathSync(metaFilename) === realpathSync(argv1);
54
- } catch {
55
- return;
56
- }
57
- if (!isEntrypoint) return;
58
- const args = process.argv.slice(2);
59
- if (args.includes("--help")) {
60
- printHelp();
61
- return;
62
- }
63
- const dryRun = args.includes("--dry-run");
64
- const migrationDir = dirname(metaFilename);
65
- try {
66
- serializeMigration(MigrationClass, migrationDir, dryRun);
67
- } catch (err) {
68
- process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
69
- process.exitCode = 1;
70
- }
71
- }
72
45
  };
46
+ /**
47
+ * Returns true when `import.meta.url` resolves to the same file that was
48
+ * invoked as the node entrypoint (`process.argv[1]`). Used by
49
+ * `MigrationCLI.run` (in `@prisma-next/cli/migration-cli`) to no-op when
50
+ * the migration module is being imported (e.g. by another script) rather
51
+ * than executed directly.
52
+ */
53
+ function isDirectEntrypoint(importMetaUrl) {
54
+ const metaFilename = fileURLToPath(importMetaUrl);
55
+ const argv1 = process.argv[1];
56
+ if (!argv1) return false;
57
+ try {
58
+ return realpathSync(metaFilename) === realpathSync(argv1);
59
+ } catch {
60
+ return false;
61
+ }
62
+ }
63
+ function printMigrationHelp() {
64
+ printHelp();
65
+ }
73
66
  function printHelp() {
74
67
  process.stdout.write([
75
68
  "Usage: node <migration-file> [options]",
@@ -81,10 +74,11 @@ function printHelp() {
81
74
  ].join("\n"));
82
75
  }
83
76
  /**
84
- * Build the attested manifest written by `Migration.run()`.
77
+ * Build the attested manifest from `describe()`-derived metadata, the
78
+ * operations list, and the previously-scaffolded manifest (if any).
85
79
  *
86
- * When a `migration.json` already exists in the directory (the common case:
87
- * the package was scaffolded by `migration plan`), preserve the contract
80
+ * When a `migration.json` already exists for this package (the common
81
+ * case: it was scaffolded by `migration plan`), preserve the contract
88
82
  * bookends, hints, labels, and `createdAt` set there — those fields are
89
83
  * owned by the CLI scaffolder, not the authored class. Only the
90
84
  * `describe()`-derived fields (`from`, `to`, `kind`) and the operations
@@ -96,8 +90,7 @@ function printHelp() {
96
90
  * The `migrationId` is recomputed against the current manifest + ops so
97
91
  * the on-disk artifacts are always fully attested.
98
92
  */
99
- function buildAttestedManifest(migrationDir, meta, ops) {
100
- const existing = readExistingManifest(join(migrationDir, "migration.json"));
93
+ function buildAttestedManifest(meta, ops, existing) {
101
94
  const baseManifest = {
102
95
  from: meta.from,
103
96
  to: meta.to,
@@ -129,38 +122,28 @@ function normalizeHints(existing) {
129
122
  plannerVersion: existing?.plannerVersion ?? "2.0.0"
130
123
  };
131
124
  }
132
- function readExistingManifest(manifestPath) {
133
- let raw;
134
- try {
135
- raw = readFileSync(manifestPath, "utf-8");
136
- } catch {
137
- return null;
138
- }
139
- try {
140
- return JSON.parse(raw);
141
- } catch {
142
- return null;
143
- }
144
- }
145
- function serializeMigration(MigrationClass, migrationDir, dryRun) {
146
- const instance = new MigrationClass();
125
+ /**
126
+ * Pure conversion from a `Migration` instance (plus the previously
127
+ * scaffolded manifest, when one exists on disk) to the in-memory
128
+ * artifacts that downstream tooling persists. Owns metadata validation,
129
+ * manifest synthesis/preservation, hint normalization, and the
130
+ * content-addressed `migrationId` computation, but performs no file I/O
131
+ * — callers handle reads (to source `existing`) and writes (to persist
132
+ * `opsJson` / `manifestJson`).
133
+ */
134
+ function buildMigrationArtifacts(instance, existing) {
147
135
  const ops = instance.operations;
148
136
  if (!Array.isArray(ops)) throw new Error("operations must be an array");
149
- const serializedOps = JSON.stringify(ops, null, 2);
150
137
  const parsed = MigrationMetaSchema(instance.describe());
151
138
  if (parsed instanceof type.errors) throw new Error(`describe() returned invalid metadata: ${parsed.summary}`);
152
- const manifest = buildAttestedManifest(migrationDir, parsed, ops);
153
- if (dryRun) {
154
- process.stdout.write(`--- migration.json ---\n${JSON.stringify(manifest, null, 2)}\n`);
155
- process.stdout.write("--- ops.json ---\n");
156
- process.stdout.write(`${serializedOps}\n`);
157
- return;
158
- }
159
- writeFileSync(join(migrationDir, "ops.json"), serializedOps);
160
- writeFileSync(join(migrationDir, "migration.json"), JSON.stringify(manifest, null, 2));
161
- process.stdout.write(`Wrote ops.json + migration.json to ${migrationDir}\n`);
139
+ const manifest = buildAttestedManifest(parsed, ops, existing);
140
+ return {
141
+ opsJson: JSON.stringify(ops, null, 2),
142
+ manifest,
143
+ manifestJson: JSON.stringify(manifest, null, 2)
144
+ };
162
145
  }
163
146
 
164
147
  //#endregion
165
- export { Migration };
148
+ export { Migration, buildMigrationArtifacts, isDirectEntrypoint, printMigrationHelp };
166
149
  //# sourceMappingURL=migration.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"migration.mjs","names":["isEntrypoint: boolean","baseManifest: Omit<MigrationManifest, 'migrationId'>","raw: string"],"sources":["../../src/migration-base.ts"],"sourcesContent":["import { readFileSync, realpathSync, writeFileSync } from 'node:fs';\nimport { fileURLToPath } from 'node:url';\nimport type { Contract } from '@prisma-next/contract/types';\nimport type {\n MigrationPlan,\n MigrationPlanOperation,\n} from '@prisma-next/framework-components/control';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { type } from 'arktype';\nimport { dirname, join } from 'pathe';\nimport { computeMigrationId } from './attestation';\nimport type { MigrationHints, MigrationManifest, MigrationOps } from './types';\n\nexport interface MigrationMeta {\n readonly from: string;\n readonly to: string;\n readonly kind?: 'regular' | 'baseline';\n readonly labels?: readonly string[];\n}\n\nconst MigrationMetaSchema = type({\n from: 'string',\n to: 'string',\n 'kind?': \"'regular' | 'baseline'\",\n 'labels?': type('string').array(),\n});\n\n/**\n * Base class for migrations.\n *\n * A `Migration` subclass is itself a `MigrationPlan`: CLI commands and the\n * runner can consume it directly via `targetId`, `operations`, `origin`, and\n * `destination`. The manifest-shaped inputs come from `describe()`, which\n * every migration must implement — `migration.json` is required for a\n * migration to be valid.\n */\nexport abstract class Migration<TOperation extends MigrationPlanOperation = MigrationPlanOperation>\n implements MigrationPlan\n{\n abstract readonly targetId: string;\n\n /**\n * Ordered list of operations this migration performs.\n *\n * Implemented as a getter so that subclasses can either precompute the list\n * in their constructor or build it lazily per access.\n */\n abstract get operations(): readonly TOperation[];\n\n /**\n * Metadata inputs used to build `migration.json` and to derive the plan's\n * origin/destination identities. Every migration must provide this —\n * omitting it would produce an invalid on-disk migration package.\n */\n abstract describe(): MigrationMeta;\n\n get origin(): { readonly storageHash: string } | null {\n const from = this.describe().from;\n // An empty `from` represents a migration with no prior origin (e.g.\n // initial baseline, or an in-process plan that was never persisted).\n // Surface that as a null origin so runners treat the plan as\n // origin-less rather than matching against an empty storage hash.\n return from === '' ? null : { storageHash: from };\n }\n\n get destination(): { readonly storageHash: string } {\n return { storageHash: this.describe().to };\n }\n\n /**\n * Entrypoint guard for migration files. When called at module scope,\n * detects whether the file is being run directly (e.g. `node migration.ts`)\n * and if so, serializes the migration plan to `ops.json` and\n * `migration.json` in the same directory. When the file is imported by\n * another module, this is a no-op.\n *\n * Usage (at module scope, after the class definition):\n *\n * class MyMigration extends Migration { ... }\n * export default MyMigration;\n * Migration.run(import.meta.url, MyMigration);\n */\n static run(importMetaUrl: string, MigrationClass: new () => Migration): void {\n if (!importMetaUrl) return;\n\n const metaFilename = fileURLToPath(importMetaUrl);\n const argv1 = process.argv[1];\n if (!argv1) return;\n\n let isEntrypoint: boolean;\n try {\n isEntrypoint = realpathSync(metaFilename) === realpathSync(argv1);\n } catch {\n return;\n }\n if (!isEntrypoint) return;\n\n const args = process.argv.slice(2);\n\n if (args.includes('--help')) {\n printHelp();\n return;\n }\n\n const dryRun = args.includes('--dry-run');\n const migrationDir = dirname(metaFilename);\n\n try {\n serializeMigration(MigrationClass, migrationDir, dryRun);\n } catch (err) {\n process.stderr.write(`${err instanceof Error ? err.message : String(err)}\\n`);\n process.exitCode = 1;\n }\n }\n}\n\nfunction printHelp(): void {\n process.stdout.write(\n [\n 'Usage: node <migration-file> [options]',\n '',\n 'Options:',\n ' --dry-run Print operations to stdout without writing files',\n ' --help Show this help message',\n '',\n ].join('\\n'),\n );\n}\n\n/**\n * Build the attested manifest written by `Migration.run()`.\n *\n * When a `migration.json` already exists in the directory (the common case:\n * the package was scaffolded by `migration plan`), preserve the contract\n * bookends, hints, labels, and `createdAt` set there — those fields are\n * owned by the CLI scaffolder, not the authored class. Only the\n * `describe()`-derived fields (`from`, `to`, `kind`) and the operations\n * change as the author iterates. When no manifest exists yet (a bare\n * `migration.ts` run from scratch), synthesize a minimal but\n * schema-conformant manifest so the resulting package can still be read,\n * verified, and applied.\n *\n * The `migrationId` is recomputed against the current manifest + ops so\n * the on-disk artifacts are always fully attested.\n */\nfunction buildAttestedManifest(\n migrationDir: string,\n meta: MigrationMeta,\n ops: MigrationOps,\n): MigrationManifest {\n const existing = readExistingManifest(join(migrationDir, 'migration.json'));\n\n const baseManifest: Omit<MigrationManifest, 'migrationId'> = {\n from: meta.from,\n to: meta.to,\n kind: meta.kind ?? 'regular',\n labels: meta.labels ?? existing?.labels ?? [],\n createdAt: existing?.createdAt ?? new Date().toISOString(),\n fromContract: existing?.fromContract ?? null,\n // When no scaffolded manifest exists we synthesize a minimal contract\n // stub so the package is still readable end-to-end. The cast is\n // intentional: only the storage bookend matters for hash computation\n // (everything else is stripped by `computeMigrationId`), and a real\n // contract bookend would only be available after `migration plan`.\n toContract: existing?.toContract ?? ({ storage: { storageHash: meta.to } } as Contract),\n hints: normalizeHints(existing?.hints),\n ...ifDefined('authorship', existing?.authorship),\n };\n\n const migrationId = computeMigrationId(baseManifest, ops);\n return { ...baseManifest, migrationId };\n}\n\n/**\n * Project `existing.hints` down to the known `MigrationHints` shape, dropping\n * any legacy keys that may linger in manifests scaffolded by older CLI\n * versions (e.g. `planningStrategy`). Picking fields explicitly instead of\n * spreading keeps refreshed `migration.json` files schema-clean regardless\n * of what was on disk before.\n */\nfunction normalizeHints(existing: MigrationHints | undefined): MigrationHints {\n return {\n used: existing?.used ?? [],\n applied: existing?.applied ?? [],\n plannerVersion: existing?.plannerVersion ?? '2.0.0',\n };\n}\n\nfunction readExistingManifest(manifestPath: string): Partial<MigrationManifest> | null {\n let raw: string;\n try {\n raw = readFileSync(manifestPath, 'utf-8');\n } catch {\n return null;\n }\n try {\n return JSON.parse(raw) as Partial<MigrationManifest>;\n } catch {\n return null;\n }\n}\n\nfunction serializeMigration(\n MigrationClass: new () => Migration,\n migrationDir: string,\n dryRun: boolean,\n): void {\n const instance = new MigrationClass();\n\n const ops = instance.operations;\n\n if (!Array.isArray(ops)) {\n throw new Error('operations must be an array');\n }\n\n const serializedOps = JSON.stringify(ops, null, 2);\n\n const rawMeta: unknown = instance.describe();\n const parsed = MigrationMetaSchema(rawMeta);\n if (parsed instanceof type.errors) {\n throw new Error(`describe() returned invalid metadata: ${parsed.summary}`);\n }\n\n const manifest = buildAttestedManifest(migrationDir, parsed, ops);\n\n if (dryRun) {\n process.stdout.write(`--- migration.json ---\\n${JSON.stringify(manifest, null, 2)}\\n`);\n process.stdout.write('--- ops.json ---\\n');\n process.stdout.write(`${serializedOps}\\n`);\n return;\n }\n\n writeFileSync(join(migrationDir, 'ops.json'), serializedOps);\n writeFileSync(join(migrationDir, 'migration.json'), JSON.stringify(manifest, null, 2));\n\n process.stdout.write(`Wrote ops.json + migration.json to ${migrationDir}\\n`);\n}\n"],"mappings":"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"}
1
+ {"version":3,"file":"migration.mjs","names":["baseManifest: Omit<MigrationManifest, 'migrationId'>"],"sources":["../../src/migration-base.ts"],"sourcesContent":["import { realpathSync } from 'node:fs';\nimport { fileURLToPath } from 'node:url';\nimport type { Contract } from '@prisma-next/contract/types';\nimport type {\n ControlStack,\n MigrationPlan,\n MigrationPlanOperation,\n} from '@prisma-next/framework-components/control';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { type } from 'arktype';\nimport { computeMigrationId } from './attestation';\nimport type { MigrationHints, MigrationManifest, MigrationOps } from './types';\n\nexport interface MigrationMeta {\n readonly from: string;\n readonly to: string;\n readonly kind?: 'regular' | 'baseline';\n readonly labels?: readonly string[];\n}\n\nconst MigrationMetaSchema = type({\n from: 'string',\n to: 'string',\n 'kind?': \"'regular' | 'baseline'\",\n 'labels?': type('string').array(),\n});\n\n/**\n * Base class for migrations.\n *\n * A `Migration` subclass is itself a `MigrationPlan`: CLI commands and the\n * runner can consume it directly via `targetId`, `operations`, `origin`, and\n * `destination`. The manifest-shaped inputs come from `describe()`, which\n * every migration must implement — `migration.json` is required for a\n * migration to be valid.\n */\nexport abstract class Migration<\n TOperation extends MigrationPlanOperation = MigrationPlanOperation,\n TFamilyId extends string = string,\n TTargetId extends string = string,\n> implements MigrationPlan\n{\n abstract readonly targetId: string;\n\n /**\n * Assembled `ControlStack` injected by the orchestrator (`runMigration`).\n *\n * Subclasses (e.g. `PostgresMigration`) read the stack to materialize their\n * adapter once per instance. Optional at the abstract level so unit tests can\n * construct `Migration` instances purely for `operations` / `describe`\n * assertions without needing a real stack; concrete subclasses that need the\n * stack at runtime should narrow the parameter to required.\n */\n protected readonly stack: ControlStack<TFamilyId, TTargetId> | undefined;\n\n constructor(stack?: ControlStack<TFamilyId, TTargetId>) {\n this.stack = stack;\n }\n\n /**\n * Ordered list of operations this migration performs.\n *\n * Implemented as a getter so that subclasses can either precompute the list\n * in their constructor or build it lazily per access.\n */\n abstract get operations(): readonly TOperation[];\n\n /**\n * Metadata inputs used to build `migration.json` and to derive the plan's\n * origin/destination identities. Every migration must provide this —\n * omitting it would produce an invalid on-disk migration package.\n */\n abstract describe(): MigrationMeta;\n\n get origin(): { readonly storageHash: string } | null {\n const from = this.describe().from;\n // An empty `from` represents a migration with no prior origin (e.g.\n // initial baseline, or an in-process plan that was never persisted).\n // Surface that as a null origin so runners treat the plan as\n // origin-less rather than matching against an empty storage hash.\n return from === '' ? null : { storageHash: from };\n }\n\n get destination(): { readonly storageHash: string } {\n return { storageHash: this.describe().to };\n }\n}\n\n/**\n * Returns true when `import.meta.url` resolves to the same file that was\n * invoked as the node entrypoint (`process.argv[1]`). Used by\n * `MigrationCLI.run` (in `@prisma-next/cli/migration-cli`) to no-op when\n * the migration module is being imported (e.g. by another script) rather\n * than executed directly.\n */\nexport function isDirectEntrypoint(importMetaUrl: string): boolean {\n const metaFilename = fileURLToPath(importMetaUrl);\n const argv1 = process.argv[1];\n if (!argv1) return false;\n try {\n return realpathSync(metaFilename) === realpathSync(argv1);\n } catch {\n return false;\n }\n}\n\nexport function printMigrationHelp(): void {\n printHelp();\n}\n\nfunction printHelp(): void {\n process.stdout.write(\n [\n 'Usage: node <migration-file> [options]',\n '',\n 'Options:',\n ' --dry-run Print operations to stdout without writing files',\n ' --help Show this help message',\n '',\n ].join('\\n'),\n );\n}\n\n/**\n * In-memory artifacts produced from a `Migration` instance: the\n * serialized `ops.json` body, the `migration.json` manifest object, and\n * its serialized form. Returned by `buildMigrationArtifacts` so callers\n * (today: `MigrationCLI.run` in `@prisma-next/cli/migration-cli`) can\n * decide how to persist them — write to disk, print in dry-run, ship\n * over the wire — without coupling artifact construction to file I/O.\n */\nexport interface MigrationArtifacts {\n readonly opsJson: string;\n readonly manifest: MigrationManifest;\n readonly manifestJson: string;\n}\n\n/**\n * Build the attested manifest from `describe()`-derived metadata, the\n * operations list, and the previously-scaffolded manifest (if any).\n *\n * When a `migration.json` already exists for this package (the common\n * case: it was scaffolded by `migration plan`), preserve the contract\n * bookends, hints, labels, and `createdAt` set there — those fields are\n * owned by the CLI scaffolder, not the authored class. Only the\n * `describe()`-derived fields (`from`, `to`, `kind`) and the operations\n * change as the author iterates. When no manifest exists yet (a bare\n * `migration.ts` run from scratch), synthesize a minimal but\n * schema-conformant manifest so the resulting package can still be read,\n * verified, and applied.\n *\n * The `migrationId` is recomputed against the current manifest + ops so\n * the on-disk artifacts are always fully attested.\n */\nfunction buildAttestedManifest(\n meta: MigrationMeta,\n ops: MigrationOps,\n existing: Partial<MigrationManifest> | null,\n): MigrationManifest {\n const baseManifest: Omit<MigrationManifest, 'migrationId'> = {\n from: meta.from,\n to: meta.to,\n kind: meta.kind ?? 'regular',\n labels: meta.labels ?? existing?.labels ?? [],\n createdAt: existing?.createdAt ?? new Date().toISOString(),\n fromContract: existing?.fromContract ?? null,\n // When no scaffolded manifest exists we synthesize a minimal contract\n // stub so the package is still readable end-to-end. The cast is\n // intentional: only the storage bookend matters for hash computation\n // (everything else is stripped by `computeMigrationId`), and a real\n // contract bookend would only be available after `migration plan`.\n toContract: existing?.toContract ?? ({ storage: { storageHash: meta.to } } as Contract),\n hints: normalizeHints(existing?.hints),\n ...ifDefined('authorship', existing?.authorship),\n };\n\n const migrationId = computeMigrationId(baseManifest, ops);\n return { ...baseManifest, migrationId };\n}\n\n/**\n * Project `existing.hints` down to the known `MigrationHints` shape, dropping\n * any legacy keys that may linger in manifests scaffolded by older CLI\n * versions (e.g. `planningStrategy`). Picking fields explicitly instead of\n * spreading keeps refreshed `migration.json` files schema-clean regardless\n * of what was on disk before.\n */\nfunction normalizeHints(existing: MigrationHints | undefined): MigrationHints {\n return {\n used: existing?.used ?? [],\n applied: existing?.applied ?? [],\n plannerVersion: existing?.plannerVersion ?? '2.0.0',\n };\n}\n\n/**\n * Pure conversion from a `Migration` instance (plus the previously\n * scaffolded manifest, when one exists on disk) to the in-memory\n * artifacts that downstream tooling persists. Owns metadata validation,\n * manifest synthesis/preservation, hint normalization, and the\n * content-addressed `migrationId` computation, but performs no file I/O\n * — callers handle reads (to source `existing`) and writes (to persist\n * `opsJson` / `manifestJson`).\n */\nexport function buildMigrationArtifacts(\n instance: Migration,\n existing: Partial<MigrationManifest> | null,\n): MigrationArtifacts {\n const ops = instance.operations;\n if (!Array.isArray(ops)) {\n throw new Error('operations must be an array');\n }\n\n const rawMeta: unknown = instance.describe();\n const parsed = MigrationMetaSchema(rawMeta);\n if (parsed instanceof type.errors) {\n throw new Error(`describe() returned invalid metadata: ${parsed.summary}`);\n }\n\n const manifest = buildAttestedManifest(parsed, ops, existing);\n\n return {\n opsJson: JSON.stringify(ops, null, 2),\n manifest,\n manifestJson: JSON.stringify(manifest, null, 2),\n };\n}\n"],"mappings":";;;;;;;;AAoBA,MAAM,sBAAsB,KAAK;CAC/B,MAAM;CACN,IAAI;CACJ,SAAS;CACT,WAAW,KAAK,SAAS,CAAC,OAAO;CAClC,CAAC;;;;;;;;;;AAWF,IAAsB,YAAtB,MAKA;;;;;;;;;;CAYE,AAAmB;CAEnB,YAAY,OAA4C;AACtD,OAAK,QAAQ;;CAkBf,IAAI,SAAkD;EACpD,MAAM,OAAO,KAAK,UAAU,CAAC;AAK7B,SAAO,SAAS,KAAK,OAAO,EAAE,aAAa,MAAM;;CAGnD,IAAI,cAAgD;AAClD,SAAO,EAAE,aAAa,KAAK,UAAU,CAAC,IAAI;;;;;;;;;;AAW9C,SAAgB,mBAAmB,eAAgC;CACjE,MAAM,eAAe,cAAc,cAAc;CACjD,MAAM,QAAQ,QAAQ,KAAK;AAC3B,KAAI,CAAC,MAAO,QAAO;AACnB,KAAI;AACF,SAAO,aAAa,aAAa,KAAK,aAAa,MAAM;SACnD;AACN,SAAO;;;AAIX,SAAgB,qBAA2B;AACzC,YAAW;;AAGb,SAAS,YAAkB;AACzB,SAAQ,OAAO,MACb;EACE;EACA;EACA;EACA;EACA;EACA;EACD,CAAC,KAAK,KAAK,CACb;;;;;;;;;;;;;;;;;;;AAkCH,SAAS,sBACP,MACA,KACA,UACmB;CACnB,MAAMA,eAAuD;EAC3D,MAAM,KAAK;EACX,IAAI,KAAK;EACT,MAAM,KAAK,QAAQ;EACnB,QAAQ,KAAK,UAAU,UAAU,UAAU,EAAE;EAC7C,WAAW,UAAU,8BAAa,IAAI,MAAM,EAAC,aAAa;EAC1D,cAAc,UAAU,gBAAgB;EAMxC,YAAY,UAAU,cAAe,EAAE,SAAS,EAAE,aAAa,KAAK,IAAI,EAAE;EAC1E,OAAO,eAAe,UAAU,MAAM;EACtC,GAAG,UAAU,cAAc,UAAU,WAAW;EACjD;CAED,MAAM,cAAc,mBAAmB,cAAc,IAAI;AACzD,QAAO;EAAE,GAAG;EAAc;EAAa;;;;;;;;;AAUzC,SAAS,eAAe,UAAsD;AAC5E,QAAO;EACL,MAAM,UAAU,QAAQ,EAAE;EAC1B,SAAS,UAAU,WAAW,EAAE;EAChC,gBAAgB,UAAU,kBAAkB;EAC7C;;;;;;;;;;;AAYH,SAAgB,wBACd,UACA,UACoB;CACpB,MAAM,MAAM,SAAS;AACrB,KAAI,CAAC,MAAM,QAAQ,IAAI,CACrB,OAAM,IAAI,MAAM,8BAA8B;CAIhD,MAAM,SAAS,oBADU,SAAS,UAAU,CACD;AAC3C,KAAI,kBAAkB,KAAK,OACzB,OAAM,IAAI,MAAM,yCAAyC,OAAO,UAAU;CAG5E,MAAM,WAAW,sBAAsB,QAAQ,KAAK,SAAS;AAE7D,QAAO;EACL,SAAS,KAAK,UAAU,KAAK,MAAM,EAAE;EACrC;EACA,cAAc,KAAK,UAAU,UAAU,MAAM,EAAE;EAChD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma-next/migration-tools",
3
- "version": "0.4.1",
3
+ "version": "0.5.0-dev.2",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "On-disk migration persistence, attestation, and chain reconstruction for Prisma Next",
@@ -8,16 +8,16 @@
8
8
  "arktype": "^2.1.29",
9
9
  "pathe": "^2.0.3",
10
10
  "prettier": "^3.6.2",
11
- "@prisma-next/contract": "0.4.1",
12
- "@prisma-next/framework-components": "0.4.1",
13
- "@prisma-next/utils": "0.4.1"
11
+ "@prisma-next/contract": "0.5.0-dev.2",
12
+ "@prisma-next/framework-components": "0.5.0-dev.2",
13
+ "@prisma-next/utils": "0.5.0-dev.2"
14
14
  },
15
15
  "devDependencies": {
16
16
  "tsdown": "0.18.4",
17
17
  "typescript": "5.9.3",
18
18
  "vitest": "4.0.17",
19
- "@prisma-next/tsconfig": "0.0.0",
20
- "@prisma-next/tsdown": "0.0.0"
19
+ "@prisma-next/tsdown": "0.0.0",
20
+ "@prisma-next/tsconfig": "0.0.0"
21
21
  },
22
22
  "files": [
23
23
  "dist",
@@ -1 +1,8 @@
1
- export { Migration, type MigrationMeta } from '../migration-base';
1
+ export {
2
+ buildMigrationArtifacts,
3
+ isDirectEntrypoint,
4
+ Migration,
5
+ type MigrationArtifacts,
6
+ type MigrationMeta,
7
+ printMigrationHelp,
8
+ } from '../migration-base';
@@ -1,13 +1,13 @@
1
- import { readFileSync, realpathSync, writeFileSync } from 'node:fs';
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<TOperation extends MigrationPlanOperation = MigrationPlanOperation>
38
- implements MigrationPlan
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
- * Entrypoint guard for migration files. When called at module scope,
72
- * detects whether the file is being run directly (e.g. `node migration.ts`)
73
- * and if so, serializes the migration plan to `ops.json` and
74
- * `migration.json` in the same directory. When the file is imported by
75
- * another module, this is a no-op.
76
- *
77
- * Usage (at module scope, after the class definition):
78
- *
79
- * class MyMigration extends Migration { ... }
80
- * export default MyMigration;
81
- * Migration.run(import.meta.url, MyMigration);
82
- */
83
- static run(importMetaUrl: string, MigrationClass: new () => Migration): void {
84
- if (!importMetaUrl) return;
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
- * Build the attested manifest written by `Migration.run()`.
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 in the directory (the common case:
134
- * the package was scaffolded by `migration plan`), preserve the contract
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
- function readExistingManifest(manifestPath: string): Partial<MigrationManifest> | null {
190
- let raw: string;
191
- try {
192
- raw = readFileSync(manifestPath, 'utf-8');
193
- } catch {
194
- return null;
195
- }
196
- try {
197
- return JSON.parse(raw) as Partial<MigrationManifest>;
198
- } catch {
199
- return null;
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(migrationDir, parsed, ops);
220
+ const manifest = buildAttestedManifest(parsed, ops, existing);
225
221
 
226
- if (dryRun) {
227
- process.stdout.write(`--- migration.json ---\n${JSON.stringify(manifest, null, 2)}\n`);
228
- process.stdout.write('--- ops.json ---\n');
229
- process.stdout.write(`${serializedOps}\n`);
230
- return;
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
  }
@@ -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` by the authoring class's `Migration.run(...)` guard.
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