@prisma/cli 3.0.0-beta.3 → 3.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/commands/app/index.js +22 -10
- package/dist/commands/branch/index.js +2 -27
- package/dist/commands/env.js +8 -4
- package/dist/controllers/app-env-api.js +54 -0
- package/dist/controllers/app-env-file.js +179 -0
- package/dist/controllers/app-env.js +200 -86
- package/dist/controllers/app.js +51 -53
- package/dist/controllers/branch.js +73 -47
- package/dist/lib/app/branch-database-deploy.js +323 -0
- package/dist/lib/app/branch-database.js +316 -0
- package/dist/lib/app/env-config.js +1 -1
- package/dist/lib/app/env-file.js +82 -0
- package/dist/lib/app/preview-branch-database.js +102 -0
- package/dist/lib/app/preview-provider.js +45 -6
- package/dist/lib/app/production-deploy-gate.js +160 -0
- package/dist/lib/git/local-branch.js +41 -0
- package/dist/lib/project/resolution.js +49 -4
- package/dist/presenters/app-env.js +44 -3
- package/dist/presenters/app.js +25 -0
- package/dist/presenters/branch.js +29 -101
- package/dist/presenters/project.js +15 -16
- package/dist/shell/command-meta.js +12 -24
- package/dist/shell/ui.js +4 -1
- package/dist/use-cases/branch.js +20 -68
- package/dist/use-cases/create-cli-gateways.js +2 -17
- package/package.json +2 -1
|
@@ -1,73 +1,99 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CliError, authRequiredError, workspaceRequiredError } from "../shell/errors.js";
|
|
2
|
+
import { requireComputeAuth } from "../lib/auth/guard.js";
|
|
3
|
+
import { resolveProjectTarget } from "../lib/project/resolution.js";
|
|
3
4
|
import { createCliUseCaseGateways } from "../use-cases/create-cli-gateways.js";
|
|
4
5
|
import { createSelectPromptPort } from "./select-prompt-port.js";
|
|
6
|
+
import { requireAuthenticatedAuthState } from "./auth.js";
|
|
7
|
+
import { listRealWorkspaceProjects } from "./project.js";
|
|
5
8
|
import { createBranchUseCases } from "../use-cases/branch.js";
|
|
6
9
|
//#region src/controllers/branch.ts
|
|
7
|
-
const PREVIEW_BRANCH_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
8
10
|
function isRealMode(context) {
|
|
9
11
|
return !context.runtime.fixturePath && !context.runtime.env.PRISMA_CLI_MOCK_FIXTURE_PATH;
|
|
10
12
|
}
|
|
11
13
|
async function runBranchList(context) {
|
|
12
|
-
if (isRealMode(context))
|
|
13
|
-
return {
|
|
14
|
+
if (isRealMode(context)) return {
|
|
14
15
|
command: "branch.list",
|
|
15
|
-
result: await
|
|
16
|
+
result: await listRealBranches(context),
|
|
16
17
|
warnings: [],
|
|
17
18
|
nextSteps: []
|
|
18
19
|
};
|
|
19
|
-
}
|
|
20
|
-
async function runBranchShow(context) {
|
|
21
|
-
if (isRealMode(context)) throw branchCommandsUnavailableError();
|
|
22
|
-
const result = await createBranchUseCases(createCliUseCaseGateways(context)).show();
|
|
23
20
|
return {
|
|
24
|
-
command: "branch.
|
|
25
|
-
result,
|
|
21
|
+
command: "branch.list",
|
|
22
|
+
result: await createBranchUseCases(createCliUseCaseGateways(context)).list(),
|
|
26
23
|
warnings: [],
|
|
27
|
-
nextSteps:
|
|
24
|
+
nextSteps: []
|
|
28
25
|
};
|
|
29
26
|
}
|
|
30
|
-
async function
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
async function listRealBranches(context) {
|
|
28
|
+
const authState = await requireAuthenticatedAuthState(context);
|
|
29
|
+
const client = await requireComputeAuth(context.runtime.env, context.runtime.signal);
|
|
30
|
+
if (!client) throw authRequiredError(["prisma-cli auth login"]);
|
|
31
|
+
const workspace = authState.workspace;
|
|
32
|
+
if (!workspace) throw workspaceRequiredError();
|
|
33
|
+
const target = await resolveProjectTarget({
|
|
34
|
+
context,
|
|
35
|
+
workspace,
|
|
36
|
+
listProjects: () => listRealWorkspaceProjects(client, workspace, context.runtime.signal),
|
|
37
|
+
prompt: createSelectPromptPort(context),
|
|
38
|
+
remember: true
|
|
39
|
+
});
|
|
40
|
+
const branches = await listBranches(client, target.project.id, context.runtime.signal);
|
|
36
41
|
return {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
nextSteps: result.branch.kind === "preview" && !result.branch.remoteState ? ["prisma-cli branch show", "prisma-cli app deploy"] : ["prisma-cli branch show"]
|
|
42
|
+
projectId: target.project.id,
|
|
43
|
+
projectName: target.project.name,
|
|
44
|
+
branches: sortBranches(branches.map(toBranchSummary))
|
|
41
45
|
};
|
|
42
46
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
choices: result.branches.map((branch) => ({
|
|
50
|
-
label: renderBranchChoiceLabel(branch),
|
|
51
|
-
value: branch.name
|
|
52
|
-
}))
|
|
47
|
+
function sortBranches(branches) {
|
|
48
|
+
return branches.slice().sort((left, right) => {
|
|
49
|
+
const leftRank = branchOrder(left);
|
|
50
|
+
const rightRank = branchOrder(right);
|
|
51
|
+
if (leftRank !== rightRank) return leftRank - rightRank;
|
|
52
|
+
return left.name.localeCompare(right.name);
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
|
-
function
|
|
56
|
-
|
|
57
|
-
if (branch.active) markers.push("active");
|
|
58
|
-
if (!branch.remoteState) markers.push("not created yet");
|
|
59
|
-
return markers.length > 0 ? `${branch.name} (${markers.join(", ")})` : branch.name;
|
|
55
|
+
function branchOrder(branch) {
|
|
56
|
+
return branch.role === "production" ? 0 : 1;
|
|
60
57
|
}
|
|
61
|
-
function
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
async function listBranches(client, projectId, signal) {
|
|
59
|
+
const collected = [];
|
|
60
|
+
let cursor;
|
|
61
|
+
while (true) {
|
|
62
|
+
const query = {};
|
|
63
|
+
if (cursor !== void 0) query.cursor = cursor;
|
|
64
|
+
const { data, error, response } = await client.GET("/v1/projects/{projectId}/branches", {
|
|
65
|
+
params: {
|
|
66
|
+
path: { projectId },
|
|
67
|
+
query
|
|
68
|
+
},
|
|
69
|
+
signal
|
|
70
|
+
});
|
|
71
|
+
if (error || !data) throw branchApiError("Failed to list branches", response, error);
|
|
72
|
+
collected.push(...data.data);
|
|
73
|
+
if (!data.pagination.hasMore || !data.pagination.nextCursor) break;
|
|
74
|
+
cursor = data.pagination.nextCursor;
|
|
75
|
+
}
|
|
76
|
+
return collected;
|
|
65
77
|
}
|
|
66
|
-
function
|
|
67
|
-
return
|
|
78
|
+
function toBranchSummary(branch) {
|
|
79
|
+
return {
|
|
80
|
+
id: branch.id,
|
|
81
|
+
name: branch.gitName,
|
|
82
|
+
role: branch.role,
|
|
83
|
+
envMap: branch.role
|
|
84
|
+
};
|
|
68
85
|
}
|
|
69
|
-
function
|
|
70
|
-
|
|
86
|
+
function branchApiError(summary, response, error) {
|
|
87
|
+
const status = response?.status ?? 0;
|
|
88
|
+
return new CliError({
|
|
89
|
+
code: error?.error?.code ?? "BRANCH_API_ERROR",
|
|
90
|
+
domain: "branch",
|
|
91
|
+
summary,
|
|
92
|
+
why: error?.error?.message ?? `The Management API returned status ${status || "unknown"}.`,
|
|
93
|
+
fix: error?.error?.hint ?? "Re-run with --trace for the underlying API response details.",
|
|
94
|
+
exitCode: 1,
|
|
95
|
+
nextSteps: []
|
|
96
|
+
});
|
|
71
97
|
}
|
|
72
98
|
//#endregion
|
|
73
|
-
export { runBranchList
|
|
99
|
+
export { runBranchList };
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { CliError, usageError } from "../../shell/errors.js";
|
|
2
|
+
import { renderSummaryLine } from "../../shell/ui.js";
|
|
3
|
+
import { canPrompt } from "../../shell/runtime.js";
|
|
4
|
+
import { confirmPrompt } from "../../shell/prompt.js";
|
|
5
|
+
import { formatCommandArgument } from "../../shell/command-arguments.js";
|
|
6
|
+
import { hasBranchDatabaseSignal, inspectBranchDatabaseSignal, runBranchDatabaseSchemaSetup } from "./branch-database.js";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
//#region src/lib/app/branch-database-deploy.ts
|
|
9
|
+
async function maybeSetupBranchDatabase(context, provider, projectId, branch, options) {
|
|
10
|
+
if (options.db === false) return emptyBranchDatabaseSetupOutcome();
|
|
11
|
+
if (hasInlineDatabaseEnvVars(options.inlineEnvVars)) {
|
|
12
|
+
if (options.db === true) throw usageError("Branch database setup cannot be combined with inline database env vars", "The deploy command received --db and an inline DATABASE_URL or DIRECT_URL value.", "Remove the inline --env database value to let --db create a branch override, or remove --db to deploy with the provided value.", ["prisma-cli app deploy --db", "prisma-cli app deploy --env DATABASE_URL=postgresql://example"], "app");
|
|
13
|
+
return emptyBranchDatabaseSetupOutcome();
|
|
14
|
+
}
|
|
15
|
+
if (branch.kind === "production") {
|
|
16
|
+
if (options.db === true) throw usageError("Branch database setup is only available for preview branches", "Production database wiring is a durable environment decision and is not created implicitly by app deploy.", "Use project env commands to manage production DATABASE_URL, or deploy a preview branch with --db.", ["prisma-cli project env add DATABASE_URL=<value> --role production", "prisma-cli app deploy --branch feature/db --db"], "app");
|
|
17
|
+
return emptyBranchDatabaseSetupOutcome();
|
|
18
|
+
}
|
|
19
|
+
const localSignal = await inspectBranchDatabaseSignal(context.runtime.cwd, context.runtime.signal);
|
|
20
|
+
const envState = await inspectBranchDatabaseEnv(provider, projectId, branch.id, context.runtime.signal);
|
|
21
|
+
const branchEnvVars = [envState.branchDatabaseUrl, envState.branchDirectUrl].filter((variable) => Boolean(variable)).map((variable) => variable.key).sort();
|
|
22
|
+
if (envState.branchDatabaseUrl) {
|
|
23
|
+
const warning = options.db === true ? `Branch "${branch.name}" already has DATABASE_URL. Leaving branch database env vars unchanged.` : null;
|
|
24
|
+
if (warning) emitBranchDatabaseWarning(context, warning);
|
|
25
|
+
return {
|
|
26
|
+
result: options.db === true ? {
|
|
27
|
+
status: "skipped",
|
|
28
|
+
reason: "branch-env-exists",
|
|
29
|
+
envVars: branchEnvVars,
|
|
30
|
+
schema: null
|
|
31
|
+
} : void 0,
|
|
32
|
+
warnings: warning ? [warning] : []
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (localSignal.unsupportedSchema) {
|
|
36
|
+
if (options.db === true) throw unsupportedBranchDatabaseSchemaError(localSignal.unsupportedSchema, branch.name, context);
|
|
37
|
+
return emptyBranchDatabaseSetupOutcome();
|
|
38
|
+
}
|
|
39
|
+
const hasSignal = hasBranchDatabaseSignal(localSignal) || Boolean(envState.previewDatabaseUrl);
|
|
40
|
+
if (options.db !== true) {
|
|
41
|
+
if (!hasSignal) return emptyBranchDatabaseSetupOutcome();
|
|
42
|
+
if (!canPrompt(context) || context.flags.yes) {
|
|
43
|
+
const warning = "This app appears to use DATABASE_URL. Run prisma-cli app deploy --db to create an isolated database for this preview branch.";
|
|
44
|
+
emitBranchDatabaseWarning(context, warning);
|
|
45
|
+
return {
|
|
46
|
+
result: void 0,
|
|
47
|
+
warnings: [warning]
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
maybeRenderBranchDatabaseSignal(context, branch.name, localSignal, envState);
|
|
51
|
+
if (!await confirmPrompt({
|
|
52
|
+
input: context.runtime.stdin,
|
|
53
|
+
output: context.output.stderr,
|
|
54
|
+
message: `Create an isolated database for branch "${branch.name}"?`,
|
|
55
|
+
initialValue: false
|
|
56
|
+
})) return emptyBranchDatabaseSetupOutcome();
|
|
57
|
+
}
|
|
58
|
+
return setupBranchDatabase(context, provider, projectId, branch, localSignal, envState);
|
|
59
|
+
}
|
|
60
|
+
async function setupBranchDatabase(context, provider, projectId, branch, signal, envState) {
|
|
61
|
+
emitBranchDatabaseProgress(context, "pending", "Creating branch database");
|
|
62
|
+
const database = await provider.createBranchDatabase({
|
|
63
|
+
projectId,
|
|
64
|
+
branchId: branch.id,
|
|
65
|
+
branchName: branch.name,
|
|
66
|
+
signal: context.runtime.signal
|
|
67
|
+
}).catch((error) => {
|
|
68
|
+
throw branchDatabaseSetupFailedError("Failed to create branch database", error, branch.name);
|
|
69
|
+
});
|
|
70
|
+
emitBranchDatabaseProgress(context, "success", "Created branch database");
|
|
71
|
+
try {
|
|
72
|
+
let schemaSetup = null;
|
|
73
|
+
const warnings = [];
|
|
74
|
+
let skippedSchemaWarning = null;
|
|
75
|
+
if (signal.schema) {
|
|
76
|
+
emitBranchDatabaseProgress(context, "pending", `Applying database schema with ${formatSchemaSetupCommand(signal.schema.command)}`);
|
|
77
|
+
schemaSetup = await runBranchDatabaseSchemaSetup({
|
|
78
|
+
context,
|
|
79
|
+
schema: signal.schema,
|
|
80
|
+
databaseUrl: database.databaseUrl,
|
|
81
|
+
directUrl: database.directUrl
|
|
82
|
+
}).catch((error) => {
|
|
83
|
+
throw schemaSetupFailedError(error, signal.schema, branch.name, context.runtime.cwd);
|
|
84
|
+
});
|
|
85
|
+
emitBranchDatabaseProgress(context, "success", "Applied database schema");
|
|
86
|
+
} else skippedSchemaWarning = "No supported Prisma schema source was found. Branch database env vars were created, but schema setup was skipped.";
|
|
87
|
+
const envVars = await upsertBranchDatabaseEnvVars(context, provider, projectId, branch, database, envState);
|
|
88
|
+
emitBranchDatabaseProgress(context, "success", `Added branch env override${envVars.length === 1 ? "" : "s"} ${envVars.join(", ")}`);
|
|
89
|
+
if (skippedSchemaWarning) {
|
|
90
|
+
emitBranchDatabaseWarning(context, skippedSchemaWarning);
|
|
91
|
+
warnings.push(skippedSchemaWarning);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
result: {
|
|
95
|
+
status: "created",
|
|
96
|
+
database: {
|
|
97
|
+
id: database.id,
|
|
98
|
+
name: database.name
|
|
99
|
+
},
|
|
100
|
+
envVars,
|
|
101
|
+
schema: schemaSetup ? {
|
|
102
|
+
command: schemaSetup.command,
|
|
103
|
+
source: schemaSetup.source,
|
|
104
|
+
path: schemaSetup.schemaPath
|
|
105
|
+
} : null
|
|
106
|
+
},
|
|
107
|
+
warnings
|
|
108
|
+
};
|
|
109
|
+
} catch (error) {
|
|
110
|
+
throw await cleanupCreatedBranchDatabaseAfterFailure(context, provider, database, branch.name, error);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function upsertBranchDatabaseEnvVars(context, provider, projectId, branch, database, envState) {
|
|
114
|
+
const written = [];
|
|
115
|
+
await upsertBranchDatabaseEnvVar(context, provider, {
|
|
116
|
+
projectId,
|
|
117
|
+
branchId: branch.id,
|
|
118
|
+
className: "preview",
|
|
119
|
+
key: "DATABASE_URL",
|
|
120
|
+
value: database.databaseUrl,
|
|
121
|
+
existing: envState.branchDatabaseUrl,
|
|
122
|
+
branchName: branch.name
|
|
123
|
+
});
|
|
124
|
+
written.push("DATABASE_URL");
|
|
125
|
+
if (database.directUrl) {
|
|
126
|
+
await upsertBranchDatabaseEnvVar(context, provider, {
|
|
127
|
+
projectId,
|
|
128
|
+
branchId: branch.id,
|
|
129
|
+
className: "preview",
|
|
130
|
+
key: "DIRECT_URL",
|
|
131
|
+
value: database.directUrl,
|
|
132
|
+
existing: envState.branchDirectUrl,
|
|
133
|
+
branchName: branch.name
|
|
134
|
+
});
|
|
135
|
+
written.push("DIRECT_URL");
|
|
136
|
+
} else if (envState.branchDirectUrl) await provider.deleteEnvironmentVariable({
|
|
137
|
+
envVarId: envState.branchDirectUrl.id,
|
|
138
|
+
signal: context.runtime.signal
|
|
139
|
+
}).catch((error) => {
|
|
140
|
+
throw branchDatabaseSetupFailedError("Failed to remove stale DIRECT_URL", error, branch.name);
|
|
141
|
+
});
|
|
142
|
+
return written;
|
|
143
|
+
}
|
|
144
|
+
async function upsertBranchDatabaseEnvVar(context, provider, options) {
|
|
145
|
+
if (options.existing) {
|
|
146
|
+
await provider.updateEnvironmentVariable({
|
|
147
|
+
envVarId: options.existing.id,
|
|
148
|
+
value: options.value,
|
|
149
|
+
signal: context.runtime.signal
|
|
150
|
+
}).catch((error) => {
|
|
151
|
+
throw branchDatabaseSetupFailedError(`Failed to update ${options.key}`, error, options.branchName);
|
|
152
|
+
});
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
await provider.createEnvironmentVariable({
|
|
156
|
+
projectId: options.projectId,
|
|
157
|
+
branchId: options.branchId,
|
|
158
|
+
className: options.className,
|
|
159
|
+
key: options.key,
|
|
160
|
+
value: options.value,
|
|
161
|
+
signal: context.runtime.signal
|
|
162
|
+
}).catch((error) => {
|
|
163
|
+
throw branchDatabaseSetupFailedError(`Failed to write ${options.key}`, error, options.branchName);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
async function inspectBranchDatabaseEnv(provider, projectId, branchId, signal) {
|
|
167
|
+
const [databaseUrlRows, directUrlRows] = await Promise.all([provider.listEnvironmentVariables({
|
|
168
|
+
projectId,
|
|
169
|
+
className: "preview",
|
|
170
|
+
key: "DATABASE_URL",
|
|
171
|
+
signal
|
|
172
|
+
}), provider.listEnvironmentVariables({
|
|
173
|
+
projectId,
|
|
174
|
+
className: "preview",
|
|
175
|
+
key: "DIRECT_URL",
|
|
176
|
+
signal
|
|
177
|
+
})]);
|
|
178
|
+
return {
|
|
179
|
+
branchDatabaseUrl: findEnvVar(databaseUrlRows, { branchId }),
|
|
180
|
+
branchDirectUrl: findEnvVar(directUrlRows, { branchId }),
|
|
181
|
+
previewDatabaseUrl: findEnvVar(databaseUrlRows, { branchId: null })
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function findEnvVar(rows, options) {
|
|
185
|
+
return rows.find((row) => row.branchId === options.branchId) ?? null;
|
|
186
|
+
}
|
|
187
|
+
function hasInlineDatabaseEnvVars(envVars) {
|
|
188
|
+
return Boolean(envVars && ("DATABASE_URL" in envVars || "DIRECT_URL" in envVars));
|
|
189
|
+
}
|
|
190
|
+
function maybeRenderBranchDatabaseSignal(context, branchName, signal, envState) {
|
|
191
|
+
if (context.flags.json || context.flags.quiet) return;
|
|
192
|
+
const rows = [
|
|
193
|
+
signal.schema ? ` Schema ${path.relative(context.runtime.cwd, signal.schema.path) || defaultSchemaSourcePath(signal.schema)}` : null,
|
|
194
|
+
signal.databaseUrlReferences.length > 0 ? ` Code ${signal.databaseUrlReferences.slice(0, 3).join(", ")}` : null,
|
|
195
|
+
envState.previewDatabaseUrl ? " Env preview DATABASE_URL is inherited by this branch" : null
|
|
196
|
+
].filter((row) => Boolean(row));
|
|
197
|
+
context.output.stderr.write(`Database signal found for branch "${branchName}"\n${rows.join("\n")}\n\n`);
|
|
198
|
+
}
|
|
199
|
+
function emitBranchDatabaseProgress(context, status, message) {
|
|
200
|
+
if (context.flags.json || context.flags.quiet) return;
|
|
201
|
+
const line = status === "pending" ? `${context.ui.warning("◇")} ${message}...` : renderSummaryLine(context.ui, "success", message);
|
|
202
|
+
context.output.stderr.write(`${line}\n`);
|
|
203
|
+
}
|
|
204
|
+
function emitBranchDatabaseWarning(context, warning) {
|
|
205
|
+
if (context.flags.json || context.flags.quiet) return;
|
|
206
|
+
context.output.stderr.write(`${renderSummaryLine(context.ui, "warning", warning)}\n`);
|
|
207
|
+
}
|
|
208
|
+
function emptyBranchDatabaseSetupOutcome() {
|
|
209
|
+
return {
|
|
210
|
+
result: void 0,
|
|
211
|
+
warnings: []
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function formatSchemaSetupCommand(command) {
|
|
215
|
+
switch (command) {
|
|
216
|
+
case "migrate-deploy": return "prisma migrate deploy";
|
|
217
|
+
case "db-push": return "prisma db push";
|
|
218
|
+
case "prisma-next-db-init": return "prisma-next db init";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function branchDatabaseSetupFailedError(summary, error, branchName) {
|
|
222
|
+
return new CliError({
|
|
223
|
+
code: "BRANCH_DATABASE_SETUP_FAILED",
|
|
224
|
+
domain: "app",
|
|
225
|
+
summary,
|
|
226
|
+
why: error instanceof Error ? error.message : String(error),
|
|
227
|
+
fix: "Retry the command, or create the branch database and env vars manually with project env commands.",
|
|
228
|
+
debug: formatDebugDetails(error),
|
|
229
|
+
meta: { branch: branchName },
|
|
230
|
+
exitCode: 1,
|
|
231
|
+
nextSteps: [`prisma-cli app deploy --branch ${formatCommandArgument(branchName)} --db`, `prisma-cli project env list --branch ${formatCommandArgument(branchName)}`]
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
async function cleanupCreatedBranchDatabaseAfterFailure(context, provider, database, branchName, error) {
|
|
235
|
+
const setupError = error instanceof CliError ? error : branchDatabaseSetupFailedError("Branch database setup failed", error, branchName);
|
|
236
|
+
emitBranchDatabaseProgress(context, "pending", "Removing branch database after setup failed");
|
|
237
|
+
try {
|
|
238
|
+
await provider.deleteBranchDatabase({
|
|
239
|
+
databaseId: database.id,
|
|
240
|
+
signal: context.runtime.signal
|
|
241
|
+
});
|
|
242
|
+
emitBranchDatabaseProgress(context, "success", "Removed branch database after setup failed");
|
|
243
|
+
} catch (cleanupError) {
|
|
244
|
+
return branchDatabaseCleanupFailedError(setupError, cleanupError, database, branchName);
|
|
245
|
+
}
|
|
246
|
+
return setupError;
|
|
247
|
+
}
|
|
248
|
+
function branchDatabaseCleanupFailedError(setupError, cleanupError, database, branchName) {
|
|
249
|
+
const cleanupWhy = cleanupError instanceof Error ? cleanupError.message : String(cleanupError);
|
|
250
|
+
const setupWhy = setupError.why ?? "Branch database setup failed.";
|
|
251
|
+
return new CliError({
|
|
252
|
+
code: setupError.code,
|
|
253
|
+
domain: setupError.domain,
|
|
254
|
+
summary: setupError.summary,
|
|
255
|
+
why: `${setupWhy} Prisma could not delete the created database "${database.name}" (${database.id}): ${cleanupWhy}`,
|
|
256
|
+
fix: "Delete the created branch database from Console or contact Prisma support, then rerun deploy with --db.",
|
|
257
|
+
debug: formatCombinedDebugDetails(setupError, cleanupError),
|
|
258
|
+
meta: {
|
|
259
|
+
...setupError.meta,
|
|
260
|
+
branch: branchName,
|
|
261
|
+
databaseId: database.id,
|
|
262
|
+
databaseName: database.name,
|
|
263
|
+
cleanupFailed: true
|
|
264
|
+
},
|
|
265
|
+
exitCode: setupError.exitCode,
|
|
266
|
+
nextSteps: []
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
function schemaSetupFailedError(error, schema, branchName, cwd) {
|
|
270
|
+
return new CliError({
|
|
271
|
+
code: "SCHEMA_SETUP_FAILED",
|
|
272
|
+
domain: "app",
|
|
273
|
+
summary: "Database schema setup failed",
|
|
274
|
+
why: error instanceof Error ? error.message : String(error),
|
|
275
|
+
fix: "Fix the Prisma schema or migrations, then rerun deploy with --db.",
|
|
276
|
+
debug: formatDebugDetails(error),
|
|
277
|
+
meta: {
|
|
278
|
+
branch: branchName,
|
|
279
|
+
schemaPath: schema.path,
|
|
280
|
+
source: schema.kind,
|
|
281
|
+
command: schema.command
|
|
282
|
+
},
|
|
283
|
+
exitCode: 1,
|
|
284
|
+
nextSteps: [...formatSchemaSetupNextSteps(schema, cwd), `prisma-cli app deploy --branch ${formatCommandArgument(branchName)} --db`]
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
function unsupportedBranchDatabaseSchemaError(schema, branchName, context) {
|
|
288
|
+
return usageError("Branch database setup is not available for this Prisma schema", `${path.relative(context.runtime.cwd, schema.path) || defaultUnsupportedSchemaSourcePath(schema)} targets ${formatUnsupportedSchemaTarget(schema.target)}, but --db creates Prisma Postgres databases.`, "Use project env commands to provide a database URL for this branch, or switch the Prisma schema source to PostgreSQL before using --db.", [`prisma-cli project env add DATABASE_URL=<value> --branch ${formatCommandArgument(branchName)}`, `prisma-cli app deploy --branch ${formatCommandArgument(branchName)}`], "app");
|
|
289
|
+
}
|
|
290
|
+
function formatSchemaSetupNextSteps(schema, cwd) {
|
|
291
|
+
const sourcePath = path.relative(cwd, schema.path) || defaultSchemaSourcePath(schema);
|
|
292
|
+
switch (schema.command) {
|
|
293
|
+
case "migrate-deploy": return [`npx --no-install prisma migrate deploy --schema ${formatCommandArgument(sourcePath)}`];
|
|
294
|
+
case "db-push": return [`npx --no-install prisma db push --schema ${formatCommandArgument(sourcePath)}`];
|
|
295
|
+
case "prisma-next-db-init": return [`npx --no-install prisma-next contract emit --config ${formatCommandArgument(sourcePath)}`, `npx --no-install prisma-next db init --config ${formatCommandArgument(sourcePath)} --db <DATABASE_URL>`];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function defaultSchemaSourcePath(schema) {
|
|
299
|
+
return schema.kind === "prisma-next" ? "prisma-next.config.ts" : "schema.prisma";
|
|
300
|
+
}
|
|
301
|
+
function defaultUnsupportedSchemaSourcePath(schema) {
|
|
302
|
+
return schema.kind === "prisma-next" ? "prisma-next.config.ts" : "schema.prisma";
|
|
303
|
+
}
|
|
304
|
+
function formatUnsupportedSchemaTarget(target) {
|
|
305
|
+
switch (target) {
|
|
306
|
+
case "cockroachdb": return "CockroachDB";
|
|
307
|
+
case "mongodb": return "MongoDB";
|
|
308
|
+
case "mysql": return "MySQL";
|
|
309
|
+
case "sqlite": return "SQLite";
|
|
310
|
+
case "sqlserver": return "SQL Server";
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function formatDebugDetails(error) {
|
|
314
|
+
if (error instanceof Error) return error.stack ?? error.message;
|
|
315
|
+
return typeof error === "string" ? error : null;
|
|
316
|
+
}
|
|
317
|
+
function formatCombinedDebugDetails(setupError, cleanupError) {
|
|
318
|
+
const setupDebug = setupError.debug ?? setupError.stack ?? setupError.message;
|
|
319
|
+
const cleanupDebug = formatDebugDetails(cleanupError);
|
|
320
|
+
return [setupDebug ? `Setup error:\n${setupDebug}` : null, cleanupDebug ? `Cleanup error:\n${cleanupDebug}` : null].filter((line) => Boolean(line)).join("\n\n") || null;
|
|
321
|
+
}
|
|
322
|
+
//#endregion
|
|
323
|
+
export { maybeSetupBranchDatabase };
|