@prisma-next/cli 0.3.0-pr.94.3 → 0.3.0-pr.95.2
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-AGOTG4L3.js +965 -0
- package/dist/chunk-AGOTG4L3.js.map +1 -0
- package/dist/chunk-HLLI4YL7.js +180 -0
- package/dist/chunk-HLLI4YL7.js.map +1 -0
- package/dist/chunk-HWYQOCAJ.js +47 -0
- package/dist/chunk-HWYQOCAJ.js.map +1 -0
- package/dist/chunk-VG2R7DGF.js +735 -0
- package/dist/chunk-VG2R7DGF.js.map +1 -0
- package/dist/cli.d.ts +2 -2
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +2764 -2522
- package/dist/cli.js.map +1 -1
- package/dist/commands/contract-emit.d.ts +3 -0
- package/dist/commands/contract-emit.d.ts.map +1 -0
- package/dist/commands/contract-emit.js +10 -0
- package/dist/commands/contract-emit.js.map +1 -0
- package/dist/commands/db-init.d.ts +3 -0
- package/dist/commands/db-init.d.ts.map +1 -0
- package/dist/commands/db-init.js +257 -0
- package/dist/commands/db-init.js.map +1 -0
- package/dist/commands/db-introspect.d.ts +3 -0
- package/dist/commands/db-introspect.d.ts.map +1 -0
- package/dist/commands/db-introspect.js +155 -0
- package/dist/commands/db-introspect.js.map +1 -0
- package/dist/commands/db-schema-verify.d.ts +3 -0
- package/dist/commands/db-schema-verify.d.ts.map +1 -0
- package/dist/commands/db-schema-verify.js +171 -0
- package/dist/commands/db-schema-verify.js.map +1 -0
- package/dist/commands/db-sign.d.ts +3 -0
- package/dist/commands/db-sign.d.ts.map +1 -0
- package/dist/commands/db-sign.js +195 -0
- package/dist/commands/db-sign.js.map +1 -0
- package/dist/commands/db-verify.d.ts +3 -0
- package/dist/commands/db-verify.d.ts.map +1 -0
- package/dist/commands/db-verify.js +193 -0
- package/dist/commands/db-verify.js.map +1 -0
- package/dist/{config-loader.d.mts → config-loader.d.ts} +3 -8
- package/dist/config-loader.d.ts.map +1 -0
- package/dist/config-loader.js +7 -0
- package/dist/config-loader.js.map +1 -0
- package/dist/control-api/client.d.ts +13 -0
- package/dist/control-api/client.d.ts.map +1 -0
- package/dist/control-api/operations/db-init.d.ts +29 -0
- package/dist/control-api/operations/db-init.d.ts.map +1 -0
- package/dist/control-api/types.d.ts +387 -0
- package/dist/control-api/types.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 +6 -0
- package/dist/exports/config-types.js.map +1 -0
- package/dist/exports/control-api.d.ts +13 -0
- package/dist/exports/control-api.d.ts.map +1 -0
- package/dist/exports/control-api.js +7 -0
- package/dist/exports/control-api.js.map +1 -0
- package/dist/exports/index.d.ts +4 -0
- package/dist/exports/index.d.ts.map +1 -0
- package/dist/exports/index.js +176 -0
- package/dist/exports/index.js.map +1 -0
- package/dist/{exports/index.d.mts → load-ts-contract.d.ts} +5 -10
- package/dist/load-ts-contract.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 +70 -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/progress-adapter.d.ts +26 -0
- package/dist/utils/progress-adapter.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/package.json +54 -29
- package/dist/client-CoxxGz_7.mjs +0 -692
- package/dist/client-CoxxGz_7.mjs.map +0 -1
- package/dist/commands/contract-emit.d.mts +0 -7
- package/dist/commands/contract-emit.d.mts.map +0 -1
- package/dist/commands/contract-emit.mjs +0 -139
- package/dist/commands/contract-emit.mjs.map +0 -1
- package/dist/commands/db-init.d.mts +0 -7
- package/dist/commands/db-init.d.mts.map +0 -1
- package/dist/commands/db-init.mjs +0 -178
- package/dist/commands/db-init.mjs.map +0 -1
- package/dist/commands/db-introspect.d.mts +0 -7
- package/dist/commands/db-introspect.d.mts.map +0 -1
- package/dist/commands/db-introspect.mjs +0 -120
- package/dist/commands/db-introspect.mjs.map +0 -1
- package/dist/commands/db-schema-verify.d.mts +0 -7
- package/dist/commands/db-schema-verify.d.mts.map +0 -1
- package/dist/commands/db-schema-verify.mjs +0 -116
- package/dist/commands/db-schema-verify.mjs.map +0 -1
- package/dist/commands/db-sign.d.mts +0 -7
- package/dist/commands/db-sign.d.mts.map +0 -1
- package/dist/commands/db-sign.mjs +0 -138
- package/dist/commands/db-sign.mjs.map +0 -1
- package/dist/commands/db-verify.d.mts +0 -7
- package/dist/commands/db-verify.d.mts.map +0 -1
- package/dist/commands/db-verify.mjs +0 -128
- package/dist/commands/db-verify.mjs.map +0 -1
- package/dist/config-loader-B-pobG0R.mjs +0 -42
- package/dist/config-loader-B-pobG0R.mjs.map +0 -1
- package/dist/config-loader.d.mts.map +0 -1
- package/dist/config-loader.mjs +0 -3
- package/dist/exports/config-types.d.mts +0 -2
- package/dist/exports/config-types.mjs +0 -3
- package/dist/exports/control-api.d.mts +0 -405
- package/dist/exports/control-api.d.mts.map +0 -1
- package/dist/exports/control-api.mjs +0 -3
- package/dist/exports/index.d.mts.map +0 -1
- package/dist/exports/index.mjs +0 -128
- package/dist/exports/index.mjs.map +0 -1
- package/dist/result-handler-Do_4Fi4U.mjs +0 -959
- package/dist/result-handler-Do_4Fi4U.mjs.map +0 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProgressAdapter,
|
|
3
|
+
formatCommandHelp,
|
|
4
|
+
formatSchemaVerifyJson,
|
|
5
|
+
formatSchemaVerifyOutput,
|
|
6
|
+
formatStyledHeader,
|
|
7
|
+
handleResult,
|
|
8
|
+
parseGlobalFlags,
|
|
9
|
+
setCommandDescriptions
|
|
10
|
+
} from "../chunk-AGOTG4L3.js";
|
|
11
|
+
import {
|
|
12
|
+
loadConfig
|
|
13
|
+
} from "../chunk-HWYQOCAJ.js";
|
|
14
|
+
import {
|
|
15
|
+
CliStructuredError,
|
|
16
|
+
createControlClient,
|
|
17
|
+
errorContractValidationFailed,
|
|
18
|
+
errorDatabaseConnectionRequired,
|
|
19
|
+
errorDriverRequired,
|
|
20
|
+
errorFileNotFound,
|
|
21
|
+
errorJsonFormatNotSupported,
|
|
22
|
+
errorUnexpected
|
|
23
|
+
} from "../chunk-VG2R7DGF.js";
|
|
24
|
+
|
|
25
|
+
// src/commands/db-schema-verify.ts
|
|
26
|
+
import { readFile } from "fs/promises";
|
|
27
|
+
import { relative, resolve } from "path";
|
|
28
|
+
import { notOk, ok } from "@prisma-next/utils/result";
|
|
29
|
+
import { Command } from "commander";
|
|
30
|
+
async function executeDbSchemaVerifyCommand(options, flags) {
|
|
31
|
+
const config = await loadConfig(options.config);
|
|
32
|
+
const configPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
|
|
33
|
+
const contractPathAbsolute = config.contract?.output ? resolve(config.contract.output) : resolve("src/prisma/contract.json");
|
|
34
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
35
|
+
if (flags.json !== "object" && !flags.quiet) {
|
|
36
|
+
const details = [
|
|
37
|
+
{ label: "config", value: configPath },
|
|
38
|
+
{ label: "contract", value: contractPath }
|
|
39
|
+
];
|
|
40
|
+
if (options.db) {
|
|
41
|
+
details.push({ label: "database", value: options.db });
|
|
42
|
+
}
|
|
43
|
+
const header = formatStyledHeader({
|
|
44
|
+
command: "db schema-verify",
|
|
45
|
+
description: "Check whether the database schema satisfies your contract",
|
|
46
|
+
url: "https://pris.ly/db-schema-verify",
|
|
47
|
+
details,
|
|
48
|
+
flags
|
|
49
|
+
});
|
|
50
|
+
console.log(header);
|
|
51
|
+
}
|
|
52
|
+
let contractJsonContent;
|
|
53
|
+
try {
|
|
54
|
+
contractJsonContent = await readFile(contractPathAbsolute, "utf-8");
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (error instanceof Error && error.code === "ENOENT") {
|
|
57
|
+
return notOk(
|
|
58
|
+
errorFileNotFound(contractPathAbsolute, {
|
|
59
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
60
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
return notOk(
|
|
65
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
66
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
let contractJson;
|
|
71
|
+
try {
|
|
72
|
+
contractJson = JSON.parse(contractJsonContent);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
return notOk(
|
|
75
|
+
errorContractValidationFailed(
|
|
76
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
77
|
+
{ where: { path: contractPathAbsolute } }
|
|
78
|
+
)
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
82
|
+
if (!dbConnection) {
|
|
83
|
+
return notOk(
|
|
84
|
+
errorDatabaseConnectionRequired({
|
|
85
|
+
why: `Database connection is required for db schema-verify (set db.connection in ${configPath}, or pass --db <url>)`
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (!config.driver) {
|
|
90
|
+
return notOk(errorDriverRequired({ why: "Config.driver is required for db schema-verify" }));
|
|
91
|
+
}
|
|
92
|
+
const client = createControlClient({
|
|
93
|
+
family: config.family,
|
|
94
|
+
target: config.target,
|
|
95
|
+
adapter: config.adapter,
|
|
96
|
+
driver: config.driver,
|
|
97
|
+
extensionPacks: config.extensionPacks ?? []
|
|
98
|
+
});
|
|
99
|
+
const onProgress = createProgressAdapter({ flags });
|
|
100
|
+
try {
|
|
101
|
+
const schemaVerifyResult = await client.schemaVerify({
|
|
102
|
+
contractIR: contractJson,
|
|
103
|
+
strict: options.strict ?? false,
|
|
104
|
+
connection: dbConnection,
|
|
105
|
+
onProgress
|
|
106
|
+
});
|
|
107
|
+
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
108
|
+
console.log("");
|
|
109
|
+
}
|
|
110
|
+
return ok(schemaVerifyResult);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error instanceof CliStructuredError) {
|
|
113
|
+
return notOk(error);
|
|
114
|
+
}
|
|
115
|
+
return notOk(
|
|
116
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
117
|
+
why: `Unexpected error during db schema-verify: ${error instanceof Error ? error.message : String(error)}`
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
} finally {
|
|
121
|
+
await client.close();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function createDbSchemaVerifyCommand() {
|
|
125
|
+
const command = new Command("schema-verify");
|
|
126
|
+
setCommandDescriptions(
|
|
127
|
+
command,
|
|
128
|
+
"Check whether the database schema satisfies your contract",
|
|
129
|
+
"Verifies that your database schema satisfies the emitted contract. Compares table structures,\ncolumn types, constraints, and extensions. Reports any mismatches via a contract-shaped\nverification tree. This is a read-only operation that does not modify the database."
|
|
130
|
+
);
|
|
131
|
+
command.configureHelp({
|
|
132
|
+
formatHelp: (cmd) => {
|
|
133
|
+
const flags = parseGlobalFlags({});
|
|
134
|
+
return formatCommandHelp({ command: cmd, flags });
|
|
135
|
+
}
|
|
136
|
+
}).option("--db <url>", "Database connection string").option("--config <path>", "Path to prisma-next.config.ts").option("--json [format]", "Output as JSON (object)", false).option("--strict", "Strict mode: extra schema elements cause failures", 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) => {
|
|
137
|
+
const flags = parseGlobalFlags(options);
|
|
138
|
+
if (flags.json === "ndjson") {
|
|
139
|
+
const result2 = notOk(
|
|
140
|
+
errorJsonFormatNotSupported({
|
|
141
|
+
command: "db schema-verify",
|
|
142
|
+
format: "ndjson",
|
|
143
|
+
supportedFormats: ["object"]
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
const exitCode2 = handleResult(result2, flags);
|
|
147
|
+
process.exit(exitCode2);
|
|
148
|
+
}
|
|
149
|
+
const result = await executeDbSchemaVerifyCommand(options, flags);
|
|
150
|
+
const exitCode = handleResult(result, flags, (schemaVerifyResult) => {
|
|
151
|
+
if (flags.json === "object") {
|
|
152
|
+
console.log(formatSchemaVerifyJson(schemaVerifyResult));
|
|
153
|
+
} else {
|
|
154
|
+
const output = formatSchemaVerifyOutput(schemaVerifyResult, flags);
|
|
155
|
+
if (output) {
|
|
156
|
+
console.log(output);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
if (result.ok && !result.value.ok) {
|
|
161
|
+
process.exit(1);
|
|
162
|
+
} else {
|
|
163
|
+
process.exit(exitCode);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
return command;
|
|
167
|
+
}
|
|
168
|
+
export {
|
|
169
|
+
createDbSchemaVerifyCommand
|
|
170
|
+
};
|
|
171
|
+
//# sourceMappingURL=db-schema-verify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/db-schema-verify.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { relative, resolve } from 'node:path';\nimport type { VerifyDatabaseSchemaResult } from '@prisma-next/core-control-plane/types';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { Command } from 'commander';\nimport { loadConfig } from '../config-loader';\nimport { createControlClient } from '../control-api/client';\nimport {\n CliStructuredError,\n errorContractValidationFailed,\n errorDatabaseConnectionRequired,\n errorDriverRequired,\n errorFileNotFound,\n errorJsonFormatNotSupported,\n errorUnexpected,\n} from '../utils/cli-errors';\nimport { setCommandDescriptions } from '../utils/command-helpers';\nimport { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';\nimport {\n formatCommandHelp,\n formatSchemaVerifyJson,\n formatSchemaVerifyOutput,\n formatStyledHeader,\n} from '../utils/output';\nimport { createProgressAdapter } from '../utils/progress-adapter';\nimport { handleResult } from '../utils/result-handler';\n\ninterface DbSchemaVerifyOptions {\n readonly db?: string;\n readonly config?: string;\n readonly json?: string | boolean;\n readonly strict?: boolean;\n readonly quiet?: boolean;\n readonly q?: boolean;\n readonly verbose?: boolean;\n readonly v?: boolean;\n readonly vv?: boolean;\n readonly trace?: boolean;\n readonly timestamps?: boolean;\n readonly color?: boolean;\n readonly 'no-color'?: boolean;\n}\n\n/**\n * Executes the db schema-verify command and returns a structured Result.\n */\nasync function executeDbSchemaVerifyCommand(\n options: DbSchemaVerifyOptions,\n flags: GlobalFlags,\n): Promise<Result<VerifyDatabaseSchemaResult, CliStructuredError>> {\n // Load config\n const config = await loadConfig(options.config);\n const configPath = options.config\n ? relative(process.cwd(), resolve(options.config))\n : 'prisma-next.config.ts';\n const contractPathAbsolute = config.contract?.output\n ? resolve(config.contract.output)\n : resolve('src/prisma/contract.json');\n const contractPath = relative(process.cwd(), contractPathAbsolute);\n\n // Output header\n if (flags.json !== 'object' && !flags.quiet) {\n const details: Array<{ label: string; value: string }> = [\n { label: 'config', value: configPath },\n { label: 'contract', value: contractPath },\n ];\n if (options.db) {\n details.push({ label: 'database', value: options.db });\n }\n const header = formatStyledHeader({\n command: 'db schema-verify',\n description: 'Check whether the database schema satisfies your contract',\n url: 'https://pris.ly/db-schema-verify',\n details,\n flags,\n });\n console.log(header);\n }\n\n // Load contract file\n let contractJsonContent: string;\n try {\n contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return notOk(\n errorFileNotFound(contractPathAbsolute, {\n why: `Contract file not found at ${contractPathAbsolute}`,\n fix: `Run \\`prisma-next contract emit\\` to generate ${contractPath}, or update \\`config.contract.output\\` in ${configPath}`,\n }),\n );\n }\n return notOk(\n errorUnexpected(error instanceof Error ? error.message : String(error), {\n why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n }\n\n let contractJson: Record<string, unknown>;\n try {\n contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;\n } catch (error) {\n return notOk(\n errorContractValidationFailed(\n `Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,\n { where: { path: contractPathAbsolute } },\n ),\n );\n }\n\n // Resolve database connection (--db flag or config.db.connection)\n const dbConnection = options.db ?? config.db?.connection;\n if (!dbConnection) {\n return notOk(\n errorDatabaseConnectionRequired({\n why: `Database connection is required for db schema-verify (set db.connection in ${configPath}, or pass --db <url>)`,\n }),\n );\n }\n\n // Check for driver\n if (!config.driver) {\n return notOk(errorDriverRequired({ why: 'Config.driver is required for db schema-verify' }));\n }\n\n // Create control client\n const client = createControlClient({\n family: config.family,\n target: config.target,\n adapter: config.adapter,\n driver: config.driver,\n extensionPacks: config.extensionPacks ?? [],\n });\n\n // Create progress adapter\n const onProgress = createProgressAdapter({ flags });\n\n try {\n const schemaVerifyResult = await client.schemaVerify({\n contractIR: contractJson,\n strict: options.strict ?? false,\n connection: dbConnection,\n onProgress,\n });\n\n // Add blank line after all async operations if spinners were shown\n if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {\n console.log('');\n }\n\n return ok(schemaVerifyResult);\n } catch (error) {\n // Driver already throws CliStructuredError for connection failures\n if (error instanceof CliStructuredError) {\n return notOk(error);\n }\n\n // Wrap unexpected errors\n return notOk(\n errorUnexpected(error instanceof Error ? error.message : String(error), {\n why: `Unexpected error during db schema-verify: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n } finally {\n await client.close();\n }\n}\n\nexport function createDbSchemaVerifyCommand(): Command {\n const command = new Command('schema-verify');\n setCommandDescriptions(\n command,\n 'Check whether the database schema satisfies your contract',\n 'Verifies that your database schema satisfies the emitted contract. Compares table structures,\\n' +\n 'column types, constraints, and extensions. Reports any mismatches via a contract-shaped\\n' +\n 'verification tree. This is a read-only operation that does not modify the database.',\n );\n command\n .configureHelp({\n formatHelp: (cmd) => {\n const flags = parseGlobalFlags({});\n return formatCommandHelp({ command: cmd, flags });\n },\n })\n .option('--db <url>', 'Database connection string')\n .option('--config <path>', 'Path to prisma-next.config.ts')\n .option('--json [format]', 'Output as JSON (object)', false)\n .option('--strict', 'Strict mode: extra schema elements cause failures', false)\n .option('-q, --quiet', 'Quiet mode: errors only')\n .option('-v, --verbose', 'Verbose output: debug info, timings')\n .option('-vv, --trace', 'Trace output: deep internals, stack traces')\n .option('--timestamps', 'Add timestamps to output')\n .option('--color', 'Force color output')\n .option('--no-color', 'Disable color output')\n .action(async (options: DbSchemaVerifyOptions) => {\n const flags = parseGlobalFlags(options);\n\n // Validate JSON format option\n if (flags.json === 'ndjson') {\n const result = notOk(\n errorJsonFormatNotSupported({\n command: 'db schema-verify',\n format: 'ndjson',\n supportedFormats: ['object'],\n }),\n );\n const exitCode = handleResult(result, flags);\n process.exit(exitCode);\n }\n\n const result = await executeDbSchemaVerifyCommand(options, flags);\n\n // Handle result - formats output and returns exit code\n const exitCode = handleResult(result, flags, (schemaVerifyResult) => {\n if (flags.json === 'object') {\n console.log(formatSchemaVerifyJson(schemaVerifyResult));\n } else {\n const output = formatSchemaVerifyOutput(schemaVerifyResult, flags);\n if (output) {\n console.log(output);\n }\n }\n });\n\n // For logical schema mismatches, check if verification passed\n // Infra errors already handled by handleResult (returns non-zero exit code)\n if (result.ok && !result.value.ok) {\n // Schema verification failed - exit with code 1\n process.exit(1);\n } else {\n // Success or infra error - use exit code from handleResult\n process.exit(exitCode);\n }\n });\n\n return command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M,SAAS,UAAU;AAC3B,gBAAQ,IAAI,uBAAuB,kBAAkB,CAAC;AAAA,MACxD,OAAO;AACL,cAAM,SAAS,yBAAyB,oBAAoB,KAAK;AACjE,YAAI,QAAQ;AACV,kBAAQ,IAAI,MAAM;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AAID,QAAI,OAAO,MAAM,CAAC,OAAO,MAAM,IAAI;AAEjC,cAAQ,KAAK,CAAC;AAAA,IAChB,OAAO;AAEL,cAAQ,KAAK,QAAQ;AAAA,IACvB;AAAA,EACF,CAAC;AAEH,SAAO;AACT;","names":["result","exitCode"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-sign.d.ts","sourceRoot":"","sources":["../../src/commands/db-sign.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAgMpC,wBAAgB,mBAAmB,IAAI,OAAO,CA+E7C"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProgressAdapter,
|
|
3
|
+
formatCommandHelp,
|
|
4
|
+
formatSchemaVerifyJson,
|
|
5
|
+
formatSchemaVerifyOutput,
|
|
6
|
+
formatSignJson,
|
|
7
|
+
formatSignOutput,
|
|
8
|
+
formatStyledHeader,
|
|
9
|
+
handleResult,
|
|
10
|
+
parseGlobalFlags,
|
|
11
|
+
setCommandDescriptions
|
|
12
|
+
} from "../chunk-AGOTG4L3.js";
|
|
13
|
+
import {
|
|
14
|
+
loadConfig
|
|
15
|
+
} from "../chunk-HWYQOCAJ.js";
|
|
16
|
+
import {
|
|
17
|
+
CliStructuredError,
|
|
18
|
+
createControlClient,
|
|
19
|
+
errorContractValidationFailed,
|
|
20
|
+
errorDatabaseConnectionRequired,
|
|
21
|
+
errorDriverRequired,
|
|
22
|
+
errorFileNotFound,
|
|
23
|
+
errorJsonFormatNotSupported,
|
|
24
|
+
errorUnexpected
|
|
25
|
+
} from "../chunk-VG2R7DGF.js";
|
|
26
|
+
|
|
27
|
+
// src/commands/db-sign.ts
|
|
28
|
+
import { readFile } from "fs/promises";
|
|
29
|
+
import { relative, resolve } from "path";
|
|
30
|
+
import { notOk, ok } from "@prisma-next/utils/result";
|
|
31
|
+
import { Command } from "commander";
|
|
32
|
+
async function executeDbSignCommand(options, flags) {
|
|
33
|
+
const config = await loadConfig(options.config);
|
|
34
|
+
const configPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
|
|
35
|
+
const contractPathAbsolute = config.contract?.output ? resolve(config.contract.output) : resolve("src/prisma/contract.json");
|
|
36
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
37
|
+
if (flags.json !== "object" && !flags.quiet) {
|
|
38
|
+
const details = [
|
|
39
|
+
{ label: "config", value: configPath },
|
|
40
|
+
{ label: "contract", value: contractPath }
|
|
41
|
+
];
|
|
42
|
+
if (options.db) {
|
|
43
|
+
details.push({ label: "database", value: options.db });
|
|
44
|
+
}
|
|
45
|
+
const header = formatStyledHeader({
|
|
46
|
+
command: "db sign",
|
|
47
|
+
description: "Sign the database with your contract so you can safely run queries",
|
|
48
|
+
url: "https://pris.ly/db-sign",
|
|
49
|
+
details,
|
|
50
|
+
flags
|
|
51
|
+
});
|
|
52
|
+
console.log(header);
|
|
53
|
+
}
|
|
54
|
+
let contractJsonContent;
|
|
55
|
+
try {
|
|
56
|
+
contractJsonContent = await readFile(contractPathAbsolute, "utf-8");
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error instanceof Error && error.code === "ENOENT") {
|
|
59
|
+
return notOk(
|
|
60
|
+
errorFileNotFound(contractPathAbsolute, {
|
|
61
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
62
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`
|
|
63
|
+
})
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
return notOk(
|
|
67
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
68
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
let contractJson;
|
|
73
|
+
try {
|
|
74
|
+
contractJson = JSON.parse(contractJsonContent);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return notOk(
|
|
77
|
+
errorContractValidationFailed(
|
|
78
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
79
|
+
{ where: { path: contractPathAbsolute } }
|
|
80
|
+
)
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
84
|
+
if (!dbConnection) {
|
|
85
|
+
return notOk(
|
|
86
|
+
errorDatabaseConnectionRequired({
|
|
87
|
+
why: `Database connection is required for db sign (set db.connection in ${configPath}, or pass --db <url>)`
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (!config.driver) {
|
|
92
|
+
return notOk(errorDriverRequired({ why: "Config.driver is required for db sign" }));
|
|
93
|
+
}
|
|
94
|
+
const client = createControlClient({
|
|
95
|
+
family: config.family,
|
|
96
|
+
target: config.target,
|
|
97
|
+
adapter: config.adapter,
|
|
98
|
+
driver: config.driver,
|
|
99
|
+
extensionPacks: config.extensionPacks ?? []
|
|
100
|
+
});
|
|
101
|
+
const onProgress = createProgressAdapter({ flags });
|
|
102
|
+
try {
|
|
103
|
+
const schemaVerifyResult = await client.schemaVerify({
|
|
104
|
+
contractIR: contractJson,
|
|
105
|
+
strict: false,
|
|
106
|
+
connection: dbConnection,
|
|
107
|
+
onProgress
|
|
108
|
+
});
|
|
109
|
+
if (!schemaVerifyResult.ok) {
|
|
110
|
+
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
111
|
+
console.log("");
|
|
112
|
+
}
|
|
113
|
+
return notOk(schemaVerifyResult);
|
|
114
|
+
}
|
|
115
|
+
const signResult = await client.sign({
|
|
116
|
+
contractIR: contractJson,
|
|
117
|
+
contractPath,
|
|
118
|
+
configPath,
|
|
119
|
+
onProgress
|
|
120
|
+
});
|
|
121
|
+
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
122
|
+
console.log("");
|
|
123
|
+
}
|
|
124
|
+
return ok(signResult);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (error instanceof CliStructuredError) {
|
|
127
|
+
return notOk(error);
|
|
128
|
+
}
|
|
129
|
+
return notOk(
|
|
130
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
131
|
+
why: `Unexpected error during db sign: ${error instanceof Error ? error.message : String(error)}`
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
} finally {
|
|
135
|
+
await client.close();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function createDbSignCommand() {
|
|
139
|
+
const command = new Command("sign");
|
|
140
|
+
setCommandDescriptions(
|
|
141
|
+
command,
|
|
142
|
+
"Sign the database with your contract so you can safely run queries",
|
|
143
|
+
"Verifies that your database schema satisfies the emitted contract, and if so, writes or\nupdates the contract marker in the database. This command is idempotent and safe to run\nin CI/deployment pipelines. The marker records that this database instance is aligned\nwith a specific contract version."
|
|
144
|
+
);
|
|
145
|
+
command.configureHelp({
|
|
146
|
+
formatHelp: (cmd) => {
|
|
147
|
+
const flags = parseGlobalFlags({});
|
|
148
|
+
return formatCommandHelp({ command: cmd, flags });
|
|
149
|
+
}
|
|
150
|
+
}).option("--db <url>", "Database connection string").option("--config <path>", "Path to prisma-next.config.ts").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) => {
|
|
151
|
+
const flags = parseGlobalFlags(options);
|
|
152
|
+
if (flags.json === "ndjson") {
|
|
153
|
+
const result2 = notOk(
|
|
154
|
+
errorJsonFormatNotSupported({
|
|
155
|
+
command: "db sign",
|
|
156
|
+
format: "ndjson",
|
|
157
|
+
supportedFormats: ["object"]
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
const exitCode = handleResult(result2, flags);
|
|
161
|
+
process.exit(exitCode);
|
|
162
|
+
}
|
|
163
|
+
const result = await executeDbSignCommand(options, flags);
|
|
164
|
+
if (result.ok) {
|
|
165
|
+
if (flags.json === "object") {
|
|
166
|
+
console.log(formatSignJson(result.value));
|
|
167
|
+
} else {
|
|
168
|
+
const output = formatSignOutput(result.value, flags);
|
|
169
|
+
if (output) {
|
|
170
|
+
console.log(output);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
process.exit(0);
|
|
174
|
+
}
|
|
175
|
+
const failure = result.failure;
|
|
176
|
+
if (failure instanceof CliStructuredError) {
|
|
177
|
+
const exitCode = handleResult(result, flags);
|
|
178
|
+
process.exit(exitCode);
|
|
179
|
+
}
|
|
180
|
+
if (flags.json === "object") {
|
|
181
|
+
console.log(formatSchemaVerifyJson(failure));
|
|
182
|
+
} else {
|
|
183
|
+
const output = formatSchemaVerifyOutput(failure, flags);
|
|
184
|
+
if (output) {
|
|
185
|
+
console.log(output);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
process.exit(1);
|
|
189
|
+
});
|
|
190
|
+
return command;
|
|
191
|
+
}
|
|
192
|
+
export {
|
|
193
|
+
createDbSignCommand
|
|
194
|
+
};
|
|
195
|
+
//# sourceMappingURL=db-sign.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/db-sign.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { relative, resolve } from 'node:path';\nimport type {\n SignDatabaseResult,\n VerifyDatabaseSchemaResult,\n} from '@prisma-next/core-control-plane/types';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { Command } from 'commander';\nimport { loadConfig } from '../config-loader';\nimport { createControlClient } from '../control-api/client';\nimport {\n CliStructuredError,\n errorContractValidationFailed,\n errorDatabaseConnectionRequired,\n errorDriverRequired,\n errorFileNotFound,\n errorJsonFormatNotSupported,\n errorUnexpected,\n} from '../utils/cli-errors';\nimport { setCommandDescriptions } from '../utils/command-helpers';\nimport { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';\nimport {\n formatCommandHelp,\n formatSchemaVerifyJson,\n formatSchemaVerifyOutput,\n formatSignJson,\n formatSignOutput,\n formatStyledHeader,\n} from '../utils/output';\nimport { createProgressAdapter } from '../utils/progress-adapter';\nimport { handleResult } from '../utils/result-handler';\n\ninterface DbSignOptions {\n readonly db?: string;\n readonly config?: string;\n readonly json?: string | boolean;\n readonly quiet?: boolean;\n readonly q?: boolean;\n readonly verbose?: boolean;\n readonly v?: boolean;\n readonly vv?: boolean;\n readonly trace?: boolean;\n readonly timestamps?: boolean;\n readonly color?: boolean;\n readonly 'no-color'?: boolean;\n}\n\n/**\n * Failure type for db sign command.\n * Either an infrastructure error (CliStructuredError) or a logical failure (schema verification failed).\n */\ntype DbSignFailure = CliStructuredError | VerifyDatabaseSchemaResult;\n\n/**\n * Executes the db sign command and returns a structured Result.\n * Success: SignDatabaseResult (sign happened)\n * Failure: CliStructuredError (infra error) or VerifyDatabaseSchemaResult (schema mismatch)\n */\nasync function executeDbSignCommand(\n options: DbSignOptions,\n flags: GlobalFlags,\n): Promise<Result<SignDatabaseResult, DbSignFailure>> {\n // Load config\n const config = await loadConfig(options.config);\n const configPath = options.config\n ? relative(process.cwd(), resolve(options.config))\n : 'prisma-next.config.ts';\n const contractPathAbsolute = config.contract?.output\n ? resolve(config.contract.output)\n : resolve('src/prisma/contract.json');\n const contractPath = relative(process.cwd(), contractPathAbsolute);\n\n // Output header\n if (flags.json !== 'object' && !flags.quiet) {\n const details: Array<{ label: string; value: string }> = [\n { label: 'config', value: configPath },\n { label: 'contract', value: contractPath },\n ];\n if (options.db) {\n details.push({ label: 'database', value: options.db });\n }\n const header = formatStyledHeader({\n command: 'db sign',\n description: 'Sign the database with your contract so you can safely run queries',\n url: 'https://pris.ly/db-sign',\n details,\n flags,\n });\n console.log(header);\n }\n\n // Load contract file\n let contractJsonContent: string;\n try {\n contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return notOk(\n errorFileNotFound(contractPathAbsolute, {\n why: `Contract file not found at ${contractPathAbsolute}`,\n fix: `Run \\`prisma-next contract emit\\` to generate ${contractPath}, or update \\`config.contract.output\\` in ${configPath}`,\n }),\n );\n }\n return notOk(\n errorUnexpected(error instanceof Error ? error.message : String(error), {\n why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n }\n\n let contractJson: Record<string, unknown>;\n try {\n contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;\n } catch (error) {\n return notOk(\n errorContractValidationFailed(\n `Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,\n { where: { path: contractPathAbsolute } },\n ),\n );\n }\n\n // Resolve database connection (--db flag or config.db.connection)\n const dbConnection = options.db ?? config.db?.connection;\n if (!dbConnection) {\n return notOk(\n errorDatabaseConnectionRequired({\n why: `Database connection is required for db sign (set db.connection in ${configPath}, or pass --db <url>)`,\n }),\n );\n }\n\n // Check for driver\n if (!config.driver) {\n return notOk(errorDriverRequired({ why: 'Config.driver is required for db sign' }));\n }\n\n // Create control client\n const client = createControlClient({\n family: config.family,\n target: config.target,\n adapter: config.adapter,\n driver: config.driver,\n extensionPacks: config.extensionPacks ?? [],\n });\n\n // Create progress adapter\n const onProgress = createProgressAdapter({ flags });\n\n try {\n // Step 1: Schema verification - connect here\n const schemaVerifyResult = await client.schemaVerify({\n contractIR: contractJson,\n strict: false,\n connection: dbConnection,\n onProgress,\n });\n\n // If schema verification failed, return as failure\n if (!schemaVerifyResult.ok) {\n // Add blank line after all async operations if spinners were shown\n if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {\n console.log('');\n }\n return notOk(schemaVerifyResult);\n }\n\n // Step 2: Sign (already connected from schemaVerify)\n const signResult = await client.sign({\n contractIR: contractJson,\n contractPath,\n configPath,\n onProgress,\n });\n\n // Add blank line after all async operations if spinners were shown\n if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {\n console.log('');\n }\n\n return ok(signResult);\n } catch (error) {\n // Driver already throws CliStructuredError for connection failures\n if (error instanceof CliStructuredError) {\n return notOk(error);\n }\n\n // Wrap unexpected errors\n return notOk(\n errorUnexpected(error instanceof Error ? error.message : String(error), {\n why: `Unexpected error during db sign: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n } finally {\n await client.close();\n }\n}\n\nexport function createDbSignCommand(): Command {\n const command = new Command('sign');\n setCommandDescriptions(\n command,\n 'Sign the database with your contract so you can safely run queries',\n 'Verifies that your database schema satisfies the emitted contract, and if so, writes or\\n' +\n 'updates the contract marker in the database. This command is idempotent and safe to run\\n' +\n 'in CI/deployment pipelines. The marker records that this database instance is aligned\\n' +\n 'with a specific contract version.',\n );\n command\n .configureHelp({\n formatHelp: (cmd) => {\n const flags = parseGlobalFlags({});\n return formatCommandHelp({ command: cmd, flags });\n },\n })\n .option('--db <url>', 'Database connection string')\n .option('--config <path>', 'Path to prisma-next.config.ts')\n .option('--json [format]', 'Output as JSON (object)', false)\n .option('-q, --quiet', 'Quiet mode: errors only')\n .option('-v, --verbose', 'Verbose output: debug info, timings')\n .option('-vv, --trace', 'Trace output: deep internals, stack traces')\n .option('--timestamps', 'Add timestamps to output')\n .option('--color', 'Force color output')\n .option('--no-color', 'Disable color output')\n .action(async (options: DbSignOptions) => {\n const flags = parseGlobalFlags(options);\n\n // Validate JSON format option\n if (flags.json === 'ndjson') {\n const result = notOk(\n errorJsonFormatNotSupported({\n command: 'db sign',\n format: 'ndjson',\n supportedFormats: ['object'],\n }),\n );\n const exitCode = handleResult(result, flags);\n process.exit(exitCode);\n }\n\n const result = await executeDbSignCommand(options, flags);\n\n if (result.ok) {\n // Success - format sign output\n if (flags.json === 'object') {\n console.log(formatSignJson(result.value));\n } else {\n const output = formatSignOutput(result.value, flags);\n if (output) {\n console.log(output);\n }\n }\n process.exit(0);\n }\n\n // Failure - determine type and handle appropriately\n const failure = result.failure;\n\n if (failure instanceof CliStructuredError) {\n // Infrastructure error - use standard handler\n const exitCode = handleResult(result as Result<never, CliStructuredError>, flags);\n process.exit(exitCode);\n }\n\n // Schema verification failed - format and print schema verification output\n if (flags.json === 'object') {\n console.log(formatSchemaVerifyJson(failure));\n } else {\n const output = formatSchemaVerifyOutput(failure, flags);\n if (output) {\n console.log(output);\n }\n }\n process.exit(1);\n });\n\n return command;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,UAAU,eAAe;AAKlC,SAAS,OAAO,UAAuB;AACvC,SAAS,eAAe;AAmDxB,eAAe,qBACb,SACA,OACoD;AAEpD,QAAM,SAAS,MAAM,WAAW,QAAQ,MAAM;AAC9C,QAAM,aAAa,QAAQ,SACvB,SAAS,QAAQ,IAAI,GAAG,QAAQ,QAAQ,MAAM,CAAC,IAC/C;AACJ,QAAM,uBAAuB,OAAO,UAAU,SAC1C,QAAQ,OAAO,SAAS,MAAM,IAC9B,QAAQ,0BAA0B;AACtC,QAAM,eAAe,SAAS,QAAQ,IAAI,GAAG,oBAAoB;AAGjE,MAAI,MAAM,SAAS,YAAY,CAAC,MAAM,OAAO;AAC3C,UAAM,UAAmD;AAAA,MACvD,EAAE,OAAO,UAAU,OAAO,WAAW;AAAA,MACrC,EAAE,OAAO,YAAY,OAAO,aAAa;AAAA,IAC3C;AACA,QAAI,QAAQ,IAAI;AACd,cAAQ,KAAK,EAAE,OAAO,YAAY,OAAO,QAAQ,GAAG,CAAC;AAAA,IACvD;AACA,UAAM,SAAS,mBAAmB;AAAA,MAChC,SAAS;AAAA,MACT,aAAa;AAAA,MACb,KAAK;AAAA,MACL;AAAA,MACA;AAAA,IACF,CAAC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|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-verify.d.ts","sourceRoot":"","sources":["../../src/commands/db-verify.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAqMpC,wBAAgB,qBAAqB,IAAI,OAAO,CAwD/C"}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProgressAdapter,
|
|
3
|
+
formatCommandHelp,
|
|
4
|
+
formatStyledHeader,
|
|
5
|
+
formatVerifyJson,
|
|
6
|
+
formatVerifyOutput,
|
|
7
|
+
handleResult,
|
|
8
|
+
parseGlobalFlags,
|
|
9
|
+
setCommandDescriptions
|
|
10
|
+
} from "../chunk-AGOTG4L3.js";
|
|
11
|
+
import {
|
|
12
|
+
loadConfig
|
|
13
|
+
} from "../chunk-HWYQOCAJ.js";
|
|
14
|
+
import {
|
|
15
|
+
CliStructuredError,
|
|
16
|
+
createControlClient,
|
|
17
|
+
errorContractValidationFailed,
|
|
18
|
+
errorDatabaseConnectionRequired,
|
|
19
|
+
errorDriverRequired,
|
|
20
|
+
errorFileNotFound,
|
|
21
|
+
errorHashMismatch,
|
|
22
|
+
errorJsonFormatNotSupported,
|
|
23
|
+
errorMarkerMissing,
|
|
24
|
+
errorRuntime,
|
|
25
|
+
errorTargetMismatch,
|
|
26
|
+
errorUnexpected
|
|
27
|
+
} from "../chunk-VG2R7DGF.js";
|
|
28
|
+
|
|
29
|
+
// src/commands/db-verify.ts
|
|
30
|
+
import { readFile } from "fs/promises";
|
|
31
|
+
import { relative, resolve } from "path";
|
|
32
|
+
import { notOk, ok } from "@prisma-next/utils/result";
|
|
33
|
+
import { Command } from "commander";
|
|
34
|
+
function mapVerifyFailure(verifyResult) {
|
|
35
|
+
if (!verifyResult.ok && verifyResult.code) {
|
|
36
|
+
if (verifyResult.code === "PN-RTM-3001") {
|
|
37
|
+
return errorMarkerMissing();
|
|
38
|
+
}
|
|
39
|
+
if (verifyResult.code === "PN-RTM-3002") {
|
|
40
|
+
return errorHashMismatch({
|
|
41
|
+
expected: verifyResult.contract.coreHash,
|
|
42
|
+
...verifyResult.marker?.coreHash ? { actual: verifyResult.marker.coreHash } : {}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (verifyResult.code === "PN-RTM-3003") {
|
|
46
|
+
return errorTargetMismatch(
|
|
47
|
+
verifyResult.target.expected,
|
|
48
|
+
verifyResult.target.actual ?? "unknown"
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return errorRuntime(verifyResult.summary);
|
|
53
|
+
}
|
|
54
|
+
async function executeDbVerifyCommand(options, flags) {
|
|
55
|
+
const config = await loadConfig(options.config);
|
|
56
|
+
const configPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
|
|
57
|
+
const contractPathAbsolute = config.contract?.output ? resolve(config.contract.output) : resolve("src/prisma/contract.json");
|
|
58
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
59
|
+
if (flags.json !== "object" && !flags.quiet) {
|
|
60
|
+
const details = [
|
|
61
|
+
{ label: "config", value: configPath },
|
|
62
|
+
{ label: "contract", value: contractPath }
|
|
63
|
+
];
|
|
64
|
+
if (options.db) {
|
|
65
|
+
details.push({ label: "database", value: options.db });
|
|
66
|
+
}
|
|
67
|
+
const header = formatStyledHeader({
|
|
68
|
+
command: "db verify",
|
|
69
|
+
description: "Check whether the database has been signed with your contract",
|
|
70
|
+
url: "https://pris.ly/db-verify",
|
|
71
|
+
details,
|
|
72
|
+
flags
|
|
73
|
+
});
|
|
74
|
+
console.log(header);
|
|
75
|
+
}
|
|
76
|
+
let contractJsonContent;
|
|
77
|
+
try {
|
|
78
|
+
contractJsonContent = await readFile(contractPathAbsolute, "utf-8");
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof Error && error.code === "ENOENT") {
|
|
81
|
+
return notOk(
|
|
82
|
+
errorFileNotFound(contractPathAbsolute, {
|
|
83
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
84
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return notOk(
|
|
89
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
90
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`
|
|
91
|
+
})
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
let contractJson;
|
|
95
|
+
try {
|
|
96
|
+
contractJson = JSON.parse(contractJsonContent);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return notOk(
|
|
99
|
+
errorContractValidationFailed(
|
|
100
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
101
|
+
{ where: { path: contractPathAbsolute } }
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
106
|
+
if (!dbConnection) {
|
|
107
|
+
return notOk(
|
|
108
|
+
errorDatabaseConnectionRequired({
|
|
109
|
+
why: `Database connection is required for db verify (set db.connection in ${configPath}, or pass --db <url>)`
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (!config.driver) {
|
|
114
|
+
return notOk(errorDriverRequired({ why: "Config.driver is required for db verify" }));
|
|
115
|
+
}
|
|
116
|
+
const client = createControlClient({
|
|
117
|
+
family: config.family,
|
|
118
|
+
target: config.target,
|
|
119
|
+
adapter: config.adapter,
|
|
120
|
+
driver: config.driver,
|
|
121
|
+
extensionPacks: config.extensionPacks ?? []
|
|
122
|
+
});
|
|
123
|
+
const onProgress = createProgressAdapter({ flags });
|
|
124
|
+
try {
|
|
125
|
+
const verifyResult = await client.verify({
|
|
126
|
+
contractIR: contractJson,
|
|
127
|
+
connection: dbConnection,
|
|
128
|
+
onProgress
|
|
129
|
+
});
|
|
130
|
+
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
131
|
+
console.log("");
|
|
132
|
+
}
|
|
133
|
+
if (!verifyResult.ok) {
|
|
134
|
+
return notOk(mapVerifyFailure(verifyResult));
|
|
135
|
+
}
|
|
136
|
+
return ok(verifyResult);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof CliStructuredError) {
|
|
139
|
+
return notOk(error);
|
|
140
|
+
}
|
|
141
|
+
return notOk(
|
|
142
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
143
|
+
why: `Unexpected error during db verify: ${error instanceof Error ? error.message : String(error)}`
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
} finally {
|
|
147
|
+
await client.close();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function createDbVerifyCommand() {
|
|
151
|
+
const command = new Command("verify");
|
|
152
|
+
setCommandDescriptions(
|
|
153
|
+
command,
|
|
154
|
+
"Check whether the database has been signed with your contract",
|
|
155
|
+
"Verifies that your database schema matches the emitted contract. Checks table structures,\ncolumn types, constraints, and codec coverage. Reports any mismatches or missing codecs."
|
|
156
|
+
);
|
|
157
|
+
command.configureHelp({
|
|
158
|
+
formatHelp: (cmd) => {
|
|
159
|
+
const flags = parseGlobalFlags({});
|
|
160
|
+
return formatCommandHelp({ command: cmd, flags });
|
|
161
|
+
}
|
|
162
|
+
}).option("--db <url>", "Database connection string").option("--config <path>", "Path to prisma-next.config.ts").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) => {
|
|
163
|
+
const flags = parseGlobalFlags(options);
|
|
164
|
+
if (flags.json === "ndjson") {
|
|
165
|
+
const result2 = notOk(
|
|
166
|
+
errorJsonFormatNotSupported({
|
|
167
|
+
command: "db verify",
|
|
168
|
+
format: "ndjson",
|
|
169
|
+
supportedFormats: ["object"]
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
const exitCode2 = handleResult(result2, flags);
|
|
173
|
+
process.exit(exitCode2);
|
|
174
|
+
}
|
|
175
|
+
const result = await executeDbVerifyCommand(options, flags);
|
|
176
|
+
const exitCode = handleResult(result, flags, (verifyResult) => {
|
|
177
|
+
if (flags.json === "object") {
|
|
178
|
+
console.log(formatVerifyJson(verifyResult));
|
|
179
|
+
} else {
|
|
180
|
+
const output = formatVerifyOutput(verifyResult, flags);
|
|
181
|
+
if (output) {
|
|
182
|
+
console.log(output);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
process.exit(exitCode);
|
|
187
|
+
});
|
|
188
|
+
return command;
|
|
189
|
+
}
|
|
190
|
+
export {
|
|
191
|
+
createDbVerifyCommand
|
|
192
|
+
};
|
|
193
|
+
//# sourceMappingURL=db-verify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/db-verify.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { relative, resolve } from 'node:path';\nimport type { VerifyDatabaseResult } from '@prisma-next/core-control-plane/types';\nimport { notOk, ok, type Result } from '@prisma-next/utils/result';\nimport { Command } from 'commander';\nimport { loadConfig } from '../config-loader';\nimport { createControlClient } from '../control-api/client';\nimport {\n CliStructuredError,\n errorContractValidationFailed,\n errorDatabaseConnectionRequired,\n errorDriverRequired,\n errorFileNotFound,\n errorHashMismatch,\n errorJsonFormatNotSupported,\n errorMarkerMissing,\n errorRuntime,\n errorTargetMismatch,\n errorUnexpected,\n} from '../utils/cli-errors';\nimport { setCommandDescriptions } from '../utils/command-helpers';\nimport { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';\nimport {\n formatCommandHelp,\n formatStyledHeader,\n formatVerifyJson,\n formatVerifyOutput,\n} from '../utils/output';\nimport { createProgressAdapter } from '../utils/progress-adapter';\nimport { handleResult } from '../utils/result-handler';\n\ninterface DbVerifyOptions {\n readonly db?: string;\n readonly config?: string;\n readonly json?: string | boolean;\n readonly quiet?: boolean;\n readonly q?: boolean;\n readonly verbose?: boolean;\n readonly v?: boolean;\n readonly vv?: boolean;\n readonly trace?: boolean;\n readonly timestamps?: boolean;\n readonly color?: boolean;\n readonly 'no-color'?: boolean;\n}\n\n/**\n * Maps a VerifyDatabaseResult failure to a CliStructuredError.\n */\nfunction mapVerifyFailure(verifyResult: VerifyDatabaseResult): CliStructuredError {\n if (!verifyResult.ok && verifyResult.code) {\n if (verifyResult.code === 'PN-RTM-3001') {\n return errorMarkerMissing();\n }\n if (verifyResult.code === 'PN-RTM-3002') {\n return errorHashMismatch({\n expected: verifyResult.contract.coreHash,\n ...(verifyResult.marker?.coreHash ? { actual: verifyResult.marker.coreHash } : {}),\n });\n }\n if (verifyResult.code === 'PN-RTM-3003') {\n return errorTargetMismatch(\n verifyResult.target.expected,\n verifyResult.target.actual ?? 'unknown',\n );\n }\n // Unknown code - fall through to runtime error\n }\n return errorRuntime(verifyResult.summary);\n}\n\n/**\n * Executes the db verify command and returns a structured Result.\n */\nasync function executeDbVerifyCommand(\n options: DbVerifyOptions,\n flags: GlobalFlags,\n): Promise<Result<VerifyDatabaseResult, CliStructuredError>> {\n // Load config\n const config = await loadConfig(options.config);\n const configPath = options.config\n ? relative(process.cwd(), resolve(options.config))\n : 'prisma-next.config.ts';\n const contractPathAbsolute = config.contract?.output\n ? resolve(config.contract.output)\n : resolve('src/prisma/contract.json');\n const contractPath = relative(process.cwd(), contractPathAbsolute);\n\n // Output header\n if (flags.json !== 'object' && !flags.quiet) {\n const details: Array<{ label: string; value: string }> = [\n { label: 'config', value: configPath },\n { label: 'contract', value: contractPath },\n ];\n if (options.db) {\n details.push({ label: 'database', value: options.db });\n }\n const header = formatStyledHeader({\n command: 'db verify',\n description: 'Check whether the database has been signed with your contract',\n url: 'https://pris.ly/db-verify',\n details,\n flags,\n });\n console.log(header);\n }\n\n // Load contract file\n let contractJsonContent: string;\n try {\n contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');\n } catch (error) {\n if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {\n return notOk(\n errorFileNotFound(contractPathAbsolute, {\n why: `Contract file not found at ${contractPathAbsolute}`,\n fix: `Run \\`prisma-next contract emit\\` to generate ${contractPath}, or update \\`config.contract.output\\` in ${configPath}`,\n }),\n );\n }\n return notOk(\n errorUnexpected(error instanceof Error ? error.message : String(error), {\n why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n }\n\n let contractJson: Record<string, unknown>;\n try {\n contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;\n } catch (error) {\n return notOk(\n errorContractValidationFailed(\n `Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,\n { where: { path: contractPathAbsolute } },\n ),\n );\n }\n\n // Resolve database connection (--db flag or config.db.connection)\n const dbConnection = options.db ?? config.db?.connection;\n if (!dbConnection) {\n return notOk(\n errorDatabaseConnectionRequired({\n why: `Database connection is required for db verify (set db.connection in ${configPath}, or pass --db <url>)`,\n }),\n );\n }\n\n // Check for driver\n if (!config.driver) {\n return notOk(errorDriverRequired({ why: 'Config.driver is required for db verify' }));\n }\n\n // Create control client\n const client = createControlClient({\n family: config.family,\n target: config.target,\n adapter: config.adapter,\n driver: config.driver,\n extensionPacks: config.extensionPacks ?? [],\n });\n\n // Create progress adapter\n const onProgress = createProgressAdapter({ flags });\n\n try {\n const verifyResult = await client.verify({\n contractIR: contractJson,\n connection: dbConnection,\n onProgress,\n });\n\n // Add blank line after all async operations if spinners were shown\n if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {\n console.log('');\n }\n\n // If verification failed, map to CLI structured error\n if (!verifyResult.ok) {\n return notOk(mapVerifyFailure(verifyResult));\n }\n\n return ok(verifyResult);\n } catch (error) {\n // Driver already throws CliStructuredError for connection failures\n if (error instanceof CliStructuredError) {\n return notOk(error);\n }\n\n // Wrap unexpected errors\n return notOk(\n errorUnexpected(error instanceof Error ? error.message : String(error), {\n why: `Unexpected error during db verify: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n } finally {\n await client.close();\n }\n}\n\nexport function createDbVerifyCommand(): Command {\n const command = new Command('verify');\n setCommandDescriptions(\n command,\n 'Check whether the database has been signed with your contract',\n 'Verifies that your database schema matches the emitted contract. Checks table structures,\\n' +\n 'column types, constraints, and codec coverage. Reports any mismatches or missing codecs.',\n );\n command\n .configureHelp({\n formatHelp: (cmd) => {\n const flags = parseGlobalFlags({});\n return formatCommandHelp({ command: cmd, flags });\n },\n })\n .option('--db <url>', 'Database connection string')\n .option('--config <path>', 'Path to prisma-next.config.ts')\n .option('--json [format]', 'Output as JSON (object)', false)\n .option('-q, --quiet', 'Quiet mode: errors only')\n .option('-v, --verbose', 'Verbose output: debug info, timings')\n .option('-vv, --trace', 'Trace output: deep internals, stack traces')\n .option('--timestamps', 'Add timestamps to output')\n .option('--color', 'Force color output')\n .option('--no-color', 'Disable color output')\n .action(async (options: DbVerifyOptions) => {\n const flags = parseGlobalFlags(options);\n\n // Validate JSON format option\n if (flags.json === 'ndjson') {\n const result = notOk(\n errorJsonFormatNotSupported({\n command: 'db verify',\n format: 'ndjson',\n supportedFormats: ['object'],\n }),\n );\n const exitCode = handleResult(result, flags);\n process.exit(exitCode);\n }\n\n const result = await executeDbVerifyCommand(options, flags);\n\n const exitCode = handleResult(result, flags, (verifyResult) => {\n if (flags.json === 'object') {\n console.log(formatVerifyJson(verifyResult));\n } else {\n const output = formatVerifyOutput(verifyResult, flags);\n if (output) {\n console.log(output);\n }\n }\n });\n process.exit(exitCode);\n });\n\n return command;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,UAAU,eAAe;AAElC,SAAS,OAAO,UAAuB;AACvC,SAAS,eAAe;AA6CxB,SAAS,iBAAiB,cAAwD;AAChF,MAAI,CAAC,aAAa,MA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