@prisma-next/cli 0.12.0-dev.52 → 0.12.0-dev.54
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/cli.mjs +7 -7
- package/dist/{client-BfNYTr8w.mjs → client-DIcitJdy.mjs} +95 -38
- package/dist/client-DIcitJdy.mjs.map +1 -0
- package/dist/commands/contract-infer.mjs +1 -1
- package/dist/commands/db-init.mjs +2 -2
- package/dist/commands/db-schema.mjs +1 -1
- package/dist/commands/db-sign.mjs +1 -1
- package/dist/commands/db-update.mjs +2 -2
- package/dist/commands/db-verify.mjs +1 -1
- package/dist/commands/migrate.d.mts +35 -1
- package/dist/commands/migrate.d.mts.map +1 -1
- package/dist/commands/migrate.mjs +287 -6
- package/dist/commands/migrate.mjs.map +1 -1
- package/dist/commands/migration-check.mjs +2 -2
- package/dist/commands/migration-graph.d.mts.map +1 -1
- package/dist/commands/migration-graph.mjs +4 -2
- package/dist/commands/migration-graph.mjs.map +1 -1
- package/dist/commands/migration-list.d.mts +1 -0
- package/dist/commands/migration-list.d.mts.map +1 -1
- package/dist/commands/migration-list.mjs +1 -1
- package/dist/commands/migration-log.mjs +1 -1
- package/dist/commands/migration-show.mjs +2 -2
- package/dist/commands/migration-status.d.mts.map +1 -1
- package/dist/commands/migration-status.mjs +2 -2
- package/dist/{contract-infer-sUml0McE.mjs → contract-infer-BAdhYGQH.mjs} +2 -2
- package/dist/{contract-infer-sUml0McE.mjs.map → contract-infer-BAdhYGQH.mjs.map} +1 -1
- package/dist/{db-verify-DSZcaGQs.mjs → db-verify-CiUCDXnv.mjs} +2 -2
- package/dist/{db-verify-DSZcaGQs.mjs.map → db-verify-CiUCDXnv.mjs.map} +1 -1
- package/dist/exports/control-api.mjs +1 -1
- package/dist/{inspect-live-schema-B44OravN.mjs → inspect-live-schema-DegaqKFT.mjs} +2 -2
- package/dist/{inspect-live-schema-B44OravN.mjs.map → inspect-live-schema-DegaqKFT.mjs.map} +1 -1
- package/dist/{migration-check-BxWlQBOs.mjs → migration-check-ldvGvsgM.mjs} +2 -3
- package/dist/{migration-check-BxWlQBOs.mjs.map → migration-check-ldvGvsgM.mjs.map} +1 -1
- package/dist/{migration-command-scaffold-BvVTTjzK.mjs → migration-command-scaffold-D6UeN71F.mjs} +2 -2
- package/dist/{migration-command-scaffold-BvVTTjzK.mjs.map → migration-command-scaffold-D6UeN71F.mjs.map} +1 -1
- package/dist/{migration-graph-space-render-CeNXh_Wy.mjs → migration-graph-space-render-B0HkTNj3.mjs} +488 -84
- package/dist/migration-graph-space-render-B0HkTNj3.mjs.map +1 -0
- package/dist/{migration-list-vJWFuXca.mjs → migration-list-mYmj2j33.mjs} +6 -4
- package/dist/migration-list-mYmj2j33.mjs.map +1 -0
- package/dist/{migration-log-DD01Jm_i.mjs → migration-log-Dzs18GU7.mjs} +3 -3
- package/dist/{migration-log-DD01Jm_i.mjs.map → migration-log-Dzs18GU7.mjs.map} +1 -1
- package/dist/{migration-path-target-UkxkgXnv.mjs → migration-path-target-DK-B7POa.mjs} +1 -1
- package/dist/{migration-path-target-UkxkgXnv.mjs.map → migration-path-target-DK-B7POa.mjs.map} +1 -1
- package/dist/{migration-status-jL5ajRrB.mjs → migration-status-BGKGc0iR.mjs} +7 -5
- package/dist/migration-status-BGKGc0iR.mjs.map +1 -0
- package/dist/{schemas-DJY2O09F.mjs → schemas-B4xeMrNt.mjs} +1 -1
- package/dist/{schemas-DJY2O09F.mjs.map → schemas-B4xeMrNt.mjs.map} +1 -1
- package/package.json +18 -18
- package/src/commands/migrate.ts +512 -2
- package/src/commands/migration-graph.ts +2 -0
- package/src/commands/migration-list.ts +3 -0
- package/src/commands/migration-status.ts +4 -0
- package/src/control-api/operations/migrate.ts +149 -56
- package/src/utils/formatters/migration-graph-layout.ts +187 -42
- package/src/utils/formatters/migration-graph-space-render.ts +11 -1
- package/src/utils/formatters/migration-graph-tree-render.ts +609 -59
- package/src/utils/formatters/migration-list-render.ts +12 -0
- package/src/utils/formatters/migration-list-styler.ts +5 -7
- package/dist/client-BfNYTr8w.mjs.map +0 -1
- package/dist/migration-graph-space-render-CeNXh_Wy.mjs.map +0 -1
- package/dist/migration-list-vJWFuXca.mjs.map +0 -1
- package/dist/migration-status-jL5ajRrB.mjs.map +0 -1
package/src/commands/migrate.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import type { Contract } from '@prisma-next/contract/types';
|
|
3
3
|
import { createControlStack } from '@prisma-next/framework-components/control';
|
|
4
|
+
import { type ContractSpaceMember, requireHeadRef } from '@prisma-next/migration-tools/aggregate';
|
|
5
|
+
import { EMPTY_CONTRACT_HASH } from '@prisma-next/migration-tools/constants';
|
|
4
6
|
import { errorUnknownInvariant, MigrationToolsError } from '@prisma-next/migration-tools/errors';
|
|
5
7
|
import { findLatestMigration, isGraphNode } from '@prisma-next/migration-tools/migration-graph';
|
|
6
8
|
import { parseContractRef } from '@prisma-next/migration-tools/ref-resolution';
|
|
7
|
-
import type { RefEntry } from '@prisma-next/migration-tools/refs';
|
|
9
|
+
import type { RefEntry, Refs } from '@prisma-next/migration-tools/refs';
|
|
10
|
+
import { readRefs } from '@prisma-next/migration-tools/refs';
|
|
8
11
|
import { ifDefined } from '@prisma-next/utils/defined';
|
|
9
12
|
import { notOk, ok, type Result } from '@prisma-next/utils/result';
|
|
10
13
|
import { Command } from 'commander';
|
|
11
14
|
import { loadConfig } from '../config-loader';
|
|
12
15
|
import { createControlClient } from '../control-api/client';
|
|
16
|
+
import { planMemberPath } from '../control-api/operations/migrate';
|
|
13
17
|
import type {
|
|
14
18
|
MigrateFailure,
|
|
15
19
|
MigratePathDecision,
|
|
@@ -29,6 +33,7 @@ import {
|
|
|
29
33
|
errorUnexpected,
|
|
30
34
|
mapMigrationToolsError,
|
|
31
35
|
mapRefResolutionError,
|
|
36
|
+
requireLiveDatabase,
|
|
32
37
|
} from '../utils/cli-errors';
|
|
33
38
|
import {
|
|
34
39
|
addGlobalOptions,
|
|
@@ -42,10 +47,21 @@ import {
|
|
|
42
47
|
} from '../utils/command-helpers';
|
|
43
48
|
import { mapContractAtError } from '../utils/contract-at-errors';
|
|
44
49
|
import {
|
|
50
|
+
buildReadAggregate,
|
|
45
51
|
loadContractSpaceAggregateForCli,
|
|
46
52
|
refuseContractSpaceIntegrity,
|
|
47
53
|
} from '../utils/contract-space-aggregate-loader';
|
|
48
54
|
import { toDeclaredExtensionsFromRaw } from '../utils/extension-pack-inputs';
|
|
55
|
+
import { buildMigrationGraphLayout } from '../utils/formatters/migration-graph-layout';
|
|
56
|
+
import { buildMigrationGraphRows } from '../utils/formatters/migration-graph-rows';
|
|
57
|
+
import { indentMigrationGraphTreeBlock } from '../utils/formatters/migration-graph-space-render';
|
|
58
|
+
import type { MigrationEdgeAnnotation } from '../utils/formatters/migration-graph-tree-render';
|
|
59
|
+
import {
|
|
60
|
+
computeMaxDirNameLengthForLayout,
|
|
61
|
+
computeMaxEdgeTreePrefixWidthForLayout,
|
|
62
|
+
formatOnPathMigrationRow,
|
|
63
|
+
renderMigrationGraphTree,
|
|
64
|
+
} from '../utils/formatters/migration-graph-tree-render';
|
|
49
65
|
import { formatMigrationApplyCommandOutput } from '../utils/formatters/migrations';
|
|
50
66
|
import { formatStyledHeader } from '../utils/formatters/styled';
|
|
51
67
|
import type { CommonCommandOptions } from '../utils/global-flags';
|
|
@@ -53,12 +69,51 @@ import { type GlobalFlags, parseGlobalFlagsOrExit } from '../utils/global-flags'
|
|
|
53
69
|
import { executeRefAdvancement, readContractIR } from '../utils/ref-advancement';
|
|
54
70
|
import { handleResult } from '../utils/result-handler';
|
|
55
71
|
import { createTerminalUI, type TerminalUI } from '../utils/terminal-ui';
|
|
72
|
+
import { listRefsByContractHash } from './migration-list';
|
|
56
73
|
|
|
57
74
|
interface MigrateCommandOptions extends CommonCommandOptions {
|
|
58
75
|
readonly db?: string;
|
|
59
76
|
readonly config?: string;
|
|
60
77
|
readonly to?: string;
|
|
61
78
|
readonly advanceRef?: string;
|
|
79
|
+
readonly show?: boolean;
|
|
80
|
+
readonly from?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* One migration that will run in a `migrate --show` preview, in execution order.
|
|
85
|
+
*/
|
|
86
|
+
export interface MigrateShowMigration {
|
|
87
|
+
readonly spaceId: string;
|
|
88
|
+
readonly dirName: string;
|
|
89
|
+
readonly migrationHash: string;
|
|
90
|
+
readonly from: string;
|
|
91
|
+
readonly to: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Result returned by `migrate --show`. Read-only; no writes performed. */
|
|
95
|
+
export interface MigrateShowResult {
|
|
96
|
+
readonly ok: true;
|
|
97
|
+
readonly migrations: readonly MigrateShowMigration[];
|
|
98
|
+
readonly summary: string;
|
|
99
|
+
/**
|
|
100
|
+
* Pre-rendered Tier-3 graph tree for human output. Off-path migrations render
|
|
101
|
+
* dim; on-path migrations render in ordinary colours. Only present in human
|
|
102
|
+
* (non-JSON) mode.
|
|
103
|
+
*/
|
|
104
|
+
readonly graphOutput?: string;
|
|
105
|
+
/**
|
|
106
|
+
* Name column width for the "Will run, in order:" list — globally aligned with
|
|
107
|
+
* every graph-tree section. Only present in human (non-JSON) mode.
|
|
108
|
+
*/
|
|
109
|
+
readonly runListDirNameWidth?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Left-pad offset (number of blank spaces) matching the graph's data-column
|
|
112
|
+
* offset (`globalMaxEdgeTreePrefixWidth`). Used to align list rows with graph
|
|
113
|
+
* rows so every `→` in the output (graph + list) lands at the same column.
|
|
114
|
+
* Only present in human (non-JSON) mode when multiple spaces are rendered.
|
|
115
|
+
*/
|
|
116
|
+
readonly runListLeftPad?: number;
|
|
62
117
|
}
|
|
63
118
|
|
|
64
119
|
export interface MigrateResult {
|
|
@@ -83,6 +138,435 @@ export interface MigrateResult {
|
|
|
83
138
|
readonly advancedRef?: { readonly name: string; readonly hash: string } | null;
|
|
84
139
|
}
|
|
85
140
|
|
|
141
|
+
/**
|
|
142
|
+
* Read-only preview of the migration path `migrate` will take.
|
|
143
|
+
*
|
|
144
|
+
* Computes the path through the SAME seam as `executeMigrate`:
|
|
145
|
+
* - `readAllMarkers()` for the from-state (when no `--from` is given), preserving
|
|
146
|
+
* the full marker including `invariants` (not just `storageHash`).
|
|
147
|
+
* - `planMemberPath()` (shared with `executeMigrate`) for per-member path selection,
|
|
148
|
+
* which feeds `graphWalkStrategy()` with the same target hash, target invariants,
|
|
149
|
+
* and current marker as the real apply path uses.
|
|
150
|
+
*
|
|
151
|
+
* Returns BEFORE any write boundary (`runMigration` / marker / DDL). No
|
|
152
|
+
* DB state is mutated.
|
|
153
|
+
*/
|
|
154
|
+
async function executeMigrateShowCommand(
|
|
155
|
+
options: MigrateCommandOptions,
|
|
156
|
+
flags: GlobalFlags,
|
|
157
|
+
ui: TerminalUI,
|
|
158
|
+
): Promise<Result<MigrateShowResult, CliStructuredErrorType>> {
|
|
159
|
+
const config = await loadConfig(options.config);
|
|
160
|
+
const { configPath, migrationsDir, migrationsRelative, refsDir } = resolveMigrationPaths(
|
|
161
|
+
options.config,
|
|
162
|
+
config,
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
166
|
+
const hasDriver = !!config.driver;
|
|
167
|
+
const hasExplicitFrom = options.from !== undefined;
|
|
168
|
+
|
|
169
|
+
// When --from is omitted we read the live DB marker (same as migrate's default).
|
|
170
|
+
// When --from is given, we're in offline hypothetical mode — no connection needed.
|
|
171
|
+
if (!hasExplicitFrom) {
|
|
172
|
+
const missingDb = requireLiveDatabase({
|
|
173
|
+
dbConnection,
|
|
174
|
+
hasDriver,
|
|
175
|
+
why: 'migrate --show needs a database connection to read the live marker (or pass --from <contract> for an offline preview)',
|
|
176
|
+
retryCommand: 'prisma-next migrate --show --from <contract>',
|
|
177
|
+
});
|
|
178
|
+
if (missingDb) {
|
|
179
|
+
return notOk(missingDb);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let allRefs: Refs = {};
|
|
184
|
+
try {
|
|
185
|
+
allRefs = await readRefs(refsDir);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
if (MigrationToolsError.is(error)) {
|
|
188
|
+
return notOk(mapMigrationToolsError(error));
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const loaded = await buildReadAggregate(config, { migrationsDir });
|
|
194
|
+
if (!loaded.ok) {
|
|
195
|
+
return notOk(loaded.failure);
|
|
196
|
+
}
|
|
197
|
+
const { aggregate, contractHash } = loaded.value;
|
|
198
|
+
const appGraph = aggregate.app.graph();
|
|
199
|
+
|
|
200
|
+
// Resolve the --to target (defaults to the on-disk contract, same as migrate).
|
|
201
|
+
// Also capture the ref's invariants so planMemberPath feeds graphWalkStrategy the
|
|
202
|
+
// same target invariants that real migrate would use (refInvariants ?? headRef.invariants).
|
|
203
|
+
let targetHash: string = contractHash;
|
|
204
|
+
let refInvariants: readonly string[] | undefined;
|
|
205
|
+
if (options.to) {
|
|
206
|
+
const toResult = parseContractRef(options.to, {
|
|
207
|
+
graph: appGraph,
|
|
208
|
+
refs: allRefs,
|
|
209
|
+
contractHash,
|
|
210
|
+
});
|
|
211
|
+
if (!toResult.ok) {
|
|
212
|
+
return notOk(mapRefResolutionError(toResult.failure));
|
|
213
|
+
}
|
|
214
|
+
if (toResult.value.provenance.kind === 'reserved-db') {
|
|
215
|
+
return notOk(
|
|
216
|
+
errorDatabaseConnectionRequired({
|
|
217
|
+
why: '@db is not valid as a --to target; it names the live database state, not a target contract.',
|
|
218
|
+
commandName: 'migrate --show',
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
targetHash = toResult.value.hash;
|
|
223
|
+
if (toResult.value.provenance.kind === 'ref') {
|
|
224
|
+
const refEntry = allRefs[toResult.value.provenance.refName];
|
|
225
|
+
if (refEntry) refInvariants = refEntry.invariants;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!flags.json && !flags.quiet) {
|
|
230
|
+
const details: Array<{ label: string; value: string }> = [
|
|
231
|
+
{ label: 'config', value: configPath },
|
|
232
|
+
{ label: 'migrations', value: migrationsRelative },
|
|
233
|
+
];
|
|
234
|
+
if (dbConnection && !hasExplicitFrom) {
|
|
235
|
+
details.push({ label: 'database', value: maskConnectionUrl(String(dbConnection)) });
|
|
236
|
+
}
|
|
237
|
+
if (options.from) {
|
|
238
|
+
details.push({ label: 'from', value: options.from });
|
|
239
|
+
}
|
|
240
|
+
if (options.to) {
|
|
241
|
+
details.push({ label: 'to', value: options.to });
|
|
242
|
+
}
|
|
243
|
+
const header = formatStyledHeader({
|
|
244
|
+
command: 'migrate --show',
|
|
245
|
+
description: 'Preview the migration path migrate will take (read-only)',
|
|
246
|
+
details,
|
|
247
|
+
flags,
|
|
248
|
+
});
|
|
249
|
+
ui.stderr(header);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Resolve the from-state.
|
|
253
|
+
// - Explicit --from: parse it offline (no connection).
|
|
254
|
+
// - Omitted: read the live DB marker via readAllMarkers() — the same source migrate uses.
|
|
255
|
+
//
|
|
256
|
+
// Full marker records (storageHash + invariants) are preserved so planMemberPath
|
|
257
|
+
// can feed graphWalkStrategy the complete currentMarker — exactly as executeMigrate
|
|
258
|
+
// does via familyInstance.readAllMarkers(). A stripped { storageHash, invariants: [] }
|
|
259
|
+
// marker would produce a different `required` set and a different (incorrect) path.
|
|
260
|
+
type LiveMarker = { readonly storageHash: string; readonly invariants: readonly string[] };
|
|
261
|
+
const markerBySpace = new Map<string, LiveMarker | null>();
|
|
262
|
+
const allMembers: ReadonlyArray<ContractSpaceMember> = [aggregate.app, ...aggregate.extensions];
|
|
263
|
+
|
|
264
|
+
if (hasExplicitFrom) {
|
|
265
|
+
// @db with explicit --from requires a connection
|
|
266
|
+
if (options.from === '@db') {
|
|
267
|
+
const missingDb = requireLiveDatabase({
|
|
268
|
+
dbConnection,
|
|
269
|
+
hasDriver,
|
|
270
|
+
why: '@db resolves to the live database marker and requires a --db connection',
|
|
271
|
+
retryCommand: 'prisma-next migrate --show --from @db --db $DATABASE_URL',
|
|
272
|
+
});
|
|
273
|
+
if (missingDb) {
|
|
274
|
+
return notOk(missingDb);
|
|
275
|
+
}
|
|
276
|
+
// Fall through to the connection path below
|
|
277
|
+
} else {
|
|
278
|
+
const fromResult = parseContractRef(options.from, {
|
|
279
|
+
graph: appGraph,
|
|
280
|
+
refs: allRefs,
|
|
281
|
+
contractHash,
|
|
282
|
+
});
|
|
283
|
+
if (!fromResult.ok) {
|
|
284
|
+
return notOk(mapRefResolutionError(fromResult.failure));
|
|
285
|
+
}
|
|
286
|
+
if (fromResult.value.provenance.kind === 'reserved-db') {
|
|
287
|
+
// Unreachable given the @db branch above, but guard for safety
|
|
288
|
+
const missingDb = requireLiveDatabase({
|
|
289
|
+
dbConnection,
|
|
290
|
+
hasDriver,
|
|
291
|
+
why: '@db resolves to the live database marker and requires a --db connection',
|
|
292
|
+
});
|
|
293
|
+
if (missingDb) {
|
|
294
|
+
return notOk(missingDb);
|
|
295
|
+
}
|
|
296
|
+
} else {
|
|
297
|
+
// Offline hypothetical: the --from ref only carries a hash (no live invariants).
|
|
298
|
+
// Apply the from-hash marker to the APP space only. Extension spaces are left
|
|
299
|
+
// absent from markerBySpace (treated as null / greenfield by planMemberPath),
|
|
300
|
+
// so they plan from their own marker → own head — exactly as executeMigrate does.
|
|
301
|
+
const fromHash = fromResult.value.hash;
|
|
302
|
+
const offlineMarker: LiveMarker | null =
|
|
303
|
+
fromHash === EMPTY_CONTRACT_HASH ? null : { storageHash: fromHash, invariants: [] };
|
|
304
|
+
markerBySpace.set(aggregate.app.spaceId, offlineMarker);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// If we need the live DB marker (no --from, or --from @db), connect and read.
|
|
310
|
+
const needsLiveMarker = !hasExplicitFrom || options.from === '@db';
|
|
311
|
+
if (needsLiveMarker) {
|
|
312
|
+
if (!dbConnection || !hasDriver) {
|
|
313
|
+
return notOk(
|
|
314
|
+
errorDatabaseConnectionRequired({
|
|
315
|
+
why: 'A database connection is required to read the live marker for migrate --show',
|
|
316
|
+
commandName: 'migrate --show',
|
|
317
|
+
}),
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
const client = createControlClient({
|
|
321
|
+
family: config.family,
|
|
322
|
+
target: config.target,
|
|
323
|
+
adapter: config.adapter,
|
|
324
|
+
driver: config.driver!,
|
|
325
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
326
|
+
});
|
|
327
|
+
try {
|
|
328
|
+
await client.connect(dbConnection);
|
|
329
|
+
const allMarkers = await client.readAllMarkers();
|
|
330
|
+
// Store the full marker record (storageHash + invariants) per space.
|
|
331
|
+
// This is the same data executeMigrate uses via familyInstance.readAllMarkers().
|
|
332
|
+
for (const member of allMembers) {
|
|
333
|
+
const marker = allMarkers.get(member.spaceId);
|
|
334
|
+
markerBySpace.set(member.spaceId, marker ?? null);
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (CliStructuredError.is(error)) {
|
|
338
|
+
return notOk(error);
|
|
339
|
+
}
|
|
340
|
+
if (MigrationToolsError.is(error)) {
|
|
341
|
+
return notOk(mapMigrationToolsError(error));
|
|
342
|
+
}
|
|
343
|
+
return notOk(
|
|
344
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
345
|
+
why: `Failed to read live DB marker: ${error instanceof Error ? error.message : String(error)}`,
|
|
346
|
+
}),
|
|
347
|
+
);
|
|
348
|
+
} finally {
|
|
349
|
+
await client.close();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Walk the path via planMemberPath — the same helper executeMigrate uses.
|
|
354
|
+
// planMemberPath feeds graphWalkStrategy identical inputs (targetHash, targetInvariants,
|
|
355
|
+
// currentMarker with full invariants), so the preview path is always the path migrate runs.
|
|
356
|
+
//
|
|
357
|
+
// Canonical schedule order: extensions alphabetically first, then app — mirroring the
|
|
358
|
+
// runner's `applyOrder` in operations/migrate.ts so the "Will run, in order:" list
|
|
359
|
+
// reflects the actual execution sequence (extensions install first, app last).
|
|
360
|
+
const canonicalOrderMembers: ReadonlyArray<ContractSpaceMember> = [
|
|
361
|
+
...aggregate.extensions,
|
|
362
|
+
aggregate.app,
|
|
363
|
+
];
|
|
364
|
+
const orderedMigrations: MigrateShowMigration[] = [];
|
|
365
|
+
for (const member of canonicalOrderMembers) {
|
|
366
|
+
const isAppMember = member.spaceId === aggregate.app.spaceId;
|
|
367
|
+
const headRef = requireHeadRef(member);
|
|
368
|
+
const memberTargetHash = isAppMember ? targetHash : headRef.hash;
|
|
369
|
+
const memberRefInvariants = isAppMember ? refInvariants : undefined;
|
|
370
|
+
const liveMarker = markerBySpace.get(member.spaceId) ?? null;
|
|
371
|
+
|
|
372
|
+
const outcome = planMemberPath({
|
|
373
|
+
member,
|
|
374
|
+
aggregate,
|
|
375
|
+
targetHash: memberTargetHash,
|
|
376
|
+
refInvariants: memberRefInvariants,
|
|
377
|
+
liveMarker,
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
if (outcome.kind === 'at-head') {
|
|
381
|
+
// Empty-graph member already at target — nothing to run for this space.
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (outcome.kind === 'never-planned') {
|
|
385
|
+
return notOk(
|
|
386
|
+
errorPathUnreachable({
|
|
387
|
+
code: 'MIGRATION_PATH_NOT_FOUND',
|
|
388
|
+
summary: `No on-disk migrations for contract space "${outcome.spaceId}"`,
|
|
389
|
+
why: `migrate is replay-only: space "${outcome.spaceId}" has no on-disk migrations but its head ref targets "${outcome.targetHash}".`,
|
|
390
|
+
meta: { spaceId: outcome.spaceId, target: outcome.targetHash, kind: 'neverPlanned' },
|
|
391
|
+
}),
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
if (outcome.kind === 'unreachable') {
|
|
395
|
+
const fromHash = outcome.liveMarker?.storageHash ?? EMPTY_CONTRACT_HASH;
|
|
396
|
+
return notOk(
|
|
397
|
+
errorPathUnreachable({
|
|
398
|
+
code: 'MIGRATION_PATH_NOT_FOUND',
|
|
399
|
+
summary: `No migration path from ${fromHash.slice(0, 14)} to ${outcome.targetHash.slice(0, 14)} in space "${outcome.spaceId}".`,
|
|
400
|
+
why: `The migration graph has no path from the from-state to the target in space "${outcome.spaceId}".`,
|
|
401
|
+
meta: { spaceId: outcome.spaceId, from: fromHash, to: outcome.targetHash },
|
|
402
|
+
}),
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
if (outcome.kind === 'unsatisfiable') {
|
|
406
|
+
return notOk(
|
|
407
|
+
errorRuntime(`Missing required invariants for space "${outcome.spaceId}"`, {
|
|
408
|
+
why: `The path requires invariants not available on disk: ${outcome.missing.join(', ')}`,
|
|
409
|
+
}),
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
for (const edge of outcome.plan.migrationEdges) {
|
|
414
|
+
orderedMigrations.push({
|
|
415
|
+
spaceId: member.spaceId,
|
|
416
|
+
dirName: edge.dirName,
|
|
417
|
+
migrationHash: edge.migrationHash,
|
|
418
|
+
from: edge.from,
|
|
419
|
+
to: edge.to,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const count = orderedMigrations.length;
|
|
425
|
+
const summary =
|
|
426
|
+
count === 0
|
|
427
|
+
? 'Already up to date — nothing to run'
|
|
428
|
+
: `${count} migration${count === 1 ? '' : 's'} will run`;
|
|
429
|
+
|
|
430
|
+
// Build the Tier-3 graph visualization (human mode only; skipped for --json).
|
|
431
|
+
// Reuses the existing annotation hook — no parallel renderer.
|
|
432
|
+
let graphOutput: string | undefined;
|
|
433
|
+
let runListDirNameWidth: number | undefined;
|
|
434
|
+
let runListLeftPad: number | undefined;
|
|
435
|
+
if (!flags.json) {
|
|
436
|
+
const onPathHashes = new Set(orderedMigrations.map((m) => m.migrationHash));
|
|
437
|
+
const colorize = flags.color !== false;
|
|
438
|
+
|
|
439
|
+
// Build layouts for all spaces first so we can compute global column widths
|
|
440
|
+
// before rendering. This ensures the name column, hash column, and ops column
|
|
441
|
+
// start at the same horizontal offset across every space section AND the
|
|
442
|
+
// "Will run, in order:" list below.
|
|
443
|
+
const memberLayouts = allMembers.map((member) => {
|
|
444
|
+
const isApp = member.spaceId === aggregate.app.spaceId;
|
|
445
|
+
const memberGraph = member.graph();
|
|
446
|
+
const rowModel = buildMigrationGraphRows(memberGraph, isApp ? { contractHash } : {});
|
|
447
|
+
const layout = buildMigrationGraphLayout(rowModel);
|
|
448
|
+
return { member, isApp, memberGraph, layout };
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
// Global max across all space layouts.
|
|
452
|
+
const globalMaxEdgeTreePrefixWidth =
|
|
453
|
+
memberLayouts.length > 1
|
|
454
|
+
? Math.max(
|
|
455
|
+
...memberLayouts.map(({ layout }) => computeMaxEdgeTreePrefixWidthForLayout(layout)),
|
|
456
|
+
)
|
|
457
|
+
: undefined;
|
|
458
|
+
const globalMaxDirNameWidthFromLayouts =
|
|
459
|
+
memberLayouts.length > 1
|
|
460
|
+
? Math.max(...memberLayouts.map(({ layout }) => computeMaxDirNameLengthForLayout(layout)))
|
|
461
|
+
: undefined;
|
|
462
|
+
// The run-list name column width must be at least as wide as the global tree dirName
|
|
463
|
+
// width so that tree sections and the list align at the hash column.
|
|
464
|
+
const runListMaxFromMigrations =
|
|
465
|
+
orderedMigrations.length > 0
|
|
466
|
+
? Math.max(...orderedMigrations.map((m) => m.dirName.length))
|
|
467
|
+
: 0;
|
|
468
|
+
const globalMaxDirNameWidth =
|
|
469
|
+
globalMaxDirNameWidthFromLayouts !== undefined
|
|
470
|
+
? Math.max(globalMaxDirNameWidthFromLayouts, runListMaxFromMigrations)
|
|
471
|
+
: undefined;
|
|
472
|
+
runListDirNameWidth = globalMaxDirNameWidth ?? runListMaxFromMigrations;
|
|
473
|
+
runListLeftPad = globalMaxEdgeTreePrefixWidth;
|
|
474
|
+
|
|
475
|
+
// Render each space section with globally computed widths.
|
|
476
|
+
const showSpaceHeadings = allMembers.length > 1;
|
|
477
|
+
const sections: string[] = [];
|
|
478
|
+
for (const { member, isApp, memberGraph, layout } of memberLayouts) {
|
|
479
|
+
const edgeAnnotations = new Map<string, MigrationEdgeAnnotation>();
|
|
480
|
+
for (const edge of memberGraph.migrationByHash.values()) {
|
|
481
|
+
edgeAnnotations.set(edge.migrationHash, {
|
|
482
|
+
pathHighlight: onPathHashes.has(edge.migrationHash) ? 'on-path' : 'off-path',
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
const liveMarker = markerBySpace.get(member.spaceId) ?? null;
|
|
486
|
+
const liveMarkerHash = liveMarker?.storageHash ?? EMPTY_CONTRACT_HASH;
|
|
487
|
+
const tree = renderMigrationGraphTree(layout, {
|
|
488
|
+
contractHash,
|
|
489
|
+
isAppSpace: isApp,
|
|
490
|
+
...(needsLiveMarker ? { dbHash: liveMarkerHash } : {}),
|
|
491
|
+
refsByHash: listRefsByContractHash(member),
|
|
492
|
+
edgeAnnotationsByHash: edgeAnnotations,
|
|
493
|
+
colorize,
|
|
494
|
+
...(globalMaxEdgeTreePrefixWidth !== undefined ? { globalMaxEdgeTreePrefixWidth } : {}),
|
|
495
|
+
...(globalMaxDirNameWidth !== undefined ? { globalMaxDirNameWidth } : {}),
|
|
496
|
+
});
|
|
497
|
+
if (tree.length === 0) continue;
|
|
498
|
+
if (showSpaceHeadings) {
|
|
499
|
+
sections.push(`${member.spaceId}:\n${indentMigrationGraphTreeBlock(tree, ' ')}`);
|
|
500
|
+
} else {
|
|
501
|
+
sections.push(tree);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
graphOutput = sections.join('\n\n');
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return ok({
|
|
508
|
+
ok: true,
|
|
509
|
+
migrations: orderedMigrations,
|
|
510
|
+
summary,
|
|
511
|
+
...(graphOutput !== undefined ? { graphOutput } : {}),
|
|
512
|
+
...(runListDirNameWidth !== undefined ? { runListDirNameWidth } : {}),
|
|
513
|
+
...(runListLeftPad !== undefined ? { runListLeftPad } : {}),
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function formatMigrateShowOutput(result: MigrateShowResult, flags: GlobalFlags): string {
|
|
518
|
+
if (flags.quiet) return '';
|
|
519
|
+
const colorize = flags.color !== false;
|
|
520
|
+
const lines: string[] = [];
|
|
521
|
+
// Graph tree first (shows the full topology with on-path highlighted).
|
|
522
|
+
if (result.graphOutput !== undefined && result.graphOutput.length > 0) {
|
|
523
|
+
lines.push(result.graphOutput);
|
|
524
|
+
lines.push('');
|
|
525
|
+
}
|
|
526
|
+
const n = result.migrations.length;
|
|
527
|
+
if (n > 0) {
|
|
528
|
+
// Consolidated header: one line replaces the old separate summary + blank +
|
|
529
|
+
// "Will run, in order:" header.
|
|
530
|
+
lines.push(`The following ${n} migration${n === 1 ? '' : 's'} will run:`);
|
|
531
|
+
// Ordered list rendered through the SAME on-path row renderer as the tree.
|
|
532
|
+
// `formatOnPathMigrationRow` uses PATH_HIGHLIGHT_STYLES.onPath so the list and
|
|
533
|
+
// graph-tree rows are styled identically — changing the on-path colour in future
|
|
534
|
+
// is a one-line edit in PATH_HIGHLIGHT_STYLES.
|
|
535
|
+
//
|
|
536
|
+
// Alignment anchor: the `→` arrow (source-hash onward) must land at the SAME
|
|
537
|
+
// absolute column as in graph edge rows, across every graph section and this list.
|
|
538
|
+
//
|
|
539
|
+
// Multi-space output layout (space headings + 2-space indented tree sections):
|
|
540
|
+
// Graph edge row: [2 heading][G gutter][D dirName][7 source] [→] [dest]
|
|
541
|
+
// List row: [2 spaces][L dirName][ ][7 source] [→] [dest]
|
|
542
|
+
// Alignment: 2 + G + D + 9 = 2 + L + 2 + 9 => L = G + D - 2
|
|
543
|
+
//
|
|
544
|
+
// Single-space output layout (flat tree, no heading indent):
|
|
545
|
+
// Graph edge row: [G gutter][D dirName][7 source] [→] [dest]
|
|
546
|
+
// List row: [2 spaces][L dirName][ ][7 source] [→] [dest]
|
|
547
|
+
// Alignment: G + D + 9 = 2 + L + 2 + 9 => L = G + D - 4
|
|
548
|
+
//
|
|
549
|
+
// D (edgeDirNameWidth) = max(rawDirNameWidth + LABEL_GAP, MIN_HASH_DATA_COLUMN - G)
|
|
550
|
+
// where LABEL_GAP = 2 and MIN_HASH_DATA_COLUMN = 25 (same constants as the renderer).
|
|
551
|
+
//
|
|
552
|
+
// runListLeftPad is set only for multi-space; undefined means single-space.
|
|
553
|
+
const isMultiSpace = result.runListLeftPad !== undefined;
|
|
554
|
+
const gutter = result.runListLeftPad ?? 0;
|
|
555
|
+
const rawDirNameWidth =
|
|
556
|
+
result.runListDirNameWidth ?? Math.max(...result.migrations.map((m) => m.dirName.length));
|
|
557
|
+
const edgeDirNameWidth = Math.max(rawDirNameWidth + 2, 25 - gutter);
|
|
558
|
+
const listDirNameWidth = gutter + edgeDirNameWidth - (isMultiSpace ? 2 : 4);
|
|
559
|
+
for (const m of result.migrations) {
|
|
560
|
+
lines.push(
|
|
561
|
+
` ${formatOnPathMigrationRow(m.dirName, m.from, m.to, listDirNameWidth, colorize, 'unicode')}`,
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
lines.push(result.summary);
|
|
566
|
+
}
|
|
567
|
+
return lines.join('\n');
|
|
568
|
+
}
|
|
569
|
+
|
|
86
570
|
function mapApplyFailure(failure: MigrateFailure): CliStructuredErrorType {
|
|
87
571
|
if (failure.code === 'MIGRATION_PATH_NOT_FOUND') {
|
|
88
572
|
return errorPathUnreachable(failure);
|
|
@@ -384,12 +868,15 @@ export function createMigrateCommand(): Command {
|
|
|
384
868
|
'Walks every contract space (app + extensions) and applies pending\n' +
|
|
385
869
|
'on-disk migrations in canonical order (extensions alphabetically,\n' +
|
|
386
870
|
'then app). Graph-walks the on-disk migration graph for every space.\n' +
|
|
387
|
-
'Use --to to target a specific contract (hash, ref name, migration dir)
|
|
871
|
+
'Use --to to target a specific contract (hash, ref name, migration dir).\n' +
|
|
872
|
+
'Use --show for a read-only preview of the path that would run.',
|
|
388
873
|
);
|
|
389
874
|
setCommandExamples(command, [
|
|
390
875
|
'prisma-next migrate --db $DATABASE_URL',
|
|
391
876
|
'prisma-next migrate --to production --db $DATABASE_URL',
|
|
392
877
|
'prisma-next migrate --to sha256:abc123 --db $DATABASE_URL',
|
|
878
|
+
'prisma-next migrate --show --db $DATABASE_URL',
|
|
879
|
+
'prisma-next migrate --show --from @contract --to production',
|
|
393
880
|
]);
|
|
394
881
|
addGlobalOptions(command)
|
|
395
882
|
.option('--db <url>', 'Database connection string')
|
|
@@ -399,12 +886,35 @@ export function createMigrateCommand(): Command {
|
|
|
399
886
|
'Target contract reference (hash, prefix, ref name, migration dir name, <dir>^, or ./path)',
|
|
400
887
|
)
|
|
401
888
|
.option('--advance-ref <name>', 'Advance the named ref to the post-apply marker after success')
|
|
889
|
+
.option('--show', 'Preview the migration path without applying (read-only)')
|
|
890
|
+
.option(
|
|
891
|
+
'--from <contract>',
|
|
892
|
+
'From-state for --show preview (@contract, @db, hash, ref name, or migration dir)',
|
|
893
|
+
)
|
|
402
894
|
.action(async (options: MigrateCommandOptions) => {
|
|
403
895
|
const flags = parseGlobalFlagsOrExit(options);
|
|
404
896
|
const startTime = Date.now();
|
|
405
897
|
|
|
406
898
|
const ui = createTerminalUI(flags);
|
|
407
899
|
|
|
900
|
+
if (options.show) {
|
|
901
|
+
// Read-only path: compute the migration plan and print the ordered list.
|
|
902
|
+
// NEVER reaches runMigration() or any write boundary.
|
|
903
|
+
const result = await executeMigrateShowCommand(options, flags, ui);
|
|
904
|
+
|
|
905
|
+
const exitCode = handleResult(result, flags, ui, (showResult) => {
|
|
906
|
+
if (flags.json) {
|
|
907
|
+
ui.output(JSON.stringify(showResult, null, 2));
|
|
908
|
+
} else {
|
|
909
|
+
// Print directly to stdout — not via ui.log() which injects Clack's │ gutter.
|
|
910
|
+
ui.output(formatMigrateShowOutput(showResult, flags));
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
process.exit(exitCode);
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
|
|
408
918
|
const result = await executeMigrateCommand(options, flags, ui, startTime);
|
|
409
919
|
|
|
410
920
|
const exitCode = handleResult(result, flags, ui, (migrateResult) => {
|
|
@@ -159,6 +159,7 @@ export async function executeMigrationGraphCommand(
|
|
|
159
159
|
continue;
|
|
160
160
|
}
|
|
161
161
|
const graph = member.graph();
|
|
162
|
+
const isAppSpace = spaceEntry.space === aggregate.app.spaceId;
|
|
162
163
|
const refsByHash = listRefsByContractHash(member);
|
|
163
164
|
const tree =
|
|
164
165
|
spaceEntry.migrations.length === 0
|
|
@@ -169,6 +170,7 @@ export async function executeMigrationGraphCommand(
|
|
|
169
170
|
liveContractHash,
|
|
170
171
|
glyphMode,
|
|
171
172
|
colorize,
|
|
173
|
+
isAppSpace,
|
|
172
174
|
refsByHash,
|
|
173
175
|
...(globalMaxEdgeTreePrefixWidth !== undefined ? { globalMaxEdgeTreePrefixWidth } : {}),
|
|
174
176
|
...(globalMaxDirNameWidth !== undefined ? { globalMaxDirNameWidth } : {}),
|
|
@@ -145,6 +145,7 @@ export interface MigrationListHumanRenderOptions {
|
|
|
145
145
|
readonly useColor: boolean;
|
|
146
146
|
readonly liveContractHash: string;
|
|
147
147
|
readonly graphForSpace: (spaceId: string) => MigrationGraph | undefined;
|
|
148
|
+
readonly appSpaceId?: string;
|
|
148
149
|
}
|
|
149
150
|
|
|
150
151
|
export function renderMigrationListHumanOutput(
|
|
@@ -156,6 +157,7 @@ export function renderMigrationListHumanOutput(
|
|
|
156
157
|
colorize: options.useColor,
|
|
157
158
|
liveContractHash: options.liveContractHash,
|
|
158
159
|
graphForSpace: options.graphForSpace,
|
|
160
|
+
...(options.appSpaceId !== undefined ? { appSpaceId: options.appSpaceId } : {}),
|
|
159
161
|
});
|
|
160
162
|
}
|
|
161
163
|
|
|
@@ -321,6 +323,7 @@ export function createMigrationListCommand(): Command {
|
|
|
321
323
|
useColor: ui.useColor,
|
|
322
324
|
liveContractHash,
|
|
323
325
|
graphForSpace: (spaceId) => aggregate.space(spaceId)?.graph(),
|
|
326
|
+
appSpaceId: aggregate.app.spaceId,
|
|
324
327
|
}),
|
|
325
328
|
);
|
|
326
329
|
}
|
|
@@ -136,6 +136,7 @@ function renderSpaceTree(args: {
|
|
|
136
136
|
readonly statusOverlay: ReadonlyMap<string, MigrationEdgeAnnotation>;
|
|
137
137
|
readonly colorize: boolean;
|
|
138
138
|
readonly glyphMode: 'unicode' | 'ascii';
|
|
139
|
+
readonly isAppSpace: boolean;
|
|
139
140
|
readonly globalMaxEdgeTreePrefixWidth?: number;
|
|
140
141
|
readonly globalMaxDirNameWidth?: number;
|
|
141
142
|
}): string {
|
|
@@ -151,6 +152,7 @@ function renderSpaceTree(args: {
|
|
|
151
152
|
statusOverlayByHash: args.statusOverlay,
|
|
152
153
|
colorize: args.colorize,
|
|
153
154
|
glyphMode: args.glyphMode,
|
|
155
|
+
isAppSpace: args.isAppSpace,
|
|
154
156
|
...(args.showDbMarker && args.markerHash !== undefined ? { dbHash: args.markerHash } : {}),
|
|
155
157
|
...(args.globalMaxEdgeTreePrefixWidth !== undefined
|
|
156
158
|
? { globalMaxEdgeTreePrefixWidth: args.globalMaxEdgeTreePrefixWidth }
|
|
@@ -541,6 +543,7 @@ export async function executeMigrationStatusCommand(
|
|
|
541
543
|
appliedMigrationHashes: appliedHashes,
|
|
542
544
|
showAppliedOverlay,
|
|
543
545
|
});
|
|
546
|
+
const isAppSpace = spaceEntry.space === aggregate.app.spaceId;
|
|
544
547
|
const tree = renderSpaceTree({
|
|
545
548
|
member,
|
|
546
549
|
liveContractHash: contractHash,
|
|
@@ -550,6 +553,7 @@ export async function executeMigrationStatusCommand(
|
|
|
550
553
|
statusOverlay: annotations,
|
|
551
554
|
colorize,
|
|
552
555
|
glyphMode,
|
|
556
|
+
isAppSpace,
|
|
553
557
|
...(globalMaxEdgeTreePrefixWidth !== undefined ? { globalMaxEdgeTreePrefixWidth } : {}),
|
|
554
558
|
...(globalMaxDirNameWidth !== undefined ? { globalMaxDirNameWidth } : {}),
|
|
555
559
|
});
|