@prisma-next/cli 0.3.0-dev.14 → 0.3.0-dev.16
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/chunk-5MPKZYVI.js +47 -0
- package/dist/chunk-5MPKZYVI.js.map +1 -0
- package/dist/chunk-74IELXRA.js +371 -0
- package/dist/chunk-74IELXRA.js.map +1 -0
- package/dist/{chunk-MG7PBERL.js → chunk-U6QI3AZ3.js} +7 -5
- package/dist/{chunk-MG7PBERL.js.map → chunk-U6QI3AZ3.js.map} +1 -1
- package/dist/{chunk-DIJPT5TZ.js → chunk-ZG5T6OB5.js} +2 -46
- package/dist/chunk-ZG5T6OB5.js.map +1 -0
- package/dist/cli.js +593 -268
- package/dist/cli.js.map +1 -1
- package/dist/commands/contract-emit.js +3 -2
- package/dist/commands/db-init.d.ts.map +1 -1
- package/dist/commands/db-init.js +238 -275
- package/dist/commands/db-init.js.map +1 -1
- package/dist/commands/db-introspect.js +6 -4
- package/dist/commands/db-introspect.js.map +1 -1
- package/dist/commands/db-schema-verify.js +6 -4
- package/dist/commands/db-schema-verify.js.map +1 -1
- package/dist/commands/db-sign.js +6 -4
- package/dist/commands/db-sign.js.map +1 -1
- package/dist/commands/db-verify.js +6 -4
- package/dist/commands/db-verify.js.map +1 -1
- package/dist/control-api/operations/db-init.d.ts +3 -1
- package/dist/control-api/operations/db-init.d.ts.map +1 -1
- package/dist/control-api/types.d.ts +54 -1
- package/dist/control-api/types.d.ts.map +1 -1
- package/dist/exports/control-api.d.ts +1 -1
- package/dist/exports/control-api.d.ts.map +1 -1
- package/dist/exports/control-api.js +3 -234
- package/dist/exports/control-api.js.map +1 -1
- package/dist/exports/index.js +3 -2
- package/dist/exports/index.js.map +1 -1
- package/dist/utils/progress-adapter.d.ts +26 -0
- package/dist/utils/progress-adapter.d.ts.map +1 -0
- package/package.json +10 -10
- package/src/commands/db-init.ts +262 -355
- package/src/control-api/client.ts +30 -0
- package/src/control-api/operations/db-init.ts +116 -2
- package/src/control-api/types.ts +63 -1
- package/src/exports/control-api.ts +3 -0
- package/src/utils/progress-adapter.ts +86 -0
- package/dist/chunk-DIJPT5TZ.js.map +0 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createContractEmitCommand
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-U6QI3AZ3.js";
|
|
4
|
+
import "../chunk-5MPKZYVI.js";
|
|
5
|
+
import "../chunk-ZG5T6OB5.js";
|
|
5
6
|
import "../chunk-HWYQOCAJ.js";
|
|
6
7
|
import "../chunk-VI2YETW7.js";
|
|
7
8
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db-init.d.ts","sourceRoot":"","sources":["../../src/commands/db-init.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"db-init.d.ts","sourceRoot":"","sources":["../../src/commands/db-init.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAmSpC,wBAAgB,mBAAmB,IAAI,OAAO,CAiE7C"}
|
package/dist/commands/db-init.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
createControlClient
|
|
3
|
+
} from "../chunk-74IELXRA.js";
|
|
4
|
+
import "../chunk-6EPKRATC.js";
|
|
5
5
|
import {
|
|
6
6
|
formatCommandHelp,
|
|
7
7
|
formatDbInitApplyOutput,
|
|
@@ -10,14 +10,13 @@ import {
|
|
|
10
10
|
formatStyledHeader,
|
|
11
11
|
handleResult,
|
|
12
12
|
parseGlobalFlags,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
withSpinner
|
|
16
|
-
} from "../chunk-DIJPT5TZ.js";
|
|
13
|
+
setCommandDescriptions
|
|
14
|
+
} from "../chunk-ZG5T6OB5.js";
|
|
17
15
|
import {
|
|
18
16
|
loadConfig
|
|
19
17
|
} from "../chunk-HWYQOCAJ.js";
|
|
20
18
|
import {
|
|
19
|
+
CliStructuredError,
|
|
21
20
|
errorContractValidationFailed,
|
|
22
21
|
errorDatabaseConnectionRequired,
|
|
23
22
|
errorDriverRequired,
|
|
@@ -32,9 +31,230 @@ import {
|
|
|
32
31
|
// src/commands/db-init.ts
|
|
33
32
|
import { readFile } from "fs/promises";
|
|
34
33
|
import { relative, resolve } from "path";
|
|
35
|
-
import {
|
|
36
|
-
import { redactDatabaseUrl } from "@prisma-next/utils/redact-db-url";
|
|
34
|
+
import { notOk, ok } from "@prisma-next/utils/result";
|
|
37
35
|
import { Command } from "commander";
|
|
36
|
+
|
|
37
|
+
// src/utils/progress-adapter.ts
|
|
38
|
+
import ora from "ora";
|
|
39
|
+
function createProgressAdapter(options) {
|
|
40
|
+
const { flags } = options;
|
|
41
|
+
const shouldShowProgress = !flags.quiet && flags.json !== "object" && process.stdout.isTTY;
|
|
42
|
+
if (!shouldShowProgress) {
|
|
43
|
+
return () => {
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const activeSpans = /* @__PURE__ */ new Map();
|
|
47
|
+
return (event) => {
|
|
48
|
+
if (event.kind === "spanStart") {
|
|
49
|
+
if (event.parentSpanId) {
|
|
50
|
+
console.log(` \u2192 ${event.label}...`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const spinner = ora({
|
|
54
|
+
text: event.label,
|
|
55
|
+
color: flags.color !== false ? "cyan" : false
|
|
56
|
+
}).start();
|
|
57
|
+
activeSpans.set(event.spanId, {
|
|
58
|
+
spinner,
|
|
59
|
+
startTime: Date.now()
|
|
60
|
+
});
|
|
61
|
+
} else if (event.kind === "spanEnd") {
|
|
62
|
+
const spanState = activeSpans.get(event.spanId);
|
|
63
|
+
if (spanState) {
|
|
64
|
+
const elapsed = Date.now() - spanState.startTime;
|
|
65
|
+
if (event.outcome === "error") {
|
|
66
|
+
spanState.spinner.fail(`${spanState.spinner.text} (failed)`);
|
|
67
|
+
} else if (event.outcome === "skipped") {
|
|
68
|
+
spanState.spinner.info(`${spanState.spinner.text} (skipped)`);
|
|
69
|
+
} else {
|
|
70
|
+
spanState.spinner.succeed(`${spanState.spinner.text} (${elapsed}ms)`);
|
|
71
|
+
}
|
|
72
|
+
activeSpans.delete(event.spanId);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/commands/db-init.ts
|
|
79
|
+
function mapDbInitFailure(failure) {
|
|
80
|
+
if (failure.code === "PLANNING_FAILED") {
|
|
81
|
+
return errorMigrationPlanningFailed({ conflicts: failure.conflicts ?? [] });
|
|
82
|
+
}
|
|
83
|
+
if (failure.code === "MARKER_ORIGIN_MISMATCH") {
|
|
84
|
+
const mismatchParts = [];
|
|
85
|
+
if (failure.marker?.coreHash !== failure.destination?.coreHash && failure.marker?.coreHash && failure.destination?.coreHash) {
|
|
86
|
+
mismatchParts.push(
|
|
87
|
+
`coreHash (marker: ${failure.marker.coreHash}, destination: ${failure.destination.coreHash})`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (failure.marker?.profileHash !== failure.destination?.profileHash && failure.marker?.profileHash && failure.destination?.profileHash) {
|
|
91
|
+
mismatchParts.push(
|
|
92
|
+
`profileHash (marker: ${failure.marker.profileHash}, destination: ${failure.destination.profileHash})`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return errorRuntime(
|
|
96
|
+
`Existing contract marker does not match plan destination.${mismatchParts.length > 0 ? ` Mismatch in ${mismatchParts.join(" and ")}.` : ""}`,
|
|
97
|
+
{
|
|
98
|
+
why: "Database has an existing contract marker that does not match the target contract",
|
|
99
|
+
fix: "If bootstrapping, drop/reset the database then re-run `prisma-next db init`; otherwise reconcile schema/marker using your migration workflow",
|
|
100
|
+
meta: {
|
|
101
|
+
code: "MARKER_ORIGIN_MISMATCH",
|
|
102
|
+
...failure.marker?.coreHash ? { markerCoreHash: failure.marker.coreHash } : {},
|
|
103
|
+
...failure.destination?.coreHash ? { destinationCoreHash: failure.destination.coreHash } : {},
|
|
104
|
+
...failure.marker?.profileHash ? { markerProfileHash: failure.marker.profileHash } : {},
|
|
105
|
+
...failure.destination?.profileHash ? { destinationProfileHash: failure.destination.profileHash } : {}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
if (failure.code === "RUNNER_FAILED") {
|
|
111
|
+
return errorRuntime(failure.summary, {
|
|
112
|
+
why: failure.why ?? "Migration runner failed",
|
|
113
|
+
fix: "Fix the schema mismatch (db init is additive-only), or drop/reset the database and re-run `prisma-next db init`",
|
|
114
|
+
meta: {
|
|
115
|
+
code: "RUNNER_FAILED",
|
|
116
|
+
...failure.meta ?? {}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const exhaustive = failure.code;
|
|
121
|
+
throw new Error(`Unhandled DbInitFailure code: ${exhaustive}`);
|
|
122
|
+
}
|
|
123
|
+
async function executeDbInitCommand(options, flags, startTime) {
|
|
124
|
+
const config = await loadConfig(options.config);
|
|
125
|
+
const configPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
|
|
126
|
+
const contractPathAbsolute = config.contract?.output ? resolve(config.contract.output) : resolve("src/prisma/contract.json");
|
|
127
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
128
|
+
if (flags.json !== "object" && !flags.quiet) {
|
|
129
|
+
const details = [
|
|
130
|
+
{ label: "config", value: configPath },
|
|
131
|
+
{ label: "contract", value: contractPath }
|
|
132
|
+
];
|
|
133
|
+
if (options.db) {
|
|
134
|
+
details.push({ label: "database", value: options.db });
|
|
135
|
+
}
|
|
136
|
+
if (options.plan) {
|
|
137
|
+
details.push({ label: "mode", value: "plan (dry run)" });
|
|
138
|
+
}
|
|
139
|
+
const header = formatStyledHeader({
|
|
140
|
+
command: "db init",
|
|
141
|
+
description: "Bootstrap a database to match the current contract",
|
|
142
|
+
url: "https://pris.ly/db-init",
|
|
143
|
+
details,
|
|
144
|
+
flags
|
|
145
|
+
});
|
|
146
|
+
console.log(header);
|
|
147
|
+
}
|
|
148
|
+
let contractJsonContent;
|
|
149
|
+
try {
|
|
150
|
+
contractJsonContent = await readFile(contractPathAbsolute, "utf-8");
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (error instanceof Error && error.code === "ENOENT") {
|
|
153
|
+
return notOk(
|
|
154
|
+
errorFileNotFound(contractPathAbsolute, {
|
|
155
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
156
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`
|
|
157
|
+
})
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return notOk(
|
|
161
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
162
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`
|
|
163
|
+
})
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
let contractJson;
|
|
167
|
+
try {
|
|
168
|
+
contractJson = JSON.parse(contractJsonContent);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
return notOk(
|
|
171
|
+
errorContractValidationFailed(
|
|
172
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
173
|
+
{ where: { path: contractPathAbsolute } }
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
178
|
+
if (!dbConnection) {
|
|
179
|
+
return notOk(
|
|
180
|
+
errorDatabaseConnectionRequired({
|
|
181
|
+
why: `Database connection is required for db init (set db.connection in ${configPath}, or pass --db <url>)`
|
|
182
|
+
})
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (!config.driver) {
|
|
186
|
+
return notOk(errorDriverRequired({ why: "Config.driver is required for db init" }));
|
|
187
|
+
}
|
|
188
|
+
if (!config.target.migrations) {
|
|
189
|
+
return notOk(
|
|
190
|
+
errorTargetMigrationNotSupported({
|
|
191
|
+
why: `Target "${config.target.id}" does not support migrations`
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
}
|
|
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
|
+
const onProgress = createProgressAdapter({ flags });
|
|
203
|
+
try {
|
|
204
|
+
const result = await client.dbInit({
|
|
205
|
+
contractIR: contractJson,
|
|
206
|
+
mode: options.plan ? "plan" : "apply",
|
|
207
|
+
connection: dbConnection,
|
|
208
|
+
onProgress
|
|
209
|
+
});
|
|
210
|
+
if (!result.ok) {
|
|
211
|
+
return notOk(mapDbInitFailure(result.failure));
|
|
212
|
+
}
|
|
213
|
+
const profileHash = result.value.marker?.profileHash;
|
|
214
|
+
const dbInitResult = {
|
|
215
|
+
ok: true,
|
|
216
|
+
mode: result.value.mode,
|
|
217
|
+
plan: {
|
|
218
|
+
targetId: config.target.targetId,
|
|
219
|
+
destination: {
|
|
220
|
+
coreHash: result.value.marker?.coreHash ?? "",
|
|
221
|
+
...profileHash ? { profileHash } : {}
|
|
222
|
+
},
|
|
223
|
+
operations: result.value.plan.operations.map((op) => ({
|
|
224
|
+
id: op.id,
|
|
225
|
+
label: op.label,
|
|
226
|
+
operationClass: op.operationClass
|
|
227
|
+
}))
|
|
228
|
+
},
|
|
229
|
+
...result.value.execution ? {
|
|
230
|
+
execution: {
|
|
231
|
+
operationsPlanned: result.value.execution.operationsPlanned,
|
|
232
|
+
operationsExecuted: result.value.execution.operationsExecuted
|
|
233
|
+
}
|
|
234
|
+
} : {},
|
|
235
|
+
...result.value.marker ? {
|
|
236
|
+
marker: {
|
|
237
|
+
coreHash: result.value.marker.coreHash,
|
|
238
|
+
...result.value.marker.profileHash ? { profileHash: result.value.marker.profileHash } : {}
|
|
239
|
+
}
|
|
240
|
+
} : {},
|
|
241
|
+
summary: result.value.summary,
|
|
242
|
+
timings: { total: Date.now() - startTime }
|
|
243
|
+
};
|
|
244
|
+
return ok(dbInitResult);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (CliStructuredError.is(error)) {
|
|
247
|
+
return notOk(error);
|
|
248
|
+
}
|
|
249
|
+
return notOk(
|
|
250
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
251
|
+
why: `Unexpected error during db init: ${error instanceof Error ? error.message : String(error)}`
|
|
252
|
+
})
|
|
253
|
+
);
|
|
254
|
+
} finally {
|
|
255
|
+
await client.close();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
38
258
|
function createDbInitCommand() {
|
|
39
259
|
const command = new Command("init");
|
|
40
260
|
setCommandDescriptions(
|
|
@@ -50,275 +270,18 @@ function createDbInitCommand() {
|
|
|
50
270
|
}).option("--db <url>", "Database connection string").option("--config <path>", "Path to prisma-next.config.ts").option("--plan", "Preview planned operations without applying", false).option("--json [format]", "Output as JSON (object)", false).option("-q, --quiet", "Quiet mode: errors only").option("-v, --verbose", "Verbose output: debug info, timings").option("-vv, --trace", "Trace output: deep internals, stack traces").option("--timestamps", "Add timestamps to output").option("--color", "Force color output").option("--no-color", "Disable color output").action(async (options) => {
|
|
51
271
|
const flags = parseGlobalFlags(options);
|
|
52
272
|
const startTime = Date.now();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
273
|
+
if (flags.json === "ndjson") {
|
|
274
|
+
const result2 = notOk(
|
|
275
|
+
errorJsonFormatNotSupported({
|
|
56
276
|
command: "db init",
|
|
57
277
|
format: "ndjson",
|
|
58
278
|
supportedFormats: ["object"]
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (flags.json !== "object" && !flags.quiet) {
|
|
66
|
-
const details = [
|
|
67
|
-
{ label: "config", value: configPath },
|
|
68
|
-
{ label: "contract", value: contractPath }
|
|
69
|
-
];
|
|
70
|
-
if (options.db) {
|
|
71
|
-
details.push({ label: "database", value: options.db });
|
|
72
|
-
}
|
|
73
|
-
if (options.plan) {
|
|
74
|
-
details.push({ label: "mode", value: "plan (dry run)" });
|
|
75
|
-
}
|
|
76
|
-
const header = formatStyledHeader({
|
|
77
|
-
command: "db init",
|
|
78
|
-
description: "Bootstrap a database to match the current contract",
|
|
79
|
-
url: "https://pris.ly/db-init",
|
|
80
|
-
details,
|
|
81
|
-
flags
|
|
82
|
-
});
|
|
83
|
-
console.log(header);
|
|
84
|
-
}
|
|
85
|
-
let contractJsonContent;
|
|
86
|
-
try {
|
|
87
|
-
contractJsonContent = await readFile(contractPathAbsolute, "utf-8");
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if (error instanceof Error && error.code === "ENOENT") {
|
|
90
|
-
throw errorFileNotFound(contractPathAbsolute, {
|
|
91
|
-
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
92
|
-
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
throw errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
96
|
-
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
let contractJson;
|
|
100
|
-
try {
|
|
101
|
-
contractJson = JSON.parse(contractJsonContent);
|
|
102
|
-
} catch (error) {
|
|
103
|
-
throw errorContractValidationFailed(
|
|
104
|
-
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
105
|
-
{ where: { path: contractPathAbsolute } }
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
const dbConnection = options.db ?? config.db?.connection;
|
|
109
|
-
if (!dbConnection) {
|
|
110
|
-
throw errorDatabaseConnectionRequired({
|
|
111
|
-
why: `Database connection is required for db init (set db.connection in ${configPath}, or pass --db <url>)`
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
if (!config.driver) {
|
|
115
|
-
throw errorDriverRequired({ why: "Config.driver is required for db init" });
|
|
116
|
-
}
|
|
117
|
-
const driverDescriptor = config.driver;
|
|
118
|
-
if (!config.target.migrations) {
|
|
119
|
-
throw errorTargetMigrationNotSupported({
|
|
120
|
-
why: `Target "${config.target.id}" does not support migrations`
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
const migrations = config.target.migrations;
|
|
124
|
-
let driver;
|
|
125
|
-
try {
|
|
126
|
-
driver = await withSpinner(() => driverDescriptor.create(dbConnection), {
|
|
127
|
-
message: "Connecting to database...",
|
|
128
|
-
flags
|
|
129
|
-
});
|
|
130
|
-
} catch (error) {
|
|
131
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
132
|
-
const code = error.code;
|
|
133
|
-
const redacted = typeof dbConnection === "string" ? redactDatabaseUrl(dbConnection) : void 0;
|
|
134
|
-
throw errorRuntime("Database connection failed", {
|
|
135
|
-
why: message,
|
|
136
|
-
fix: "Verify the database connection, ensure the database is reachable, and confirm credentials/permissions",
|
|
137
|
-
meta: {
|
|
138
|
-
...typeof code !== "undefined" ? { code } : {},
|
|
139
|
-
...redacted ?? {}
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
try {
|
|
144
|
-
const stack = createControlPlaneStack({
|
|
145
|
-
target: config.target,
|
|
146
|
-
adapter: config.adapter,
|
|
147
|
-
driver: driverDescriptor,
|
|
148
|
-
extensionPacks: config.extensionPacks
|
|
149
|
-
});
|
|
150
|
-
const familyInstance = config.family.create(stack);
|
|
151
|
-
const rawComponents = [config.target, config.adapter, ...config.extensionPacks ?? []];
|
|
152
|
-
const frameworkComponents = assertFrameworkComponentsCompatible(
|
|
153
|
-
config.family.familyId,
|
|
154
|
-
config.target.targetId,
|
|
155
|
-
rawComponents
|
|
156
|
-
);
|
|
157
|
-
const contractIR = familyInstance.validateContractIR(contractJson);
|
|
158
|
-
assertContractRequirementsSatisfied({ contract: contractIR, stack });
|
|
159
|
-
const planner = migrations.createPlanner(familyInstance);
|
|
160
|
-
const runner = migrations.createRunner(familyInstance);
|
|
161
|
-
const schemaIR = await withSpinner(() => familyInstance.introspect({ driver }), {
|
|
162
|
-
message: "Introspecting database schema...",
|
|
163
|
-
flags
|
|
164
|
-
});
|
|
165
|
-
const policy = { allowedOperationClasses: ["additive"] };
|
|
166
|
-
const plannerResult = await withSpinner(
|
|
167
|
-
async () => planner.plan({
|
|
168
|
-
contract: contractIR,
|
|
169
|
-
schema: schemaIR,
|
|
170
|
-
policy,
|
|
171
|
-
frameworkComponents
|
|
172
|
-
}),
|
|
173
|
-
{
|
|
174
|
-
message: "Planning migration...",
|
|
175
|
-
flags
|
|
176
|
-
}
|
|
177
|
-
);
|
|
178
|
-
if (plannerResult.kind === "failure") {
|
|
179
|
-
throw errorMigrationPlanningFailed({ conflicts: plannerResult.conflicts });
|
|
180
|
-
}
|
|
181
|
-
const migrationPlan = plannerResult.plan;
|
|
182
|
-
const existingMarker = await familyInstance.readMarker({ driver });
|
|
183
|
-
if (existingMarker) {
|
|
184
|
-
const markerMatchesDestination = existingMarker.coreHash === migrationPlan.destination.coreHash && (!migrationPlan.destination.profileHash || existingMarker.profileHash === migrationPlan.destination.profileHash);
|
|
185
|
-
if (markerMatchesDestination) {
|
|
186
|
-
const dbInitResult2 = {
|
|
187
|
-
ok: true,
|
|
188
|
-
mode: options.plan ? "plan" : "apply",
|
|
189
|
-
plan: {
|
|
190
|
-
targetId: migrationPlan.targetId,
|
|
191
|
-
destination: migrationPlan.destination,
|
|
192
|
-
operations: []
|
|
193
|
-
},
|
|
194
|
-
...options.plan ? {} : {
|
|
195
|
-
execution: { operationsPlanned: 0, operationsExecuted: 0 },
|
|
196
|
-
marker: {
|
|
197
|
-
coreHash: existingMarker.coreHash,
|
|
198
|
-
profileHash: existingMarker.profileHash
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
summary: "Database already at target contract state",
|
|
202
|
-
timings: { total: Date.now() - startTime }
|
|
203
|
-
};
|
|
204
|
-
return dbInitResult2;
|
|
205
|
-
}
|
|
206
|
-
const coreHashMismatch = existingMarker.coreHash !== migrationPlan.destination.coreHash;
|
|
207
|
-
const profileHashMismatch = migrationPlan.destination.profileHash && existingMarker.profileHash !== migrationPlan.destination.profileHash;
|
|
208
|
-
const mismatchParts = [];
|
|
209
|
-
if (coreHashMismatch) {
|
|
210
|
-
mismatchParts.push(
|
|
211
|
-
`coreHash (marker: ${existingMarker.coreHash}, destination: ${migrationPlan.destination.coreHash})`
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
if (profileHashMismatch) {
|
|
215
|
-
mismatchParts.push(
|
|
216
|
-
`profileHash (marker: ${existingMarker.profileHash}, destination: ${migrationPlan.destination.profileHash})`
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
throw errorRuntime(
|
|
220
|
-
`Existing contract marker does not match plan destination. Mismatch in ${mismatchParts.join(" and ")}.`,
|
|
221
|
-
{
|
|
222
|
-
why: "Database has an existing contract marker that does not match the target contract",
|
|
223
|
-
fix: "If bootstrapping, drop/reset the database then re-run `prisma-next db init`; otherwise reconcile schema/marker using your migration workflow",
|
|
224
|
-
meta: {
|
|
225
|
-
code: "MARKER_ORIGIN_MISMATCH",
|
|
226
|
-
markerCoreHash: existingMarker.coreHash,
|
|
227
|
-
destinationCoreHash: migrationPlan.destination.coreHash,
|
|
228
|
-
...existingMarker.profileHash ? { markerProfileHash: existingMarker.profileHash } : {},
|
|
229
|
-
...migrationPlan.destination.profileHash ? { destinationProfileHash: migrationPlan.destination.profileHash } : {}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
if (options.plan) {
|
|
235
|
-
const dbInitResult2 = {
|
|
236
|
-
ok: true,
|
|
237
|
-
mode: "plan",
|
|
238
|
-
plan: {
|
|
239
|
-
targetId: migrationPlan.targetId,
|
|
240
|
-
destination: migrationPlan.destination,
|
|
241
|
-
operations: migrationPlan.operations.map((op) => ({
|
|
242
|
-
id: op.id,
|
|
243
|
-
label: op.label,
|
|
244
|
-
operationClass: op.operationClass
|
|
245
|
-
}))
|
|
246
|
-
},
|
|
247
|
-
summary: `Planned ${migrationPlan.operations.length} operation(s)`,
|
|
248
|
-
timings: { total: Date.now() - startTime }
|
|
249
|
-
};
|
|
250
|
-
return dbInitResult2;
|
|
251
|
-
}
|
|
252
|
-
if (!flags.quiet && flags.json !== "object") {
|
|
253
|
-
console.log("Applying migration plan and verifying schema...");
|
|
254
|
-
}
|
|
255
|
-
const callbacks = {
|
|
256
|
-
onOperationStart: (op) => {
|
|
257
|
-
if (!flags.quiet && flags.json !== "object") {
|
|
258
|
-
console.log(` \u2192 ${op.label}...`);
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
onOperationComplete: (_op) => {
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
const runnerResult = await runner.execute({
|
|
265
|
-
plan: migrationPlan,
|
|
266
|
-
driver,
|
|
267
|
-
destinationContract: contractIR,
|
|
268
|
-
policy,
|
|
269
|
-
callbacks,
|
|
270
|
-
// db init plans and applies back-to-back from a fresh introspection, so per-operation
|
|
271
|
-
// pre/postchecks and the idempotency probe are usually redundant overhead. We still
|
|
272
|
-
// enforce marker/origin compatibility and a full schema verification after apply.
|
|
273
|
-
executionChecks: {
|
|
274
|
-
prechecks: false,
|
|
275
|
-
postchecks: false,
|
|
276
|
-
idempotencyChecks: false
|
|
277
|
-
},
|
|
278
|
-
frameworkComponents
|
|
279
|
-
});
|
|
280
|
-
if (!runnerResult.ok) {
|
|
281
|
-
const meta = {
|
|
282
|
-
code: runnerResult.failure.code,
|
|
283
|
-
...runnerResult.failure.meta ?? {}
|
|
284
|
-
};
|
|
285
|
-
const sqlState = typeof meta["sqlState"] === "string" ? meta["sqlState"] : void 0;
|
|
286
|
-
const fix = sqlState === "42501" ? "Grant the database user sufficient privileges (insufficient_privilege), or run db init as a more privileged role" : runnerResult.failure.code === "SCHEMA_VERIFY_FAILED" ? "Fix the schema mismatch (db init is additive-only), or drop/reset the database and re-run `prisma-next db init`" : void 0;
|
|
287
|
-
throw errorRuntime(runnerResult.failure.summary, {
|
|
288
|
-
why: runnerResult.failure.why ?? `Migration runner failed: ${runnerResult.failure.code}`,
|
|
289
|
-
...fix ? { fix } : {},
|
|
290
|
-
meta
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
const execution = runnerResult.value;
|
|
294
|
-
const dbInitResult = {
|
|
295
|
-
ok: true,
|
|
296
|
-
mode: "apply",
|
|
297
|
-
plan: {
|
|
298
|
-
targetId: migrationPlan.targetId,
|
|
299
|
-
destination: migrationPlan.destination,
|
|
300
|
-
operations: migrationPlan.operations.map((op) => ({
|
|
301
|
-
id: op.id,
|
|
302
|
-
label: op.label,
|
|
303
|
-
operationClass: op.operationClass
|
|
304
|
-
}))
|
|
305
|
-
},
|
|
306
|
-
execution: {
|
|
307
|
-
operationsPlanned: execution.operationsPlanned,
|
|
308
|
-
operationsExecuted: execution.operationsExecuted
|
|
309
|
-
},
|
|
310
|
-
marker: migrationPlan.destination.profileHash ? {
|
|
311
|
-
coreHash: migrationPlan.destination.coreHash,
|
|
312
|
-
profileHash: migrationPlan.destination.profileHash
|
|
313
|
-
} : { coreHash: migrationPlan.destination.coreHash },
|
|
314
|
-
summary: `Applied ${execution.operationsExecuted} operation(s), marker written`,
|
|
315
|
-
timings: { total: Date.now() - startTime }
|
|
316
|
-
};
|
|
317
|
-
return dbInitResult;
|
|
318
|
-
} finally {
|
|
319
|
-
await driver.close();
|
|
320
|
-
}
|
|
321
|
-
});
|
|
279
|
+
})
|
|
280
|
+
);
|
|
281
|
+
const exitCode2 = handleResult(result2, flags);
|
|
282
|
+
process.exit(exitCode2);
|
|
283
|
+
}
|
|
284
|
+
const result = await executeDbInitCommand(options, flags, startTime);
|
|
322
285
|
const exitCode = handleResult(result, flags, (dbInitResult) => {
|
|
323
286
|
if (flags.json === "object") {
|
|
324
287
|
console.log(formatDbInitJson(dbInitResult));
|