@prisma-next/cli 0.3.0-dev.6 → 0.3.0-dev.64
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/LICENSE +201 -0
- package/README.md +314 -80
- package/dist/cli-errors-JlPTsazx.mjs +3 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.js +1 -2376
- package/dist/cli.mjs +203 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/client-PimzSD1f.mjs +981 -0
- package/dist/client-PimzSD1f.mjs.map +1 -0
- package/dist/commands/contract-emit.d.mts +7 -0
- package/dist/commands/contract-emit.d.mts.map +1 -0
- package/dist/commands/contract-emit.mjs +151 -0
- package/dist/commands/contract-emit.mjs.map +1 -0
- package/dist/commands/db-init.d.mts +7 -0
- package/dist/commands/db-init.d.mts.map +1 -0
- package/dist/commands/db-init.mjs +134 -0
- package/dist/commands/db-init.mjs.map +1 -0
- package/dist/commands/db-introspect.d.mts +7 -0
- package/dist/commands/db-introspect.d.mts.map +1 -0
- package/dist/commands/db-introspect.mjs +118 -0
- package/dist/commands/db-introspect.mjs.map +1 -0
- package/dist/commands/db-schema-verify.d.mts +7 -0
- package/dist/commands/db-schema-verify.d.mts.map +1 -0
- package/dist/commands/db-schema-verify.mjs +120 -0
- package/dist/commands/db-schema-verify.mjs.map +1 -0
- package/dist/commands/db-sign.d.mts +7 -0
- package/dist/commands/db-sign.d.mts.map +1 -0
- package/dist/commands/db-sign.mjs +142 -0
- package/dist/commands/db-sign.mjs.map +1 -0
- package/dist/commands/db-update.d.mts +7 -0
- package/dist/commands/db-update.d.mts.map +1 -0
- package/dist/commands/db-update.mjs +123 -0
- package/dist/commands/db-update.mjs.map +1 -0
- package/dist/commands/db-verify.d.mts +7 -0
- package/dist/commands/db-verify.d.mts.map +1 -0
- package/dist/commands/db-verify.mjs +133 -0
- package/dist/commands/db-verify.mjs.map +1 -0
- package/dist/commands/migration-apply.d.mts +23 -0
- package/dist/commands/migration-apply.d.mts.map +1 -0
- package/dist/commands/migration-apply.mjs +250 -0
- package/dist/commands/migration-apply.mjs.map +1 -0
- package/dist/commands/migration-plan.d.mts +25 -0
- package/dist/commands/migration-plan.d.mts.map +1 -0
- package/dist/commands/migration-plan.mjs +266 -0
- package/dist/commands/migration-plan.mjs.map +1 -0
- package/dist/commands/migration-show.d.mts +28 -0
- package/dist/commands/migration-show.d.mts.map +1 -0
- package/dist/commands/migration-show.mjs +138 -0
- package/dist/commands/migration-show.mjs.map +1 -0
- package/dist/commands/migration-status.d.mts +35 -0
- package/dist/commands/migration-status.d.mts.map +1 -0
- package/dist/commands/migration-status.mjs +260 -0
- package/dist/commands/migration-status.mjs.map +1 -0
- package/dist/commands/migration-verify.d.mts +16 -0
- package/dist/commands/migration-verify.d.mts.map +1 -0
- package/dist/commands/migration-verify.mjs +86 -0
- package/dist/commands/migration-verify.mjs.map +1 -0
- package/dist/config-loader-PPf4CtDj.mjs +43 -0
- package/dist/config-loader-PPf4CtDj.mjs.map +1 -0
- package/dist/{config-loader.d.ts → config-loader.d.mts} +8 -3
- package/dist/config-loader.d.mts.map +1 -0
- package/dist/config-loader.mjs +3 -0
- package/dist/exports/config-types.d.mts +2 -0
- package/dist/exports/config-types.mjs +3 -0
- package/dist/exports/control-api.d.mts +621 -0
- package/dist/exports/control-api.d.mts.map +1 -0
- package/dist/exports/control-api.mjs +98 -0
- package/dist/exports/control-api.mjs.map +1 -0
- package/dist/{load-ts-contract.d.ts → exports/index.d.mts} +10 -5
- package/dist/exports/index.d.mts.map +1 -0
- package/dist/exports/index.mjs +135 -0
- package/dist/exports/index.mjs.map +1 -0
- package/dist/extract-sql-ddl-BmlKvk4o.mjs +26 -0
- package/dist/extract-sql-ddl-BmlKvk4o.mjs.map +1 -0
- package/dist/framework-components-CjV_jD8f.mjs +59 -0
- package/dist/framework-components-CjV_jD8f.mjs.map +1 -0
- package/dist/migration-command-scaffold-DfY_F3ev.mjs +97 -0
- package/dist/migration-command-scaffold-DfY_F3ev.mjs.map +1 -0
- package/dist/progress-adapter-DENrzF6I.mjs +49 -0
- package/dist/progress-adapter-DENrzF6I.mjs.map +1 -0
- package/dist/result-handler-iA9JtUC7.mjs +1186 -0
- package/dist/result-handler-iA9JtUC7.mjs.map +1 -0
- package/package.json +75 -38
- package/src/cli.ts +43 -0
- package/src/commands/contract-emit.ts +221 -111
- package/src/commands/db-init.ts +217 -426
- package/src/commands/db-introspect.ts +148 -185
- package/src/commands/db-schema-verify.ts +162 -149
- package/src/commands/db-sign.ts +215 -202
- package/src/commands/db-update.ts +220 -0
- package/src/commands/db-verify.ts +193 -156
- package/src/commands/migration-apply.ts +431 -0
- package/src/commands/migration-plan.ts +446 -0
- package/src/commands/migration-show.ts +255 -0
- package/src/commands/migration-status.ts +436 -0
- package/src/commands/migration-verify.ts +151 -0
- package/src/config-loader.ts +13 -3
- package/src/control-api/client.ts +605 -0
- package/src/control-api/errors.ts +9 -0
- package/src/control-api/operations/contract-emit.ts +161 -0
- package/src/control-api/operations/db-init.ts +286 -0
- package/src/control-api/operations/db-update.ts +221 -0
- package/src/control-api/operations/extract-sql-ddl.ts +47 -0
- package/src/control-api/operations/migration-apply.ts +195 -0
- package/src/control-api/operations/migration-helpers.ts +49 -0
- package/src/control-api/types.ts +687 -0
- package/src/exports/config-types.ts +3 -3
- package/src/exports/control-api.ts +53 -0
- package/src/load-ts-contract.ts +16 -11
- package/src/utils/cli-errors.ts +3 -1
- package/src/utils/command-helpers.ts +92 -3
- package/src/utils/framework-components.ts +11 -30
- package/src/utils/migration-command-scaffold.ts +190 -0
- package/src/utils/output.ts +363 -25
- package/src/utils/progress-adapter.ts +86 -0
- package/dist/chunk-464LNZCE.js +0 -134
- package/dist/chunk-464LNZCE.js.map +0 -1
- package/dist/chunk-BZMBKEEQ.js +0 -997
- package/dist/chunk-BZMBKEEQ.js.map +0 -1
- package/dist/chunk-HWYQOCAJ.js +0 -47
- package/dist/chunk-HWYQOCAJ.js.map +0 -1
- package/dist/chunk-ZKYEJROM.js +0 -94
- package/dist/chunk-ZKYEJROM.js.map +0 -1
- package/dist/cli.d.ts +0 -2
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/commands/contract-emit.d.ts +0 -3
- package/dist/commands/contract-emit.d.ts.map +0 -1
- package/dist/commands/contract-emit.js +0 -9
- package/dist/commands/contract-emit.js.map +0 -1
- package/dist/commands/db-init.d.ts +0 -3
- package/dist/commands/db-init.d.ts.map +0 -1
- package/dist/commands/db-init.js +0 -341
- package/dist/commands/db-init.js.map +0 -1
- package/dist/commands/db-introspect.d.ts +0 -3
- package/dist/commands/db-introspect.d.ts.map +0 -1
- package/dist/commands/db-introspect.js +0 -190
- package/dist/commands/db-introspect.js.map +0 -1
- package/dist/commands/db-schema-verify.d.ts +0 -3
- package/dist/commands/db-schema-verify.d.ts.map +0 -1
- package/dist/commands/db-schema-verify.js +0 -164
- package/dist/commands/db-schema-verify.js.map +0 -1
- package/dist/commands/db-sign.d.ts +0 -3
- package/dist/commands/db-sign.d.ts.map +0 -1
- package/dist/commands/db-sign.js +0 -199
- package/dist/commands/db-sign.js.map +0 -1
- package/dist/commands/db-verify.d.ts +0 -3
- package/dist/commands/db-verify.d.ts.map +0 -1
- package/dist/commands/db-verify.js +0 -173
- package/dist/commands/db-verify.js.map +0 -1
- package/dist/config-loader.d.ts.map +0 -1
- package/dist/config-loader.js +0 -7
- package/dist/config-loader.js.map +0 -1
- package/dist/exports/config-types.d.ts +0 -3
- package/dist/exports/config-types.d.ts.map +0 -1
- package/dist/exports/config-types.js +0 -6
- package/dist/exports/config-types.js.map +0 -1
- package/dist/exports/index.d.ts +0 -4
- package/dist/exports/index.d.ts.map +0 -1
- package/dist/exports/index.js +0 -175
- package/dist/exports/index.js.map +0 -1
- package/dist/load-ts-contract.d.ts.map +0 -1
- package/dist/utils/action.d.ts +0 -16
- package/dist/utils/action.d.ts.map +0 -1
- package/dist/utils/cli-errors.d.ts +0 -7
- package/dist/utils/cli-errors.d.ts.map +0 -1
- package/dist/utils/command-helpers.d.ts +0 -12
- package/dist/utils/command-helpers.d.ts.map +0 -1
- package/dist/utils/framework-components.d.ts +0 -81
- package/dist/utils/framework-components.d.ts.map +0 -1
- package/dist/utils/global-flags.d.ts +0 -25
- package/dist/utils/global-flags.d.ts.map +0 -1
- package/dist/utils/output.d.ts +0 -142
- package/dist/utils/output.d.ts.map +0 -1
- package/dist/utils/result-handler.d.ts +0 -15
- package/dist/utils/result-handler.d.ts.map +0 -1
- package/dist/utils/spinner.d.ts +0 -29
- package/dist/utils/spinner.d.ts.map +0 -1
- package/src/utils/action.ts +0 -43
- package/src/utils/spinner.ts +0 -67
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { EMPTY_CONTRACT_HASH } from '@prisma-next/core-control-plane/constants';
|
|
3
|
+
import { findPath, reconstructGraph } from '@prisma-next/migration-tools/dag';
|
|
4
|
+
import { readMigrationsDir } from '@prisma-next/migration-tools/io';
|
|
5
|
+
import type { MigrationGraph, MigrationPackage } from '@prisma-next/migration-tools/types';
|
|
6
|
+
import { MigrationToolsError } from '@prisma-next/migration-tools/types';
|
|
7
|
+
import { notOk, ok, type Result } from '@prisma-next/utils/result';
|
|
8
|
+
import { Command } from 'commander';
|
|
9
|
+
import { relative, resolve } from 'pathe';
|
|
10
|
+
import { loadConfig } from '../config-loader';
|
|
11
|
+
import { createControlClient } from '../control-api/client';
|
|
12
|
+
import type { MigrationApplyFailure, MigrationApplyStep } from '../control-api/types';
|
|
13
|
+
import {
|
|
14
|
+
CliStructuredError,
|
|
15
|
+
type CliStructuredError as CliStructuredErrorType,
|
|
16
|
+
errorDatabaseConnectionRequired,
|
|
17
|
+
errorDriverRequired,
|
|
18
|
+
errorRuntime,
|
|
19
|
+
errorTargetMigrationNotSupported,
|
|
20
|
+
errorUnexpected,
|
|
21
|
+
} from '../utils/cli-errors';
|
|
22
|
+
import {
|
|
23
|
+
maskConnectionUrl,
|
|
24
|
+
resolveContractPath,
|
|
25
|
+
setCommandDescriptions,
|
|
26
|
+
} from '../utils/command-helpers';
|
|
27
|
+
import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
|
|
28
|
+
import {
|
|
29
|
+
formatCommandHelp,
|
|
30
|
+
formatMigrationApplyCommandOutput,
|
|
31
|
+
formatStyledHeader,
|
|
32
|
+
} from '../utils/output';
|
|
33
|
+
import { handleResult } from '../utils/result-handler';
|
|
34
|
+
|
|
35
|
+
interface MigrationApplyCommandOptions {
|
|
36
|
+
readonly db?: string;
|
|
37
|
+
readonly config?: string;
|
|
38
|
+
readonly json?: string | boolean;
|
|
39
|
+
readonly quiet?: boolean;
|
|
40
|
+
readonly q?: boolean;
|
|
41
|
+
readonly verbose?: boolean;
|
|
42
|
+
readonly v?: boolean;
|
|
43
|
+
readonly vv?: boolean;
|
|
44
|
+
readonly trace?: boolean;
|
|
45
|
+
readonly timestamps?: boolean;
|
|
46
|
+
readonly color?: boolean;
|
|
47
|
+
readonly 'no-color'?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface MigrationApplyResult {
|
|
51
|
+
readonly ok: boolean;
|
|
52
|
+
readonly migrationsApplied: number;
|
|
53
|
+
readonly migrationsTotal: number;
|
|
54
|
+
readonly markerHash: string;
|
|
55
|
+
readonly applied: readonly {
|
|
56
|
+
readonly dirName: string;
|
|
57
|
+
readonly from: string;
|
|
58
|
+
readonly to: string;
|
|
59
|
+
readonly operationsExecuted: number;
|
|
60
|
+
}[];
|
|
61
|
+
readonly summary: string;
|
|
62
|
+
readonly timings: {
|
|
63
|
+
readonly total: number;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function mapMigrationToolsError(error: unknown): CliStructuredErrorType {
|
|
68
|
+
if (MigrationToolsError.is(error)) {
|
|
69
|
+
return errorRuntime(error.message, {
|
|
70
|
+
why: error.why,
|
|
71
|
+
fix: error.fix,
|
|
72
|
+
meta: { code: error.code, ...(error.details ?? {}) },
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
76
|
+
why: `Unexpected error during migration apply: ${error instanceof Error ? error.message : String(error)}`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function mapApplyFailure(failure: MigrationApplyFailure): CliStructuredErrorType {
|
|
81
|
+
return errorRuntime(failure.summary, {
|
|
82
|
+
why: failure.why ?? 'Migration runner failed',
|
|
83
|
+
fix: 'Fix the issue and re-run `prisma-next migration apply` — previously applied migrations are preserved.',
|
|
84
|
+
meta: failure.meta ?? {},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function packageToStep(pkg: MigrationPackage): MigrationApplyStep {
|
|
89
|
+
return {
|
|
90
|
+
dirName: pkg.dirName,
|
|
91
|
+
from: pkg.manifest.from,
|
|
92
|
+
to: pkg.manifest.to,
|
|
93
|
+
toContract: pkg.manifest.toContract,
|
|
94
|
+
operations: pkg.ops as MigrationApplyStep['operations'],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function executeMigrationApplyCommand(
|
|
99
|
+
options: MigrationApplyCommandOptions,
|
|
100
|
+
flags: GlobalFlags,
|
|
101
|
+
startTime: number,
|
|
102
|
+
): Promise<Result<MigrationApplyResult, CliStructuredErrorType>> {
|
|
103
|
+
const config = await loadConfig(options.config);
|
|
104
|
+
const configPath = options.config
|
|
105
|
+
? relative(process.cwd(), resolve(options.config))
|
|
106
|
+
: 'prisma-next.config.ts';
|
|
107
|
+
|
|
108
|
+
const migrationsDir = resolve(
|
|
109
|
+
options.config ? resolve(options.config, '..') : process.cwd(),
|
|
110
|
+
config.migrations?.dir ?? 'migrations',
|
|
111
|
+
);
|
|
112
|
+
const migrationsRelative = relative(process.cwd(), migrationsDir);
|
|
113
|
+
|
|
114
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
115
|
+
if (!dbConnection) {
|
|
116
|
+
return notOk(
|
|
117
|
+
errorDatabaseConnectionRequired({
|
|
118
|
+
why: `Database connection is required for migration apply (set db.connection in ${configPath}, or pass --db <url>)`,
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!config.driver) {
|
|
124
|
+
return notOk(errorDriverRequired({ why: 'Config.driver is required for migration apply' }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const targetWithMigrations = config.target as typeof config.target & {
|
|
128
|
+
readonly migrations?: unknown;
|
|
129
|
+
};
|
|
130
|
+
if (!targetWithMigrations.migrations) {
|
|
131
|
+
return notOk(
|
|
132
|
+
errorTargetMigrationNotSupported({
|
|
133
|
+
why: `Target "${config.target.id}" does not support migrations`,
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let destinationHash: string;
|
|
139
|
+
try {
|
|
140
|
+
const contractPathAbsolute = resolveContractPath(config);
|
|
141
|
+
const contractRaw = JSON.parse(await readFile(contractPathAbsolute, 'utf-8')) as Record<
|
|
142
|
+
string,
|
|
143
|
+
unknown
|
|
144
|
+
>;
|
|
145
|
+
const contractHash = contractRaw['storageHash'];
|
|
146
|
+
if (typeof contractHash !== 'string') {
|
|
147
|
+
return notOk(
|
|
148
|
+
errorRuntime('Current contract is missing storage hash', {
|
|
149
|
+
why: `The contract at ${relative(process.cwd(), contractPathAbsolute)} does not contain a valid storageHash`,
|
|
150
|
+
fix: 'Run `prisma-next contract emit` and re-run `prisma-next migration apply`.',
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
destinationHash = contractHash;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
return notOk(
|
|
157
|
+
errorRuntime('Current contract is unavailable', {
|
|
158
|
+
why: `Failed to read contract hash before apply: ${error instanceof Error ? error.message : String(error)}`,
|
|
159
|
+
fix: 'Run `prisma-next contract emit` to generate a valid contract.json, then retry apply.',
|
|
160
|
+
}),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (flags.json !== 'object' && !flags.quiet) {
|
|
165
|
+
const details: Array<{ label: string; value: string }> = [
|
|
166
|
+
{ label: 'config', value: configPath },
|
|
167
|
+
{ label: 'migrations', value: migrationsRelative },
|
|
168
|
+
];
|
|
169
|
+
if (typeof dbConnection === 'string') {
|
|
170
|
+
details.push({ label: 'database', value: maskConnectionUrl(dbConnection) });
|
|
171
|
+
}
|
|
172
|
+
const header = formatStyledHeader({
|
|
173
|
+
command: 'migration apply',
|
|
174
|
+
description: 'Apply planned migrations to the database',
|
|
175
|
+
url: 'https://pris.ly/migration-apply',
|
|
176
|
+
details,
|
|
177
|
+
flags,
|
|
178
|
+
});
|
|
179
|
+
console.log(header);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Read migrations and build migration chain model (offline — no DB needed)
|
|
183
|
+
let packages: readonly MigrationPackage[];
|
|
184
|
+
try {
|
|
185
|
+
const allPackages = await readMigrationsDir(migrationsDir);
|
|
186
|
+
packages = allPackages.filter((p) => typeof p.manifest.migrationId === 'string');
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (MigrationToolsError.is(error)) {
|
|
189
|
+
return notOk(mapMigrationToolsError(error));
|
|
190
|
+
}
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (packages.length === 0) {
|
|
195
|
+
const client = createControlClient({
|
|
196
|
+
family: config.family,
|
|
197
|
+
target: config.target,
|
|
198
|
+
adapter: config.adapter,
|
|
199
|
+
driver: config.driver,
|
|
200
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
201
|
+
});
|
|
202
|
+
try {
|
|
203
|
+
await client.connect(dbConnection);
|
|
204
|
+
const marker = await client.readMarker();
|
|
205
|
+
const markerHash = marker?.storageHash ?? EMPTY_CONTRACT_HASH;
|
|
206
|
+
if (markerHash !== EMPTY_CONTRACT_HASH) {
|
|
207
|
+
return notOk(
|
|
208
|
+
errorRuntime('Database has state but no migrations exist', {
|
|
209
|
+
why: `The database marker hash "${markerHash}" exists but no attested migrations were found in ${migrationsRelative}`,
|
|
210
|
+
fix: 'Ensure the migrations directory is correct, or reset the database with `prisma-next db init`.',
|
|
211
|
+
meta: { markerHash, migrationsDir: migrationsRelative },
|
|
212
|
+
}),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
if (destinationHash !== EMPTY_CONTRACT_HASH) {
|
|
216
|
+
return notOk(
|
|
217
|
+
errorRuntime('Current contract has no planned migrations', {
|
|
218
|
+
why: `No attested migrations were found in ${migrationsRelative}, but current contract hash is "${destinationHash}"`,
|
|
219
|
+
fix: 'Run `prisma-next migration plan` to create an attested migration for the current contract.',
|
|
220
|
+
meta: { destinationHash, migrationsDir: migrationsRelative },
|
|
221
|
+
}),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
} catch (error) {
|
|
225
|
+
if (CliStructuredError.is(error)) {
|
|
226
|
+
return notOk(error);
|
|
227
|
+
}
|
|
228
|
+
return notOk(
|
|
229
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
230
|
+
why: `Unexpected error during migration apply: ${error instanceof Error ? error.message : String(error)}`,
|
|
231
|
+
}),
|
|
232
|
+
);
|
|
233
|
+
} finally {
|
|
234
|
+
await client.close();
|
|
235
|
+
}
|
|
236
|
+
return ok({
|
|
237
|
+
ok: true,
|
|
238
|
+
migrationsApplied: 0,
|
|
239
|
+
migrationsTotal: 0,
|
|
240
|
+
markerHash: EMPTY_CONTRACT_HASH,
|
|
241
|
+
applied: [],
|
|
242
|
+
summary: 'No attested migrations found',
|
|
243
|
+
timings: { total: Date.now() - startTime },
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
let graph: MigrationGraph;
|
|
248
|
+
try {
|
|
249
|
+
graph = reconstructGraph(packages);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
if (MigrationToolsError.is(error)) {
|
|
252
|
+
return notOk(mapMigrationToolsError(error));
|
|
253
|
+
}
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Create control client for all DB operations
|
|
258
|
+
const client = createControlClient({
|
|
259
|
+
family: config.family,
|
|
260
|
+
target: config.target,
|
|
261
|
+
adapter: config.adapter,
|
|
262
|
+
driver: config.driver,
|
|
263
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
try {
|
|
267
|
+
await client.connect(dbConnection);
|
|
268
|
+
|
|
269
|
+
const marker = await client.readMarker();
|
|
270
|
+
|
|
271
|
+
// Distinguish "no marker row" (null) from "marker row exists with the
|
|
272
|
+
// empty sentinel". The sentinel should never appear in a real marker row —
|
|
273
|
+
// if it does, the marker was corrupted and replaying all migrations would
|
|
274
|
+
// be dangerous (the DB likely already has tables).
|
|
275
|
+
if (marker?.storageHash === EMPTY_CONTRACT_HASH) {
|
|
276
|
+
return notOk(
|
|
277
|
+
errorRuntime('Database marker contains the empty sentinel hash', {
|
|
278
|
+
why: `The marker row exists but contains the empty sentinel value "${EMPTY_CONTRACT_HASH}". This should never happen — the marker should contain the hash of the last applied contract.`,
|
|
279
|
+
fix: 'The marker is corrupted. Reset the database with `prisma-next db init`, or manually update the marker to the correct contract hash.',
|
|
280
|
+
meta: { markerHash: EMPTY_CONTRACT_HASH },
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const markerHash = marker?.storageHash ?? EMPTY_CONTRACT_HASH;
|
|
286
|
+
|
|
287
|
+
if (markerHash !== EMPTY_CONTRACT_HASH && !graph.nodes.has(markerHash)) {
|
|
288
|
+
return notOk(
|
|
289
|
+
errorRuntime('Database marker does not match any known migration', {
|
|
290
|
+
why: `The database marker hash "${markerHash}" is not found in the migration history at ${migrationsRelative}`,
|
|
291
|
+
fix: 'Ensure the migrations directory matches this database, or reset the database with `prisma-next db init`.',
|
|
292
|
+
meta: { markerHash, knownNodes: [...graph.nodes] },
|
|
293
|
+
}),
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (!graph.nodes.has(destinationHash)) {
|
|
298
|
+
return notOk(
|
|
299
|
+
errorRuntime('Current contract has no planned migration path', {
|
|
300
|
+
why: `Current contract hash "${destinationHash}" is not present in the migration history at ${migrationsRelative}`,
|
|
301
|
+
fix: 'Run `prisma-next migration plan` to create a migration for the current contract, then re-run apply.',
|
|
302
|
+
meta: { destinationHash, knownNodes: [...graph.nodes] },
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const pendingPath = findPath(graph, markerHash, destinationHash);
|
|
308
|
+
if (!pendingPath) {
|
|
309
|
+
return notOk(
|
|
310
|
+
errorRuntime('No migration path from current state to target', {
|
|
311
|
+
why: `Cannot find a path from marker hash "${markerHash}" to target "${destinationHash}"`,
|
|
312
|
+
fix: 'Check the migration history for gaps or inconsistencies.',
|
|
313
|
+
meta: { markerHash, destinationHash },
|
|
314
|
+
}),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (pendingPath.length === 0) {
|
|
319
|
+
return ok({
|
|
320
|
+
ok: true,
|
|
321
|
+
migrationsApplied: 0,
|
|
322
|
+
migrationsTotal: 0,
|
|
323
|
+
markerHash,
|
|
324
|
+
applied: [],
|
|
325
|
+
summary: 'Already up to date',
|
|
326
|
+
timings: { total: Date.now() - startTime },
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Resolve graph edges to full apply-ready edges
|
|
331
|
+
const packageByDir = new Map(packages.map((pkg) => [pkg.dirName, pkg]));
|
|
332
|
+
const pendingMigrations: MigrationApplyStep[] = [];
|
|
333
|
+
for (const migration of pendingPath) {
|
|
334
|
+
const pkg = packageByDir.get(migration.dirName);
|
|
335
|
+
if (!pkg) {
|
|
336
|
+
return notOk(
|
|
337
|
+
errorRuntime(`Migration package not found: ${migration.dirName}`, {
|
|
338
|
+
why: `The migration directory for path segment ${migration.from} → ${migration.to} was not found`,
|
|
339
|
+
fix: 'Ensure all migration directories are present and intact.',
|
|
340
|
+
}),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
pendingMigrations.push(packageToStep(pkg));
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (!flags.quiet && flags.json !== 'object') {
|
|
347
|
+
for (const migration of pendingMigrations) {
|
|
348
|
+
console.log(` Applying ${migration.dirName}...`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const applyResult = await client.migrationApply({
|
|
353
|
+
originHash: markerHash,
|
|
354
|
+
destinationHash,
|
|
355
|
+
pendingMigrations,
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
if (!applyResult.ok) {
|
|
359
|
+
return notOk(mapApplyFailure(applyResult.failure));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const { value } = applyResult;
|
|
363
|
+
|
|
364
|
+
return ok({
|
|
365
|
+
ok: true,
|
|
366
|
+
migrationsApplied: value.migrationsApplied,
|
|
367
|
+
migrationsTotal: pendingPath.length,
|
|
368
|
+
markerHash: value.markerHash,
|
|
369
|
+
applied: value.applied,
|
|
370
|
+
summary: value.summary,
|
|
371
|
+
timings: { total: Date.now() - startTime },
|
|
372
|
+
});
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (CliStructuredError.is(error)) {
|
|
375
|
+
return notOk(error);
|
|
376
|
+
}
|
|
377
|
+
return notOk(
|
|
378
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
379
|
+
why: `Unexpected error during migration apply: ${error instanceof Error ? error.message : String(error)}`,
|
|
380
|
+
}),
|
|
381
|
+
);
|
|
382
|
+
} finally {
|
|
383
|
+
await client.close();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function createMigrationApplyCommand(): Command {
|
|
388
|
+
const command = new Command('apply');
|
|
389
|
+
setCommandDescriptions(
|
|
390
|
+
command,
|
|
391
|
+
'Apply planned migrations to the database',
|
|
392
|
+
'Applies previously planned migrations (created by `migration plan`) to a live database.\n' +
|
|
393
|
+
'Compares the database marker against the migration chain to determine which\n' +
|
|
394
|
+
'migrations are pending, then executes them sequentially. Each migration runs\n' +
|
|
395
|
+
'in its own transaction. Does not plan new migrations — run `migration plan` first.',
|
|
396
|
+
);
|
|
397
|
+
command
|
|
398
|
+
.configureHelp({
|
|
399
|
+
formatHelp: (cmd) => {
|
|
400
|
+
const defaultFlags = parseGlobalFlags({});
|
|
401
|
+
return formatCommandHelp({ command: cmd, flags: defaultFlags });
|
|
402
|
+
},
|
|
403
|
+
})
|
|
404
|
+
.option('--db <url>', 'Database connection string')
|
|
405
|
+
.option('--config <path>', 'Path to prisma-next.config.ts')
|
|
406
|
+
.option('--json [format]', 'Output as JSON (object)', false)
|
|
407
|
+
.option('-q, --quiet', 'Quiet mode: errors only')
|
|
408
|
+
.option('-v, --verbose', 'Verbose output: debug info, timings')
|
|
409
|
+
.option('-vv, --trace', 'Trace output: deep internals, stack traces')
|
|
410
|
+
.option('--timestamps', 'Add timestamps to output')
|
|
411
|
+
.option('--color', 'Force color output')
|
|
412
|
+
.option('--no-color', 'Disable color output')
|
|
413
|
+
.action(async (options: MigrationApplyCommandOptions) => {
|
|
414
|
+
const flags = parseGlobalFlags(options);
|
|
415
|
+
const startTime = Date.now();
|
|
416
|
+
|
|
417
|
+
const result = await executeMigrationApplyCommand(options, flags, startTime);
|
|
418
|
+
|
|
419
|
+
const exitCode = handleResult(result, flags, (applyResult) => {
|
|
420
|
+
if (flags.json === 'object') {
|
|
421
|
+
console.log(JSON.stringify(applyResult, null, 2));
|
|
422
|
+
} else if (!flags.quiet) {
|
|
423
|
+
console.log(formatMigrationApplyCommandOutput(applyResult, flags));
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
process.exit(exitCode);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
return command;
|
|
431
|
+
}
|