@prisma-next/migration-tools 0.4.0-dev.9 → 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/README.md +1 -1
- package/dist/{attestation-DnebS4XZ.mjs → attestation-BnzTb0Qp.mjs} +24 -23
- package/dist/attestation-BnzTb0Qp.mjs.map +1 -0
- package/dist/{errors-C_XuSbX7.mjs → errors-BmiSgz1j.mjs} +14 -7
- package/dist/errors-BmiSgz1j.mjs.map +1 -0
- package/dist/exports/attestation.d.mts +20 -6
- package/dist/exports/attestation.d.mts.map +1 -1
- package/dist/exports/attestation.mjs +3 -3
- package/dist/exports/dag.d.mts +8 -6
- package/dist/exports/dag.d.mts.map +1 -1
- package/dist/exports/dag.mjs +181 -107
- package/dist/exports/dag.mjs.map +1 -1
- package/dist/exports/io.d.mts +16 -13
- package/dist/exports/io.d.mts.map +1 -1
- package/dist/exports/io.mjs +2 -2
- package/dist/exports/migration-ts.d.mts +15 -21
- package/dist/exports/migration-ts.d.mts.map +1 -1
- package/dist/exports/migration-ts.mjs +28 -36
- package/dist/exports/migration-ts.mjs.map +1 -1
- package/dist/exports/migration.d.mts +48 -18
- package/dist/exports/migration.d.mts.map +1 -1
- package/dist/exports/migration.mjs +75 -85
- 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.d.mts +2 -2
- package/dist/exports/types.mjs +2 -16
- package/dist/{io-Cun81AIZ.mjs → io-Cd6GLyjK.mjs} +18 -22
- package/dist/io-Cd6GLyjK.mjs.map +1 -0
- package/dist/types-DyGXcWWp.d.mts +71 -0
- package/dist/types-DyGXcWWp.d.mts.map +1 -0
- package/package.json +5 -4
- package/src/attestation.ts +34 -26
- package/src/dag.ts +140 -154
- package/src/errors.ts +8 -0
- package/src/exports/attestation.ts +2 -1
- package/src/exports/io.ts +1 -1
- package/src/exports/migration-ts.ts +1 -1
- package/src/exports/migration.ts +8 -1
- package/src/exports/refs.ts +10 -2
- package/src/exports/types.ts +2 -8
- package/src/graph-ops.ts +65 -0
- package/src/io.ts +23 -24
- package/src/migration-base.ts +99 -101
- package/src/migration-ts.ts +28 -50
- package/src/queue.ts +37 -0
- package/src/refs.ts +148 -37
- package/src/types.ts +15 -55
- package/dist/attestation-DnebS4XZ.mjs.map +0 -1
- package/dist/errors-C_XuSbX7.mjs.map +0 -1
- package/dist/exports/types.mjs.map +0 -1
- package/dist/io-Cun81AIZ.mjs.map +0 -1
- package/dist/types-D2uX4ql7.d.mts +0 -100
- package/dist/types-D2uX4ql7.d.mts.map +0 -1
package/src/migration-base.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
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
|
-
import type { MigrationManifest, MigrationOps } from './types';
|
|
12
|
+
import type { MigrationHints, MigrationManifest, MigrationOps } from './types';
|
|
13
13
|
|
|
14
14
|
export interface MigrationMeta {
|
|
15
15
|
readonly from: string;
|
|
@@ -26,7 +26,7 @@ const MigrationMetaSchema = type({
|
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* Base class for
|
|
29
|
+
* Base class for migrations.
|
|
30
30
|
*
|
|
31
31
|
* A `Migration` subclass is itself a `MigrationPlan`: CLI commands and the
|
|
32
32
|
* runner can consume it directly via `targetId`, `operations`, `origin`, and
|
|
@@ -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
|
|
@@ -140,19 +149,15 @@ function printHelp(): void {
|
|
|
140
149
|
* schema-conformant manifest so the resulting package can still be read,
|
|
141
150
|
* verified, and applied.
|
|
142
151
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
* draft (`migrationId: null`) ever leaves this function.
|
|
152
|
+
* The `migrationId` is recomputed against the current manifest + ops so
|
|
153
|
+
* the on-disk artifacts are always fully attested.
|
|
146
154
|
*/
|
|
147
155
|
function buildAttestedManifest(
|
|
148
|
-
migrationDir: string,
|
|
149
156
|
meta: MigrationMeta,
|
|
150
157
|
ops: MigrationOps,
|
|
158
|
+
existing: Partial<MigrationManifest> | null,
|
|
151
159
|
): MigrationManifest {
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
const baseManifest: MigrationManifest = {
|
|
155
|
-
migrationId: null,
|
|
160
|
+
const baseManifest: Omit<MigrationManifest, 'migrationId'> = {
|
|
156
161
|
from: meta.from,
|
|
157
162
|
to: meta.to,
|
|
158
163
|
kind: meta.kind ?? 'regular',
|
|
@@ -165,12 +170,7 @@ function buildAttestedManifest(
|
|
|
165
170
|
// (everything else is stripped by `computeMigrationId`), and a real
|
|
166
171
|
// contract bookend would only be available after `migration plan`.
|
|
167
172
|
toContract: existing?.toContract ?? ({ storage: { storageHash: meta.to } } as Contract),
|
|
168
|
-
hints: existing?.hints
|
|
169
|
-
used: [],
|
|
170
|
-
applied: [],
|
|
171
|
-
plannerVersion: '2.0.0',
|
|
172
|
-
planningStrategy: 'class-based',
|
|
173
|
-
},
|
|
173
|
+
hints: normalizeHints(existing?.hints),
|
|
174
174
|
...ifDefined('authorship', existing?.authorship),
|
|
175
175
|
};
|
|
176
176
|
|
|
@@ -178,52 +178,50 @@ function buildAttestedManifest(
|
|
|
178
178
|
return { ...baseManifest, migrationId };
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Project `existing.hints` down to the known `MigrationHints` shape, dropping
|
|
183
|
+
* any legacy keys that may linger in manifests scaffolded by older CLI
|
|
184
|
+
* versions (e.g. `planningStrategy`). Picking fields explicitly instead of
|
|
185
|
+
* spreading keeps refreshed `migration.json` files schema-clean regardless
|
|
186
|
+
* of what was on disk before.
|
|
187
|
+
*/
|
|
188
|
+
function normalizeHints(existing: MigrationHints | undefined): MigrationHints {
|
|
189
|
+
return {
|
|
190
|
+
used: existing?.used ?? [],
|
|
191
|
+
applied: existing?.applied ?? [],
|
|
192
|
+
plannerVersion: existing?.plannerVersion ?? '2.0.0',
|
|
193
|
+
};
|
|
193
194
|
}
|
|
194
195
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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 {
|
|
202
209
|
const ops = instance.operations;
|
|
203
|
-
|
|
204
210
|
if (!Array.isArray(ops)) {
|
|
205
211
|
throw new Error('operations must be an array');
|
|
206
212
|
}
|
|
207
213
|
|
|
208
|
-
const serializedOps = JSON.stringify(ops, null, 2);
|
|
209
|
-
|
|
210
214
|
const rawMeta: unknown = instance.describe();
|
|
211
215
|
const parsed = MigrationMetaSchema(rawMeta);
|
|
212
216
|
if (parsed instanceof type.errors) {
|
|
213
217
|
throw new Error(`describe() returned invalid metadata: ${parsed.summary}`);
|
|
214
218
|
}
|
|
215
219
|
|
|
216
|
-
const manifest = buildAttestedManifest(
|
|
217
|
-
|
|
218
|
-
if (dryRun) {
|
|
219
|
-
process.stdout.write(`--- migration.json ---\n${JSON.stringify(manifest, null, 2)}\n`);
|
|
220
|
-
process.stdout.write('--- ops.json ---\n');
|
|
221
|
-
process.stdout.write(`${serializedOps}\n`);
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
220
|
+
const manifest = buildAttestedManifest(parsed, ops, existing);
|
|
224
221
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
222
|
+
return {
|
|
223
|
+
opsJson: JSON.stringify(ops, null, 2),
|
|
224
|
+
manifest,
|
|
225
|
+
manifestJson: JSON.stringify(manifest, null, 2),
|
|
226
|
+
};
|
|
229
227
|
}
|
package/src/migration-ts.ts
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Utilities for reading/writing `migration.ts` files.
|
|
3
3
|
*
|
|
4
|
-
* Rendering migration.ts source is
|
|
5
|
-
* obtains source strings
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* here is limited to file I/O: writing the returned source with the right
|
|
9
|
-
* executable bit, probing for existence, and evaluating legacy descriptor-
|
|
10
|
-
* flow files.
|
|
4
|
+
* Rendering migration.ts source is the target's responsibility — the CLI
|
|
5
|
+
* obtains source strings from a planner's `plan.renderTypeScript()`. The
|
|
6
|
+
* helper here is limited to file I/O: writing the returned source with the
|
|
7
|
+
* right executable bit and probing for existence.
|
|
11
8
|
*/
|
|
12
9
|
|
|
13
10
|
import { stat, writeFile } from 'node:fs/promises';
|
|
14
|
-
import { join
|
|
11
|
+
import { join } from 'pathe';
|
|
12
|
+
import { format } from 'prettier';
|
|
15
13
|
|
|
16
14
|
const MIGRATION_TS_FILE = 'migration.ts';
|
|
17
15
|
|
|
@@ -19,17 +17,36 @@ const MIGRATION_TS_FILE = 'migration.ts';
|
|
|
19
17
|
* Writes a pre-rendered `migration.ts` source string to the given package
|
|
20
18
|
* directory. If the source begins with a shebang, the file is written with
|
|
21
19
|
* executable permissions (0o755) so it can be run directly via
|
|
22
|
-
* `./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.
|
|
25
|
+
*
|
|
26
|
+
* The source is run through prettier before writing so migration renderers
|
|
27
|
+
* can produce structurally-correct but loosely-indented source and rely on
|
|
28
|
+
* a single canonical format on disk. Matches what `@prisma-next/emitter`
|
|
29
|
+
* already does for generated `contract.d.ts`.
|
|
23
30
|
*/
|
|
24
31
|
export async function writeMigrationTs(packageDir: string, content: string): Promise<void> {
|
|
25
|
-
const
|
|
32
|
+
const formatted = await formatMigrationTsSource(content);
|
|
33
|
+
const isExecutable = formatted.startsWith('#!');
|
|
26
34
|
await writeFile(
|
|
27
35
|
join(packageDir, MIGRATION_TS_FILE),
|
|
28
|
-
|
|
36
|
+
formatted,
|
|
29
37
|
isExecutable ? { mode: 0o755 } : undefined,
|
|
30
38
|
);
|
|
31
39
|
}
|
|
32
40
|
|
|
41
|
+
async function formatMigrationTsSource(source: string): Promise<string> {
|
|
42
|
+
return format(source, {
|
|
43
|
+
parser: 'typescript',
|
|
44
|
+
singleQuote: true,
|
|
45
|
+
semi: true,
|
|
46
|
+
printWidth: 100,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
33
50
|
/**
|
|
34
51
|
* Checks whether a migration.ts file exists in the package directory.
|
|
35
52
|
*/
|
|
@@ -41,42 +58,3 @@ export async function hasMigrationTs(packageDir: string): Promise<boolean> {
|
|
|
41
58
|
return false;
|
|
42
59
|
}
|
|
43
60
|
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Evaluates a descriptor-flow migration.ts file by loading it via native
|
|
47
|
-
* Node import. Returns the result of calling the default export (expected
|
|
48
|
-
* to be a function returning an array of operation descriptors).
|
|
49
|
-
*
|
|
50
|
-
* Class-flow migration.ts files use a different shape — their default
|
|
51
|
-
* export is a class that extends `Migration` — and are evaluated by the
|
|
52
|
-
* target's `emit` capability, not this helper.
|
|
53
|
-
*
|
|
54
|
-
* Requires Node ≥24 for native TypeScript support.
|
|
55
|
-
*/
|
|
56
|
-
export async function evaluateMigrationTs(packageDir: string): Promise<readonly unknown[]> {
|
|
57
|
-
const filePath = resolve(join(packageDir, MIGRATION_TS_FILE));
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
await stat(filePath);
|
|
61
|
-
} catch {
|
|
62
|
-
throw new Error(`migration.ts not found at "${filePath}"`);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const mod = (await import(filePath)) as { default?: unknown };
|
|
66
|
-
|
|
67
|
-
if (typeof mod.default !== 'function') {
|
|
68
|
-
throw new Error(
|
|
69
|
-
`migration.ts must export a default function returning an operation list. Got: ${typeof mod.default}`,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const result: unknown = mod.default();
|
|
74
|
-
|
|
75
|
-
if (!Array.isArray(result)) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`migration.ts default export must return an array of operations. Got: ${typeof result}`,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return result;
|
|
82
|
-
}
|
package/src/queue.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FIFO queue with amortised O(1) push and shift.
|
|
3
|
+
*
|
|
4
|
+
* Uses a head-index cursor over a backing array rather than
|
|
5
|
+
* `Array.prototype.shift()`, which is O(n) on V8. Intended for BFS-shaped
|
|
6
|
+
* traversals where the queue is drained in a single pass — it does not
|
|
7
|
+
* reclaim memory for already-shifted items, so it is not suitable for
|
|
8
|
+
* long-lived queues with many push/shift cycles.
|
|
9
|
+
*/
|
|
10
|
+
export class Queue<T> {
|
|
11
|
+
private readonly items: T[];
|
|
12
|
+
private head = 0;
|
|
13
|
+
|
|
14
|
+
constructor(initial: Iterable<T> = []) {
|
|
15
|
+
this.items = [...initial];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
push(item: T): void {
|
|
19
|
+
this.items.push(item);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Remove and return the next item. Caller must check `isEmpty` first —
|
|
24
|
+
* shifting an empty queue throws.
|
|
25
|
+
*/
|
|
26
|
+
shift(): T {
|
|
27
|
+
if (this.head >= this.items.length) {
|
|
28
|
+
throw new Error('Queue.shift called on empty queue');
|
|
29
|
+
}
|
|
30
|
+
// biome-ignore lint/style/noNonNullAssertion: bounds-checked on the line above
|
|
31
|
+
return this.items[this.head++]!;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
get isEmpty(): boolean {
|
|
35
|
+
return this.head >= this.items.length;
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/refs.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { mkdir, readdir, readFile, rename, rmdir, unlink, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { type } from 'arktype';
|
|
3
|
-
import { dirname, join } from 'pathe';
|
|
3
|
+
import { dirname, join, relative } from 'pathe';
|
|
4
4
|
import {
|
|
5
|
+
errorInvalidRefFile,
|
|
5
6
|
errorInvalidRefName,
|
|
6
|
-
errorInvalidRefs,
|
|
7
7
|
errorInvalidRefValue,
|
|
8
8
|
MigrationToolsError,
|
|
9
9
|
} from './errors';
|
|
10
10
|
|
|
11
|
-
export
|
|
11
|
+
export interface RefEntry {
|
|
12
|
+
readonly hash: string;
|
|
13
|
+
readonly invariants: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type Refs = Readonly<Record<string, RefEntry>>;
|
|
12
17
|
|
|
13
18
|
const REF_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\/[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;
|
|
14
19
|
const REF_VALUE_PATTERN = /^sha256:(empty|[0-9a-f]{64})$/;
|
|
@@ -25,22 +30,40 @@ export function validateRefValue(value: string): boolean {
|
|
|
25
30
|
return REF_VALUE_PATTERN.test(value);
|
|
26
31
|
}
|
|
27
32
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
const RefEntrySchema = type({
|
|
34
|
+
hash: 'string',
|
|
35
|
+
invariants: 'string[]',
|
|
36
|
+
}).narrow((entry, ctx) => {
|
|
37
|
+
if (!validateRefValue(entry.hash))
|
|
38
|
+
return ctx.mustBe(`a valid contract hash (got "${entry.hash}")`);
|
|
34
39
|
return true;
|
|
35
40
|
});
|
|
36
41
|
|
|
37
|
-
|
|
42
|
+
function refFilePath(refsDir: string, name: string): string {
|
|
43
|
+
return join(refsDir, `${name}.json`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function refNameFromPath(refsDir: string, filePath: string): string {
|
|
47
|
+
const rel = relative(refsDir, filePath);
|
|
48
|
+
return rel.replace(/\.json$/, '');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function readRef(refsDir: string, name: string): Promise<RefEntry> {
|
|
52
|
+
if (!validateRefName(name)) {
|
|
53
|
+
throw errorInvalidRefName(name);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const filePath = refFilePath(refsDir, name);
|
|
38
57
|
let raw: string;
|
|
39
58
|
try {
|
|
40
|
-
raw = await readFile(
|
|
59
|
+
raw = await readFile(filePath, 'utf-8');
|
|
41
60
|
} catch (error) {
|
|
42
61
|
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
43
|
-
|
|
62
|
+
throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref "${name}"`, {
|
|
63
|
+
why: `No ref file found at "${filePath}".`,
|
|
64
|
+
fix: `Create the ref with: prisma-next migration ref set ${name} <hash>`,
|
|
65
|
+
details: { refName: name, filePath },
|
|
66
|
+
});
|
|
44
67
|
}
|
|
45
68
|
throw error;
|
|
46
69
|
}
|
|
@@ -49,54 +72,142 @@ export async function readRefs(refsPath: string): Promise<Refs> {
|
|
|
49
72
|
try {
|
|
50
73
|
parsed = JSON.parse(raw);
|
|
51
74
|
} catch {
|
|
52
|
-
throw
|
|
75
|
+
throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');
|
|
53
76
|
}
|
|
54
77
|
|
|
55
|
-
const result =
|
|
78
|
+
const result = RefEntrySchema(parsed);
|
|
56
79
|
if (result instanceof type.errors) {
|
|
57
|
-
throw
|
|
80
|
+
throw errorInvalidRefFile(filePath, result.summary);
|
|
58
81
|
}
|
|
59
82
|
|
|
60
83
|
return result;
|
|
61
84
|
}
|
|
62
85
|
|
|
63
|
-
export async function
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
86
|
+
export async function readRefs(refsDir: string): Promise<Refs> {
|
|
87
|
+
let entries: string[];
|
|
88
|
+
try {
|
|
89
|
+
entries = await readdir(refsDir, { recursive: true, encoding: 'utf-8' });
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const jsonFiles = entries.filter((entry) => entry.endsWith('.json'));
|
|
98
|
+
const refs: Record<string, RefEntry> = {};
|
|
99
|
+
|
|
100
|
+
for (const jsonFile of jsonFiles) {
|
|
101
|
+
const filePath = join(refsDir, jsonFile);
|
|
102
|
+
const name = refNameFromPath(refsDir, filePath);
|
|
103
|
+
|
|
104
|
+
let raw: string;
|
|
105
|
+
try {
|
|
106
|
+
raw = await readFile(filePath, 'utf-8');
|
|
107
|
+
} catch (error) {
|
|
108
|
+
// Tolerate the TOCTOU race between `readdir` and `readFile` (ENOENT) and
|
|
109
|
+
// benign EISDIR if a directory happens to end in `.json`. Anything else
|
|
110
|
+
// (EACCES, EIO, EMFILE, …) is a real failure and propagates so the CLI
|
|
111
|
+
// surfaces it rather than silently dropping the ref.
|
|
112
|
+
const code = error instanceof Error ? (error as { code?: string }).code : undefined;
|
|
113
|
+
if (code === 'ENOENT' || code === 'EISDIR') {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let parsed: unknown;
|
|
120
|
+
try {
|
|
121
|
+
parsed = JSON.parse(raw);
|
|
122
|
+
} catch {
|
|
123
|
+
throw errorInvalidRefFile(filePath, 'Failed to parse as JSON');
|
|
67
124
|
}
|
|
68
|
-
|
|
69
|
-
|
|
125
|
+
|
|
126
|
+
const result = RefEntrySchema(parsed);
|
|
127
|
+
if (result instanceof type.errors) {
|
|
128
|
+
throw errorInvalidRefFile(filePath, result.summary);
|
|
70
129
|
}
|
|
130
|
+
|
|
131
|
+
refs[name] = result;
|
|
71
132
|
}
|
|
72
133
|
|
|
73
|
-
|
|
134
|
+
return refs;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function writeRef(refsDir: string, name: string, entry: RefEntry): Promise<void> {
|
|
138
|
+
if (!validateRefName(name)) {
|
|
139
|
+
throw errorInvalidRefName(name);
|
|
140
|
+
}
|
|
141
|
+
if (!validateRefValue(entry.hash)) {
|
|
142
|
+
throw errorInvalidRefValue(entry.hash);
|
|
143
|
+
}
|
|
74
144
|
|
|
75
|
-
const
|
|
145
|
+
const filePath = refFilePath(refsDir, name);
|
|
146
|
+
const dir = dirname(filePath);
|
|
76
147
|
await mkdir(dir, { recursive: true });
|
|
77
148
|
|
|
78
|
-
const tmpPath = join(dir,
|
|
79
|
-
await writeFile(
|
|
80
|
-
|
|
149
|
+
const tmpPath = join(dir, `.${name.split('/').pop()}.json.${Date.now()}.tmp`);
|
|
150
|
+
await writeFile(
|
|
151
|
+
tmpPath,
|
|
152
|
+
`${JSON.stringify({ hash: entry.hash, invariants: [...entry.invariants] }, null, 2)}\n`,
|
|
153
|
+
);
|
|
154
|
+
await rename(tmpPath, filePath);
|
|
81
155
|
}
|
|
82
156
|
|
|
83
|
-
export function
|
|
157
|
+
export async function deleteRef(refsDir: string, name: string): Promise<void> {
|
|
84
158
|
if (!validateRefName(name)) {
|
|
85
159
|
throw errorInvalidRefName(name);
|
|
86
160
|
}
|
|
87
161
|
|
|
88
|
-
const
|
|
89
|
-
|
|
162
|
+
const filePath = refFilePath(refsDir, name);
|
|
163
|
+
try {
|
|
164
|
+
await unlink(filePath);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
167
|
+
throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref "${name}"`, {
|
|
168
|
+
why: `No ref file found at "${filePath}".`,
|
|
169
|
+
fix: 'Run `prisma-next migration ref list` to see available refs.',
|
|
170
|
+
details: { refName: name, filePath },
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Clean empty parent directories up to refsDir. Stop walking on the expected
|
|
177
|
+
// "directory has siblings" signal (ENOTEMPTY on Linux, EEXIST on some BSDs)
|
|
178
|
+
// and on ENOENT (concurrent removal). Anything else (EACCES, EIO, …) is a
|
|
179
|
+
// real failure and propagates.
|
|
180
|
+
let dir = dirname(filePath);
|
|
181
|
+
while (dir !== refsDir && dir.startsWith(refsDir)) {
|
|
182
|
+
try {
|
|
183
|
+
await rmdir(dir);
|
|
184
|
+
dir = dirname(dir);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
const code = error instanceof Error ? (error as { code?: string }).code : undefined;
|
|
187
|
+
if (code === 'ENOTEMPTY' || code === 'EEXIST' || code === 'ENOENT') {
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function resolveRef(refs: Refs, name: string): RefEntry {
|
|
196
|
+
if (!validateRefName(name)) {
|
|
197
|
+
throw errorInvalidRefName(name);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Object.hasOwn gate: plain-object `refs` would otherwise let
|
|
201
|
+
// `refs['constructor']` return Object.prototype.constructor and bypass the
|
|
202
|
+
// UNKNOWN_REF throw. validateRefName accepts `"constructor"` as a name shape.
|
|
203
|
+
if (!Object.hasOwn(refs, name)) {
|
|
90
204
|
throw new MigrationToolsError('MIGRATION.UNKNOWN_REF', `Unknown ref "${name}"`, {
|
|
91
|
-
why: `No ref named "${name}" exists
|
|
92
|
-
fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: set
|
|
205
|
+
why: `No ref named "${name}" exists.`,
|
|
206
|
+
fix: `Available refs: ${Object.keys(refs).join(', ') || '(none)'}. Create a ref with: prisma-next migration ref set ${name} <hash>`,
|
|
93
207
|
details: { refName: name, availableRefs: Object.keys(refs) },
|
|
94
208
|
});
|
|
95
209
|
}
|
|
96
210
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return hash;
|
|
211
|
+
// biome-ignore lint/style/noNonNullAssertion: Object.hasOwn gate above guarantees this is defined
|
|
212
|
+
return refs[name]!;
|
|
102
213
|
}
|