@prisma-next/cli 0.3.0-dev.4 → 0.3.0-dev.6
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.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/commands/contract-emit.d.ts +2 -4
- package/dist/commands/contract-emit.d.ts.map +1 -0
- package/dist/commands/db-init.d.ts +2 -4
- package/dist/commands/db-init.d.ts.map +1 -0
- package/dist/commands/db-introspect.d.ts +2 -4
- package/dist/commands/db-introspect.d.ts.map +1 -0
- package/dist/commands/db-schema-verify.d.ts +2 -4
- package/dist/commands/db-schema-verify.d.ts.map +1 -0
- package/dist/commands/db-sign.d.ts +2 -4
- package/dist/commands/db-sign.d.ts.map +1 -0
- package/dist/commands/db-verify.d.ts +2 -4
- package/dist/commands/db-verify.d.ts.map +1 -0
- package/dist/config-loader.d.ts +3 -5
- package/dist/config-loader.d.ts.map +1 -0
- package/dist/exports/config-types.d.ts +3 -0
- package/dist/exports/config-types.d.ts.map +1 -0
- package/dist/exports/config-types.js.map +1 -0
- package/dist/exports/index.d.ts +4 -0
- package/dist/exports/index.d.ts.map +1 -0
- package/dist/{index.js → exports/index.js} +3 -3
- package/dist/exports/index.js.map +1 -0
- package/dist/{index.d.ts → load-ts-contract.d.ts} +4 -8
- package/dist/load-ts-contract.d.ts.map +1 -0
- package/dist/utils/action.d.ts +16 -0
- package/dist/utils/action.d.ts.map +1 -0
- package/dist/utils/cli-errors.d.ts +7 -0
- package/dist/utils/cli-errors.d.ts.map +1 -0
- package/dist/utils/command-helpers.d.ts +12 -0
- package/dist/utils/command-helpers.d.ts.map +1 -0
- package/dist/utils/framework-components.d.ts +81 -0
- package/dist/utils/framework-components.d.ts.map +1 -0
- package/dist/utils/global-flags.d.ts +25 -0
- package/dist/utils/global-flags.d.ts.map +1 -0
- package/dist/utils/output.d.ts +142 -0
- package/dist/utils/output.d.ts.map +1 -0
- package/dist/utils/result-handler.d.ts +15 -0
- package/dist/utils/result-handler.d.ts.map +1 -0
- package/dist/utils/spinner.d.ts +29 -0
- package/dist/utils/spinner.d.ts.map +1 -0
- package/package.json +17 -16
- package/src/cli.ts +260 -0
- package/src/commands/contract-emit.ts +189 -0
- package/src/commands/db-init.ts +456 -0
- package/src/commands/db-introspect.ts +260 -0
- package/src/commands/db-schema-verify.ts +236 -0
- package/src/commands/db-sign.ts +277 -0
- package/src/commands/db-verify.ts +233 -0
- package/src/config-loader.ts +76 -0
- package/src/exports/config-types.ts +6 -0
- package/src/exports/index.ts +4 -0
- package/src/load-ts-contract.ts +217 -0
- package/src/utils/action.ts +43 -0
- package/src/utils/cli-errors.ts +26 -0
- package/src/utils/command-helpers.ts +26 -0
- package/src/utils/framework-components.ts +196 -0
- package/src/utils/global-flags.ts +75 -0
- package/src/utils/output.ts +1471 -0
- package/src/utils/result-handler.ts +44 -0
- package/src/utils/spinner.ts +67 -0
- package/dist/config-types.d.ts +0 -1
- package/dist/config-types.js.map +0 -1
- package/dist/index.js.map +0 -1
- /package/dist/{config-types.js → exports/config-types.js} +0 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { relative, resolve } from 'node:path';
|
|
3
|
+
import type {
|
|
4
|
+
MigrationPlan,
|
|
5
|
+
MigrationPlannerResult,
|
|
6
|
+
MigrationPlanOperation,
|
|
7
|
+
MigrationRunnerResult,
|
|
8
|
+
} from '@prisma-next/core-control-plane/types';
|
|
9
|
+
import { redactDatabaseUrl } from '@prisma-next/utils/redact-db-url';
|
|
10
|
+
import { Command } from 'commander';
|
|
11
|
+
import { loadConfig } from '../config-loader';
|
|
12
|
+
import { performAction } from '../utils/action';
|
|
13
|
+
import {
|
|
14
|
+
errorContractValidationFailed,
|
|
15
|
+
errorDatabaseUrlRequired,
|
|
16
|
+
errorDriverRequired,
|
|
17
|
+
errorFileNotFound,
|
|
18
|
+
errorJsonFormatNotSupported,
|
|
19
|
+
errorMigrationPlanningFailed,
|
|
20
|
+
errorRuntime,
|
|
21
|
+
errorTargetMigrationNotSupported,
|
|
22
|
+
errorUnexpected,
|
|
23
|
+
} from '../utils/cli-errors';
|
|
24
|
+
import { setCommandDescriptions } from '../utils/command-helpers';
|
|
25
|
+
import {
|
|
26
|
+
assertContractRequirementsSatisfied,
|
|
27
|
+
assertFrameworkComponentsCompatible,
|
|
28
|
+
} from '../utils/framework-components';
|
|
29
|
+
import { parseGlobalFlags } from '../utils/global-flags';
|
|
30
|
+
import {
|
|
31
|
+
type DbInitResult,
|
|
32
|
+
formatCommandHelp,
|
|
33
|
+
formatDbInitApplyOutput,
|
|
34
|
+
formatDbInitJson,
|
|
35
|
+
formatDbInitPlanOutput,
|
|
36
|
+
formatStyledHeader,
|
|
37
|
+
} from '../utils/output';
|
|
38
|
+
import { handleResult } from '../utils/result-handler';
|
|
39
|
+
import { withSpinner } from '../utils/spinner';
|
|
40
|
+
|
|
41
|
+
interface DbInitOptions {
|
|
42
|
+
readonly db?: string;
|
|
43
|
+
readonly config?: string;
|
|
44
|
+
readonly plan?: boolean;
|
|
45
|
+
readonly json?: string | boolean;
|
|
46
|
+
readonly quiet?: boolean;
|
|
47
|
+
readonly q?: boolean;
|
|
48
|
+
readonly verbose?: boolean;
|
|
49
|
+
readonly v?: boolean;
|
|
50
|
+
readonly vv?: boolean;
|
|
51
|
+
readonly trace?: boolean;
|
|
52
|
+
readonly timestamps?: boolean;
|
|
53
|
+
readonly color?: boolean;
|
|
54
|
+
readonly 'no-color'?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function createDbInitCommand(): Command {
|
|
58
|
+
const command = new Command('init');
|
|
59
|
+
setCommandDescriptions(
|
|
60
|
+
command,
|
|
61
|
+
'Bootstrap a database to match the current contract and write the contract marker',
|
|
62
|
+
'Initializes a database to match your emitted contract using additive-only operations.\n' +
|
|
63
|
+
'Creates any missing tables, columns, indexes, and constraints defined in your contract.\n' +
|
|
64
|
+
'Leaves existing compatible structures in place, surfaces conflicts when destructive changes\n' +
|
|
65
|
+
'would be required, and writes a contract marker to track the database state. Use --plan to\n' +
|
|
66
|
+
'preview changes without applying.',
|
|
67
|
+
);
|
|
68
|
+
command
|
|
69
|
+
.configureHelp({
|
|
70
|
+
formatHelp: (cmd) => {
|
|
71
|
+
const flags = parseGlobalFlags({});
|
|
72
|
+
return formatCommandHelp({ command: cmd, flags });
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
.option('--db <url>', 'Database connection string')
|
|
76
|
+
.option('--config <path>', 'Path to prisma-next.config.ts')
|
|
77
|
+
.option('--plan', 'Preview planned operations without applying', false)
|
|
78
|
+
.option('--json [format]', 'Output as JSON (object)', false)
|
|
79
|
+
.option('-q, --quiet', 'Quiet mode: errors only')
|
|
80
|
+
.option('-v, --verbose', 'Verbose output: debug info, timings')
|
|
81
|
+
.option('-vv, --trace', 'Trace output: deep internals, stack traces')
|
|
82
|
+
.option('--timestamps', 'Add timestamps to output')
|
|
83
|
+
.option('--color', 'Force color output')
|
|
84
|
+
.option('--no-color', 'Disable color output')
|
|
85
|
+
.action(async (options: DbInitOptions) => {
|
|
86
|
+
const flags = parseGlobalFlags(options);
|
|
87
|
+
const startTime = Date.now();
|
|
88
|
+
|
|
89
|
+
const result = await performAction(async () => {
|
|
90
|
+
if (flags.json === 'ndjson') {
|
|
91
|
+
throw errorJsonFormatNotSupported({
|
|
92
|
+
command: 'db init',
|
|
93
|
+
format: 'ndjson',
|
|
94
|
+
supportedFormats: ['object'],
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Load config
|
|
99
|
+
const config = await loadConfig(options.config);
|
|
100
|
+
const configPath = options.config
|
|
101
|
+
? relative(process.cwd(), resolve(options.config))
|
|
102
|
+
: 'prisma-next.config.ts';
|
|
103
|
+
const contractPathAbsolute = config.contract?.output
|
|
104
|
+
? resolve(config.contract.output)
|
|
105
|
+
: resolve('src/prisma/contract.json');
|
|
106
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
107
|
+
|
|
108
|
+
// Output header
|
|
109
|
+
if (flags.json !== 'object' && !flags.quiet) {
|
|
110
|
+
const details: Array<{ label: string; value: string }> = [
|
|
111
|
+
{ label: 'config', value: configPath },
|
|
112
|
+
{ label: 'contract', value: contractPath },
|
|
113
|
+
];
|
|
114
|
+
if (options.db) {
|
|
115
|
+
details.push({ label: 'database', value: options.db });
|
|
116
|
+
}
|
|
117
|
+
if (options.plan) {
|
|
118
|
+
details.push({ label: 'mode', value: 'plan (dry run)' });
|
|
119
|
+
}
|
|
120
|
+
const header = formatStyledHeader({
|
|
121
|
+
command: 'db init',
|
|
122
|
+
description: 'Bootstrap a database to match the current contract',
|
|
123
|
+
url: 'https://pris.ly/db-init',
|
|
124
|
+
details,
|
|
125
|
+
flags,
|
|
126
|
+
});
|
|
127
|
+
console.log(header);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Load contract file
|
|
131
|
+
let contractJsonContent: string;
|
|
132
|
+
try {
|
|
133
|
+
contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
|
|
134
|
+
} catch (error) {
|
|
135
|
+
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
136
|
+
throw errorFileNotFound(contractPathAbsolute, {
|
|
137
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
138
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
throw errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
142
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let contractJson: Record<string, unknown>;
|
|
147
|
+
try {
|
|
148
|
+
contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
throw errorContractValidationFailed(
|
|
151
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
152
|
+
{ where: { path: contractPathAbsolute } },
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Resolve database URL
|
|
157
|
+
const dbUrl = options.db ?? config.db?.url;
|
|
158
|
+
if (!dbUrl) {
|
|
159
|
+
throw errorDatabaseUrlRequired({
|
|
160
|
+
why: `Database URL is required for db init (set db.url in ${configPath}, or pass --db <url>)`,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Check for driver
|
|
165
|
+
if (!config.driver) {
|
|
166
|
+
throw errorDriverRequired({ why: 'Config.driver is required for db init' });
|
|
167
|
+
}
|
|
168
|
+
const driverDescriptor = config.driver;
|
|
169
|
+
|
|
170
|
+
// Check target supports migrations via the migrations capability
|
|
171
|
+
if (!config.target.migrations) {
|
|
172
|
+
throw errorTargetMigrationNotSupported({
|
|
173
|
+
why: `Target "${config.target.id}" does not support migrations`,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const migrations = config.target.migrations;
|
|
177
|
+
|
|
178
|
+
// Create driver
|
|
179
|
+
let driver: Awaited<ReturnType<(typeof driverDescriptor)['create']>>;
|
|
180
|
+
try {
|
|
181
|
+
driver = await withSpinner(() => driverDescriptor.create(dbUrl), {
|
|
182
|
+
message: 'Connecting to database...',
|
|
183
|
+
flags,
|
|
184
|
+
});
|
|
185
|
+
} catch (error) {
|
|
186
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
187
|
+
const code = (error as { code?: unknown }).code;
|
|
188
|
+
const redacted = redactDatabaseUrl(dbUrl);
|
|
189
|
+
throw errorRuntime('Database connection failed', {
|
|
190
|
+
why: message,
|
|
191
|
+
fix: 'Verify the database URL, ensure the database is reachable, and confirm credentials/permissions',
|
|
192
|
+
meta: {
|
|
193
|
+
...(typeof code !== 'undefined' ? { code } : {}),
|
|
194
|
+
...redacted,
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
// Create family instance
|
|
201
|
+
const familyInstance = config.family.create({
|
|
202
|
+
target: config.target,
|
|
203
|
+
adapter: config.adapter,
|
|
204
|
+
driver: driverDescriptor,
|
|
205
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
206
|
+
});
|
|
207
|
+
const rawComponents = [config.target, config.adapter, ...(config.extensionPacks ?? [])];
|
|
208
|
+
const frameworkComponents = assertFrameworkComponentsCompatible(
|
|
209
|
+
config.family.familyId,
|
|
210
|
+
config.target.targetId,
|
|
211
|
+
rawComponents,
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
// Validate contract
|
|
215
|
+
const contractIR = familyInstance.validateContractIR(contractJson);
|
|
216
|
+
assertContractRequirementsSatisfied({
|
|
217
|
+
contract: contractIR,
|
|
218
|
+
family: config.family,
|
|
219
|
+
target: config.target,
|
|
220
|
+
adapter: config.adapter,
|
|
221
|
+
extensionPacks: config.extensionPacks,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Create planner and runner from target migrations capability
|
|
225
|
+
const planner = migrations.createPlanner(familyInstance);
|
|
226
|
+
const runner = migrations.createRunner(familyInstance);
|
|
227
|
+
|
|
228
|
+
// Introspect live schema
|
|
229
|
+
const schemaIR = await withSpinner(() => familyInstance.introspect({ driver }), {
|
|
230
|
+
message: 'Introspecting database schema...',
|
|
231
|
+
flags,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// Policy for init mode (additive only)
|
|
235
|
+
const policy = { allowedOperationClasses: ['additive'] as const };
|
|
236
|
+
|
|
237
|
+
// Plan migration
|
|
238
|
+
const plannerResult: MigrationPlannerResult = await withSpinner(
|
|
239
|
+
async () =>
|
|
240
|
+
planner.plan({
|
|
241
|
+
contract: contractIR,
|
|
242
|
+
schema: schemaIR,
|
|
243
|
+
policy,
|
|
244
|
+
frameworkComponents,
|
|
245
|
+
}),
|
|
246
|
+
{
|
|
247
|
+
message: 'Planning migration...',
|
|
248
|
+
flags,
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
if (plannerResult.kind === 'failure') {
|
|
253
|
+
throw errorMigrationPlanningFailed({ conflicts: plannerResult.conflicts });
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const migrationPlan: MigrationPlan = plannerResult.plan;
|
|
257
|
+
|
|
258
|
+
// Check for existing marker - handle idempotency and mismatch errors
|
|
259
|
+
const existingMarker = await familyInstance.readMarker({ driver });
|
|
260
|
+
if (existingMarker) {
|
|
261
|
+
const markerMatchesDestination =
|
|
262
|
+
existingMarker.coreHash === migrationPlan.destination.coreHash &&
|
|
263
|
+
(!migrationPlan.destination.profileHash ||
|
|
264
|
+
existingMarker.profileHash === migrationPlan.destination.profileHash);
|
|
265
|
+
|
|
266
|
+
if (markerMatchesDestination) {
|
|
267
|
+
// Already at destination - return success with no operations
|
|
268
|
+
const dbInitResult: DbInitResult = {
|
|
269
|
+
ok: true,
|
|
270
|
+
mode: options.plan ? 'plan' : 'apply',
|
|
271
|
+
plan: {
|
|
272
|
+
targetId: migrationPlan.targetId,
|
|
273
|
+
destination: migrationPlan.destination,
|
|
274
|
+
operations: [],
|
|
275
|
+
},
|
|
276
|
+
...(options.plan
|
|
277
|
+
? {}
|
|
278
|
+
: {
|
|
279
|
+
execution: { operationsPlanned: 0, operationsExecuted: 0 },
|
|
280
|
+
marker: {
|
|
281
|
+
coreHash: existingMarker.coreHash,
|
|
282
|
+
profileHash: existingMarker.profileHash,
|
|
283
|
+
},
|
|
284
|
+
}),
|
|
285
|
+
summary: 'Database already at target contract state',
|
|
286
|
+
timings: { total: Date.now() - startTime },
|
|
287
|
+
};
|
|
288
|
+
return dbInitResult;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Marker exists but doesn't match destination - fail
|
|
292
|
+
const coreHashMismatch = existingMarker.coreHash !== migrationPlan.destination.coreHash;
|
|
293
|
+
const profileHashMismatch =
|
|
294
|
+
migrationPlan.destination.profileHash &&
|
|
295
|
+
existingMarker.profileHash !== migrationPlan.destination.profileHash;
|
|
296
|
+
|
|
297
|
+
const mismatchParts: string[] = [];
|
|
298
|
+
if (coreHashMismatch) {
|
|
299
|
+
mismatchParts.push(
|
|
300
|
+
`coreHash (marker: ${existingMarker.coreHash}, destination: ${migrationPlan.destination.coreHash})`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
if (profileHashMismatch) {
|
|
304
|
+
mismatchParts.push(
|
|
305
|
+
`profileHash (marker: ${existingMarker.profileHash}, destination: ${migrationPlan.destination.profileHash})`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
throw errorRuntime(
|
|
310
|
+
`Existing contract marker does not match plan destination. Mismatch in ${mismatchParts.join(' and ')}.`,
|
|
311
|
+
{
|
|
312
|
+
why: 'Database has an existing contract marker that does not match the target contract',
|
|
313
|
+
fix: 'If bootstrapping, drop/reset the database then re-run `prisma-next db init`; otherwise reconcile schema/marker using your migration workflow',
|
|
314
|
+
meta: {
|
|
315
|
+
code: 'MARKER_ORIGIN_MISMATCH',
|
|
316
|
+
markerCoreHash: existingMarker.coreHash,
|
|
317
|
+
destinationCoreHash: migrationPlan.destination.coreHash,
|
|
318
|
+
...(existingMarker.profileHash
|
|
319
|
+
? { markerProfileHash: existingMarker.profileHash }
|
|
320
|
+
: {}),
|
|
321
|
+
...(migrationPlan.destination.profileHash
|
|
322
|
+
? { destinationProfileHash: migrationPlan.destination.profileHash }
|
|
323
|
+
: {}),
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Plan mode - don't execute
|
|
330
|
+
if (options.plan) {
|
|
331
|
+
const dbInitResult: DbInitResult = {
|
|
332
|
+
ok: true,
|
|
333
|
+
mode: 'plan',
|
|
334
|
+
plan: {
|
|
335
|
+
targetId: migrationPlan.targetId,
|
|
336
|
+
destination: migrationPlan.destination,
|
|
337
|
+
operations: migrationPlan.operations.map((op) => ({
|
|
338
|
+
id: op.id,
|
|
339
|
+
label: op.label,
|
|
340
|
+
operationClass: op.operationClass,
|
|
341
|
+
})),
|
|
342
|
+
},
|
|
343
|
+
summary: `Planned ${migrationPlan.operations.length} operation(s)`,
|
|
344
|
+
timings: { total: Date.now() - startTime },
|
|
345
|
+
};
|
|
346
|
+
return dbInitResult;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Apply mode - execute runner
|
|
350
|
+
// Log main message once, then show individual operations via callbacks
|
|
351
|
+
if (!flags.quiet && flags.json !== 'object') {
|
|
352
|
+
console.log('Applying migration plan and verifying schema...');
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const callbacks = {
|
|
356
|
+
onOperationStart: (op: MigrationPlanOperation) => {
|
|
357
|
+
if (!flags.quiet && flags.json !== 'object') {
|
|
358
|
+
console.log(` → ${op.label}...`);
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
onOperationComplete: (_op: MigrationPlanOperation) => {
|
|
362
|
+
// Could log completion if needed
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const runnerResult: MigrationRunnerResult = await runner.execute({
|
|
367
|
+
plan: migrationPlan,
|
|
368
|
+
driver,
|
|
369
|
+
destinationContract: contractIR,
|
|
370
|
+
policy,
|
|
371
|
+
callbacks,
|
|
372
|
+
// db init plans and applies back-to-back from a fresh introspection, so per-operation
|
|
373
|
+
// pre/postchecks and the idempotency probe are usually redundant overhead. We still
|
|
374
|
+
// enforce marker/origin compatibility and a full schema verification after apply.
|
|
375
|
+
executionChecks: {
|
|
376
|
+
prechecks: false,
|
|
377
|
+
postchecks: false,
|
|
378
|
+
idempotencyChecks: false,
|
|
379
|
+
},
|
|
380
|
+
frameworkComponents,
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
if (!runnerResult.ok) {
|
|
384
|
+
const meta: Record<string, unknown> = {
|
|
385
|
+
code: runnerResult.failure.code,
|
|
386
|
+
...(runnerResult.failure.meta ?? {}),
|
|
387
|
+
};
|
|
388
|
+
const sqlState = typeof meta['sqlState'] === 'string' ? meta['sqlState'] : undefined;
|
|
389
|
+
const fix =
|
|
390
|
+
sqlState === '42501'
|
|
391
|
+
? 'Grant the database user sufficient privileges (insufficient_privilege), or run db init as a more privileged role'
|
|
392
|
+
: runnerResult.failure.code === 'SCHEMA_VERIFY_FAILED'
|
|
393
|
+
? 'Fix the schema mismatch (db init is additive-only), or drop/reset the database and re-run `prisma-next db init`'
|
|
394
|
+
: undefined;
|
|
395
|
+
|
|
396
|
+
throw errorRuntime(runnerResult.failure.summary, {
|
|
397
|
+
why:
|
|
398
|
+
runnerResult.failure.why ?? `Migration runner failed: ${runnerResult.failure.code}`,
|
|
399
|
+
...(fix ? { fix } : {}),
|
|
400
|
+
meta,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const execution = runnerResult.value;
|
|
405
|
+
|
|
406
|
+
const dbInitResult: DbInitResult = {
|
|
407
|
+
ok: true,
|
|
408
|
+
mode: 'apply',
|
|
409
|
+
plan: {
|
|
410
|
+
targetId: migrationPlan.targetId,
|
|
411
|
+
destination: migrationPlan.destination,
|
|
412
|
+
operations: migrationPlan.operations.map((op) => ({
|
|
413
|
+
id: op.id,
|
|
414
|
+
label: op.label,
|
|
415
|
+
operationClass: op.operationClass,
|
|
416
|
+
})),
|
|
417
|
+
},
|
|
418
|
+
execution: {
|
|
419
|
+
operationsPlanned: execution.operationsPlanned,
|
|
420
|
+
operationsExecuted: execution.operationsExecuted,
|
|
421
|
+
},
|
|
422
|
+
marker: migrationPlan.destination.profileHash
|
|
423
|
+
? {
|
|
424
|
+
coreHash: migrationPlan.destination.coreHash,
|
|
425
|
+
profileHash: migrationPlan.destination.profileHash,
|
|
426
|
+
}
|
|
427
|
+
: { coreHash: migrationPlan.destination.coreHash },
|
|
428
|
+
summary: `Applied ${execution.operationsExecuted} operation(s), marker written`,
|
|
429
|
+
timings: { total: Date.now() - startTime },
|
|
430
|
+
};
|
|
431
|
+
return dbInitResult;
|
|
432
|
+
} finally {
|
|
433
|
+
await driver.close();
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
// Handle result
|
|
438
|
+
const exitCode = handleResult(result, flags, (dbInitResult) => {
|
|
439
|
+
if (flags.json === 'object') {
|
|
440
|
+
console.log(formatDbInitJson(dbInitResult));
|
|
441
|
+
} else {
|
|
442
|
+
const output =
|
|
443
|
+
dbInitResult.mode === 'plan'
|
|
444
|
+
? formatDbInitPlanOutput(dbInitResult, flags)
|
|
445
|
+
: formatDbInitApplyOutput(dbInitResult, flags);
|
|
446
|
+
if (output) {
|
|
447
|
+
console.log(output);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
process.exit(exitCode);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
return command;
|
|
456
|
+
}
|