@prisma-next/cli 0.3.0-dev.2 → 0.3.0-dev.21
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 +111 -27
- package/dist/{chunk-BZMBKEEQ.js → chunk-AGOTG4L3.js} +44 -76
- 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-VG2R7DGF.js +735 -0
- package/dist/chunk-VG2R7DGF.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1502 -968
- package/dist/cli.js.map +1 -1
- package/dist/commands/contract-emit.d.ts +2 -4
- package/dist/commands/contract-emit.d.ts.map +1 -0
- package/dist/commands/contract-emit.js +3 -2
- package/dist/commands/db-init.d.ts +2 -4
- package/dist/commands/db-init.d.ts.map +1 -0
- package/dist/commands/db-init.js +205 -289
- package/dist/commands/db-init.js.map +1 -1
- package/dist/commands/db-introspect.d.ts +2 -4
- package/dist/commands/db-introspect.d.ts.map +1 -0
- package/dist/commands/db-introspect.js +108 -143
- package/dist/commands/db-introspect.js.map +1 -1
- package/dist/commands/db-schema-verify.d.ts +2 -4
- package/dist/commands/db-schema-verify.d.ts.map +1 -0
- package/dist/commands/db-schema-verify.js +120 -113
- package/dist/commands/db-schema-verify.js.map +1 -1
- package/dist/commands/db-sign.d.ts +2 -4
- package/dist/commands/db-sign.d.ts.map +1 -0
- package/dist/commands/db-sign.js +152 -156
- package/dist/commands/db-sign.js.map +1 -1
- package/dist/commands/db-verify.d.ts +2 -4
- package/dist/commands/db-verify.d.ts.map +1 -0
- package/dist/commands/db-verify.js +142 -122
- package/dist/commands/db-verify.js.map +1 -1
- package/dist/config-loader.d.ts +3 -5
- package/dist/config-loader.d.ts.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.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/{index.js → exports/index.js} +4 -3
- package/dist/exports/index.js.map +1 -0
- package/dist/{index.d.ts → load-ts-contract.d.ts} +4 -8
- package/dist/load-ts-contract.d.ts.map +1 -0
- package/dist/utils/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 +28 -25
- package/src/cli.ts +260 -0
- package/src/commands/contract-emit.ts +259 -0
- package/src/commands/db-init.ts +360 -0
- package/src/commands/db-introspect.ts +227 -0
- package/src/commands/db-schema-verify.ts +238 -0
- package/src/commands/db-sign.ts +279 -0
- package/src/commands/db-verify.ts +258 -0
- package/src/config-loader.ts +76 -0
- package/src/control-api/client.ts +589 -0
- package/src/control-api/operations/db-init.ts +281 -0
- package/src/control-api/types.ts +461 -0
- package/src/exports/config-types.ts +6 -0
- package/src/exports/control-api.ts +46 -0
- package/src/exports/index.ts +4 -0
- package/src/load-ts-contract.ts +217 -0
- package/src/utils/cli-errors.ts +26 -0
- package/src/utils/command-helpers.ts +26 -0
- package/src/utils/framework-components.ts +177 -0
- package/src/utils/global-flags.ts +75 -0
- package/src/utils/output.ts +1471 -0
- package/src/utils/progress-adapter.ts +86 -0
- package/src/utils/result-handler.ts +44 -0
- package/dist/chunk-464LNZCE.js +0 -134
- package/dist/chunk-464LNZCE.js.map +0 -1
- package/dist/chunk-BZMBKEEQ.js.map +0 -1
- package/dist/chunk-ZKYEJROM.js +0 -94
- package/dist/chunk-ZKYEJROM.js.map +0 -1
- package/dist/config-types.d.ts +0 -1
- package/dist/config-types.js.map +0 -1
- package/dist/index.js.map +0 -1
- /package/dist/{config-types.js → exports/config-types.js} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-introspect.d.ts","sourceRoot":"","sources":["../../src/commands/db-introspect.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAkKpC,wBAAgB,yBAAyB,IAAI,OAAO,CA4DnD"}
|
|
@@ -1,31 +1,112 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-ZKYEJROM.js";
|
|
4
|
-
import {
|
|
2
|
+
createProgressAdapter,
|
|
5
3
|
formatCommandHelp,
|
|
6
4
|
formatIntrospectJson,
|
|
7
5
|
formatIntrospectOutput,
|
|
8
6
|
formatStyledHeader,
|
|
9
7
|
handleResult,
|
|
10
8
|
parseGlobalFlags,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
withSpinner
|
|
14
|
-
} from "../chunk-BZMBKEEQ.js";
|
|
9
|
+
setCommandDescriptions
|
|
10
|
+
} from "../chunk-AGOTG4L3.js";
|
|
15
11
|
import {
|
|
16
12
|
loadConfig
|
|
17
13
|
} from "../chunk-HWYQOCAJ.js";
|
|
18
|
-
|
|
19
|
-
// src/commands/db-introspect.ts
|
|
20
|
-
import { readFile } from "fs/promises";
|
|
21
|
-
import { relative, resolve } from "path";
|
|
22
14
|
import {
|
|
23
|
-
|
|
15
|
+
CliStructuredError,
|
|
16
|
+
createControlClient,
|
|
17
|
+
errorDatabaseConnectionRequired,
|
|
24
18
|
errorDriverRequired,
|
|
25
|
-
|
|
19
|
+
errorJsonFormatNotSupported,
|
|
26
20
|
errorUnexpected
|
|
27
|
-
} from "
|
|
21
|
+
} from "../chunk-VG2R7DGF.js";
|
|
22
|
+
|
|
23
|
+
// src/commands/db-introspect.ts
|
|
24
|
+
import { relative, resolve } from "path";
|
|
25
|
+
import { notOk, ok } from "@prisma-next/utils/result";
|
|
28
26
|
import { Command } from "commander";
|
|
27
|
+
async function executeDbIntrospectCommand(options, flags, startTime) {
|
|
28
|
+
const config = await loadConfig(options.config);
|
|
29
|
+
const configPath = options.config ? relative(process.cwd(), resolve(options.config)) : "prisma-next.config.ts";
|
|
30
|
+
if (flags.json !== "object" && !flags.quiet) {
|
|
31
|
+
const details = [
|
|
32
|
+
{ label: "config", value: configPath }
|
|
33
|
+
];
|
|
34
|
+
if (options.db) {
|
|
35
|
+
const maskedUrl = options.db.replace(/:([^:@]+)@/, ":****@");
|
|
36
|
+
details.push({ label: "database", value: maskedUrl });
|
|
37
|
+
} else if (config.db?.connection && typeof config.db.connection === "string") {
|
|
38
|
+
const maskedUrl = config.db.connection.replace(/:([^:@]+)@/, ":****@");
|
|
39
|
+
details.push({ label: "database", value: maskedUrl });
|
|
40
|
+
}
|
|
41
|
+
const header = formatStyledHeader({
|
|
42
|
+
command: "db introspect",
|
|
43
|
+
description: "Inspect the database schema",
|
|
44
|
+
url: "https://pris.ly/db-introspect",
|
|
45
|
+
details,
|
|
46
|
+
flags
|
|
47
|
+
});
|
|
48
|
+
console.log(header);
|
|
49
|
+
}
|
|
50
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
51
|
+
if (!dbConnection) {
|
|
52
|
+
return notOk(
|
|
53
|
+
errorDatabaseConnectionRequired({
|
|
54
|
+
why: `Database connection is required for db introspect (set db.connection in ${configPath}, or pass --db <url>)`
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
if (!config.driver) {
|
|
59
|
+
return notOk(errorDriverRequired({ why: "Config.driver is required for db introspect" }));
|
|
60
|
+
}
|
|
61
|
+
const client = createControlClient({
|
|
62
|
+
family: config.family,
|
|
63
|
+
target: config.target,
|
|
64
|
+
adapter: config.adapter,
|
|
65
|
+
driver: config.driver,
|
|
66
|
+
extensionPacks: config.extensionPacks ?? []
|
|
67
|
+
});
|
|
68
|
+
const onProgress = createProgressAdapter({ flags });
|
|
69
|
+
try {
|
|
70
|
+
const schemaIR = await client.introspect({
|
|
71
|
+
connection: dbConnection,
|
|
72
|
+
onProgress
|
|
73
|
+
});
|
|
74
|
+
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
75
|
+
console.log("");
|
|
76
|
+
}
|
|
77
|
+
const schemaView = client.toSchemaView(schemaIR);
|
|
78
|
+
const totalTime = Date.now() - startTime;
|
|
79
|
+
const connectionForMeta = typeof dbConnection === "string" ? dbConnection.replace(/:([^:@]+)@/, ":****@") : void 0;
|
|
80
|
+
const introspectResult = {
|
|
81
|
+
ok: true,
|
|
82
|
+
summary: "Schema introspected successfully",
|
|
83
|
+
target: {
|
|
84
|
+
familyId: config.family.familyId,
|
|
85
|
+
id: config.target.targetId
|
|
86
|
+
},
|
|
87
|
+
schema: schemaIR,
|
|
88
|
+
meta: {
|
|
89
|
+
...configPath ? { configPath } : {},
|
|
90
|
+
...connectionForMeta ? { dbUrl: connectionForMeta } : {}
|
|
91
|
+
},
|
|
92
|
+
timings: {
|
|
93
|
+
total: totalTime
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return ok({ introspectResult, schemaView });
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if (error instanceof CliStructuredError) {
|
|
99
|
+
return notOk(error);
|
|
100
|
+
}
|
|
101
|
+
return notOk(
|
|
102
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
103
|
+
why: `Unexpected error during db introspect: ${error instanceof Error ? error.message : String(error)}`
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
} finally {
|
|
107
|
+
await client.close();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
29
110
|
function createDbIntrospectCommand() {
|
|
30
111
|
const command = new Command("introspect");
|
|
31
112
|
setCommandDescriptions(
|
|
@@ -38,137 +119,21 @@ function createDbIntrospectCommand() {
|
|
|
38
119
|
const flags = parseGlobalFlags({});
|
|
39
120
|
return formatCommandHelp({ command: cmd, flags });
|
|
40
121
|
}
|
|
41
|
-
}).option("--db <url>", "Database connection string").option("--config <path>", "Path to prisma-next.config.ts").option("--json [format]", "Output as JSON (object
|
|
122
|
+
}).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) => {
|
|
42
123
|
const flags = parseGlobalFlags(options);
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
let contractIR;
|
|
48
|
-
if (config.contract?.output) {
|
|
49
|
-
const contractPath = resolve(config.contract.output);
|
|
50
|
-
try {
|
|
51
|
-
const contractJsonContent = await readFile(contractPath, "utf-8");
|
|
52
|
-
contractIR = JSON.parse(contractJsonContent);
|
|
53
|
-
} catch (error) {
|
|
54
|
-
if (error instanceof Error && error.code !== "ENOENT") {
|
|
55
|
-
throw errorUnexpected(error.message, {
|
|
56
|
-
why: `Failed to read contract file: ${error.message}`
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
if (flags.json !== "object" && !flags.quiet) {
|
|
62
|
-
const details = [
|
|
63
|
-
{ label: "config", value: configPath }
|
|
64
|
-
];
|
|
65
|
-
if (options.db) {
|
|
66
|
-
const maskedUrl = options.db.replace(/:([^:@]+)@/, ":****@");
|
|
67
|
-
details.push({ label: "database", value: maskedUrl });
|
|
68
|
-
} else if (config.db?.url) {
|
|
69
|
-
const maskedUrl = config.db.url.replace(/:([^:@]+)@/, ":****@");
|
|
70
|
-
details.push({ label: "database", value: maskedUrl });
|
|
71
|
-
}
|
|
72
|
-
const header = formatStyledHeader({
|
|
124
|
+
const startTime = Date.now();
|
|
125
|
+
if (flags.json === "ndjson") {
|
|
126
|
+
const result2 = notOk(
|
|
127
|
+
errorJsonFormatNotSupported({
|
|
73
128
|
command: "db introspect",
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (!dbUrl) {
|
|
83
|
-
throw errorDatabaseUrlRequired();
|
|
84
|
-
}
|
|
85
|
-
if (!config.driver) {
|
|
86
|
-
throw errorDriverRequired();
|
|
87
|
-
}
|
|
88
|
-
const driverDescriptor = config.driver;
|
|
89
|
-
const driver = await withSpinner(() => driverDescriptor.create(dbUrl), {
|
|
90
|
-
message: "Connecting to database...",
|
|
91
|
-
flags
|
|
92
|
-
});
|
|
93
|
-
try {
|
|
94
|
-
const familyInstance = config.family.create({
|
|
95
|
-
target: config.target,
|
|
96
|
-
adapter: config.adapter,
|
|
97
|
-
driver: driverDescriptor,
|
|
98
|
-
extensionPacks: config.extensionPacks ?? []
|
|
99
|
-
});
|
|
100
|
-
if (contractIR) {
|
|
101
|
-
const validatedContract = familyInstance.validateContractIR(contractIR);
|
|
102
|
-
assertContractRequirementsSatisfied({
|
|
103
|
-
contract: validatedContract,
|
|
104
|
-
family: config.family,
|
|
105
|
-
target: config.target,
|
|
106
|
-
adapter: config.adapter,
|
|
107
|
-
extensionPacks: config.extensionPacks
|
|
108
|
-
});
|
|
109
|
-
contractIR = validatedContract;
|
|
110
|
-
}
|
|
111
|
-
let schemaIR;
|
|
112
|
-
try {
|
|
113
|
-
schemaIR = await withSpinner(
|
|
114
|
-
() => familyInstance.introspect({
|
|
115
|
-
driver,
|
|
116
|
-
contractIR
|
|
117
|
-
}),
|
|
118
|
-
{
|
|
119
|
-
message: "Introspecting database schema...",
|
|
120
|
-
flags
|
|
121
|
-
}
|
|
122
|
-
);
|
|
123
|
-
} catch (error) {
|
|
124
|
-
throw errorRuntime(error instanceof Error ? error.message : String(error), {
|
|
125
|
-
why: `Failed to introspect database: ${error instanceof Error ? error.message : String(error)}`
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
let schemaView;
|
|
129
|
-
if (familyInstance.toSchemaView) {
|
|
130
|
-
try {
|
|
131
|
-
schemaView = familyInstance.toSchemaView(schemaIR);
|
|
132
|
-
} catch (error) {
|
|
133
|
-
if (flags.verbose) {
|
|
134
|
-
console.error(
|
|
135
|
-
`Warning: Failed to project schema to view: ${error instanceof Error ? error.message : String(error)}`
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
const totalTime = Date.now() - startTime;
|
|
141
|
-
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
142
|
-
console.log("");
|
|
143
|
-
}
|
|
144
|
-
const introspectResult = {
|
|
145
|
-
ok: true,
|
|
146
|
-
summary: "Schema introspected successfully",
|
|
147
|
-
target: {
|
|
148
|
-
familyId: config.family.familyId,
|
|
149
|
-
id: config.target.targetId
|
|
150
|
-
},
|
|
151
|
-
schema: schemaIR,
|
|
152
|
-
...configPath || options.db || config.db?.url ? {
|
|
153
|
-
meta: {
|
|
154
|
-
...configPath ? { configPath } : {},
|
|
155
|
-
...options.db || config.db?.url ? {
|
|
156
|
-
dbUrl: (options.db ?? config.db?.url ?? "").replace(
|
|
157
|
-
/:([^:@]+)@/,
|
|
158
|
-
":****@"
|
|
159
|
-
)
|
|
160
|
-
} : {}
|
|
161
|
-
}
|
|
162
|
-
} : {},
|
|
163
|
-
timings: {
|
|
164
|
-
total: totalTime
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
return { introspectResult, schemaView };
|
|
168
|
-
} finally {
|
|
169
|
-
await driver.close();
|
|
170
|
-
}
|
|
171
|
-
});
|
|
129
|
+
format: "ndjson",
|
|
130
|
+
supportedFormats: ["object"]
|
|
131
|
+
})
|
|
132
|
+
);
|
|
133
|
+
const exitCode2 = handleResult(result2, flags);
|
|
134
|
+
process.exit(exitCode2);
|
|
135
|
+
}
|
|
136
|
+
const result = await executeDbIntrospectCommand(options, flags, startTime);
|
|
172
137
|
const exitCode = handleResult(result, flags, (value) => {
|
|
173
138
|
const { introspectResult, schemaView } = value;
|
|
174
139
|
if (flags.json === "object") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/commands/db-introspect.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises';\nimport { relative, resolve } from 'node:path';\nimport {\n errorDatabaseUrlRequired,\n errorDriverRequired,\n errorRuntime,\n errorUnexpected,\n} from '@prisma-next/core-control-plane/errors';\nimport type { CoreSchemaView } from '@prisma-next/core-control-plane/schema-view';\nimport type { IntrospectSchemaResult } from '@prisma-next/core-control-plane/types';\nimport { Command } from 'commander';\nimport { loadConfig } from '../config-loader';\nimport { performAction } from '../utils/action';\nimport { setCommandDescriptions } from '../utils/command-helpers';\nimport { assertContractRequirementsSatisfied } from '../utils/framework-components';\nimport { parseGlobalFlags } from '../utils/global-flags';\nimport {\n formatCommandHelp,\n formatIntrospectJson,\n formatIntrospectOutput,\n formatStyledHeader,\n} from '../utils/output';\nimport { handleResult } from '../utils/result-handler';\nimport { withSpinner } from '../utils/spinner';\n\ninterface DbIntrospectOptions {\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\nexport function createDbIntrospectCommand(): Command {\n const command = new Command('introspect');\n setCommandDescriptions(\n command,\n 'Inspect the database schema',\n 'Reads the live database schema and displays it as a tree structure. This command\\n' +\n 'does not check the schema against your contract - it only shows what exists in\\n' +\n 'the database. Use `db verify` or `db schema-verify` to compare against your contract.',\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 or ndjson)', 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: DbIntrospectOptions) => {\n const flags = parseGlobalFlags(options);\n\n const result = await performAction(async () => {\n const startTime = Date.now();\n\n // Load config (file I/O)\n const config = await loadConfig(options.config);\n // Normalize config path for display (match contract path format - no ./ prefix)\n const configPath = options.config\n ? relative(process.cwd(), resolve(options.config))\n : 'prisma-next.config.ts';\n\n // Optionally load contract if contract config exists\n let contractIR: unknown | undefined;\n if (config.contract?.output) {\n const contractPath = resolve(config.contract.output);\n try {\n const contractJsonContent = await readFile(contractPath, 'utf-8');\n contractIR = JSON.parse(contractJsonContent);\n } catch (error) {\n // Contract file is optional for introspection - don't fail if it doesn't exist\n if (error instanceof Error && (error as { code?: string }).code !== 'ENOENT') {\n throw errorUnexpected(error.message, {\n why: `Failed to read contract file: ${error.message}`,\n });\n }\n }\n }\n\n // Output header (only for human-readable output)\n if (flags.json !== 'object' && !flags.quiet) {\n const details: Array<{ label: string; value: string }> = [\n { label: 'config', value: configPath },\n ];\n if (options.db) {\n // Mask password in URL for security\n const maskedUrl = options.db.replace(/:([^:@]+)@/, ':****@');\n details.push({ label: 'database', value: maskedUrl });\n } else if (config.db?.url) {\n // Mask password in URL for security\n const maskedUrl = config.db.url.replace(/:([^:@]+)@/, ':****@');\n details.push({ label: 'database', value: maskedUrl });\n }\n const header = formatStyledHeader({\n command: 'db introspect',\n description: 'Inspect the database schema',\n url: 'https://pris.ly/db-introspect',\n details,\n flags,\n });\n console.log(header);\n }\n\n // Resolve database URL\n const dbUrl = options.db ?? config.db?.url;\n if (!dbUrl) {\n throw errorDatabaseUrlRequired();\n }\n\n // Check for driver\n if (!config.driver) {\n throw errorDriverRequired();\n }\n\n // Store driver descriptor after null check\n const driverDescriptor = config.driver;\n\n // Create driver\n const driver = await withSpinner(() => driverDescriptor.create(dbUrl), {\n message: 'Connecting to database...',\n flags,\n });\n\n try {\n // Create family instance\n const familyInstance = config.family.create({\n target: config.target,\n adapter: config.adapter,\n driver: driverDescriptor,\n extensionPacks: config.extensionPacks ?? [],\n });\n\n // Validate contract IR if we loaded it\n if (contractIR) {\n const validatedContract = familyInstance.validateContractIR(contractIR);\n assertContractRequirementsSatisfied({\n contract: validatedContract,\n family: config.family,\n target: config.target,\n adapter: config.adapter,\n extensionPacks: config.extensionPacks,\n });\n contractIR = validatedContract;\n }\n\n // Call family instance introspect method\n let schemaIR: unknown;\n try {\n schemaIR = await withSpinner(\n () =>\n familyInstance.introspect({\n driver,\n contractIR,\n }),\n {\n message: 'Introspecting database schema...',\n flags,\n },\n );\n } catch (error) {\n // Wrap errors from introspect() in structured error\n throw errorRuntime(error instanceof Error ? error.message : String(error), {\n why: `Failed to introspect database: ${error instanceof Error ? error.message : String(error)}`,\n });\n }\n\n // Optionally call toSchemaView if available\n let schemaView: CoreSchemaView | undefined;\n if (familyInstance.toSchemaView) {\n try {\n schemaView = familyInstance.toSchemaView(schemaIR);\n } catch (error) {\n // Schema view projection is optional - log but don't fail\n if (flags.verbose) {\n console.error(\n `Warning: Failed to project schema to view: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n }\n\n const totalTime = Date.now() - startTime;\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 // Build result envelope\n const introspectResult: IntrospectSchemaResult<unknown> = {\n ok: true,\n summary: 'Schema introspected successfully',\n target: {\n familyId: config.family.familyId,\n id: config.target.targetId,\n },\n schema: schemaIR,\n ...(configPath || options.db || config.db?.url\n ? {\n meta: {\n ...(configPath ? { configPath } : {}),\n ...(options.db || config.db?.url\n ? {\n dbUrl: (options.db ?? config.db?.url ?? '').replace(\n /:([^:@]+)@/,\n ':****@',\n ),\n }\n : {}),\n },\n }\n : {}),\n timings: {\n total: totalTime,\n },\n };\n\n return { introspectResult, schemaView };\n } finally {\n // Ensure driver connection is closed\n await driver.close();\n }\n });\n\n // Handle result - formats output and returns exit code\n const exitCode = handleResult(result, flags, (value) => {\n const { introspectResult, schemaView } = value;\n // Output based on flags\n if (flags.json === 'object') {\n // JSON output to stdout\n console.log(formatIntrospectJson(introspectResult));\n } else {\n // Human-readable output to stdout\n const output = formatIntrospectOutput(introspectResult, schemaView, 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;AAClC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,SAAS,eAAe;AA8BjB,SAAS,4BAAqC;AACnD,QAAM,UAAU,IAAI,QAAQ,YAAY;AACxC;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EAGF;AACA,UACG,cAAc;AAAA,IACb,YAAY,CAAC,QAAQ;AACnB,YAAM,QAAQ,iBAAiB,CAAC,CAAC;AACjC,aAAO,kBAAkB,EAAE,SAAS,KAAK,MAAM,CAAC;AAAA,IAClD;AAAA,EACF,CAAC,EACA,OAAO,cAAc,4BAA4B,EACjD,OAAO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gB,CAAC;AAAA,MACpD,OAAO;AAEL,cAAM,SAAS,uBAAuB,kBAAkB,YAAY,KAAK;AACzE,YAAI,QAAQ;AACV,kBAAQ,IAAI,MAAM;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AACD,YAAQ,KAAK,QAAQ;AAAA,EACvB,CAAC;AAEH,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/commands/db-introspect.ts"],"sourcesContent":["import { relative, resolve } from 'node:path';\nimport type { CoreSchemaView } from '@prisma-next/core-control-plane/schema-view';\nimport type { IntrospectSchemaResult } 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 errorDatabaseConnectionRequired,\n errorDriverRequired,\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 formatIntrospectJson,\n formatIntrospectOutput,\n formatStyledHeader,\n} from '../utils/output';\nimport { createProgressAdapter } from '../utils/progress-adapter';\nimport { handleResult } from '../utils/result-handler';\n\ninterface DbIntrospectOptions {\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\ninterface DbIntrospectCommandResult {\n readonly introspectResult: IntrospectSchemaResult<unknown>;\n readonly schemaView: CoreSchemaView | undefined;\n}\n\n/**\n * Executes the db introspect command and returns a structured Result.\n */\nasync function executeDbIntrospectCommand(\n options: DbIntrospectOptions,\n flags: GlobalFlags,\n startTime: number,\n): Promise<Result<DbIntrospectCommandResult, 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\n // Output header\n if (flags.json !== 'object' && !flags.quiet) {\n const details: Array<{ label: string; value: string }> = [\n { label: 'config', value: configPath },\n ];\n if (options.db) {\n // Mask password in URL for security\n const maskedUrl = options.db.replace(/:([^:@]+)@/, ':****@');\n details.push({ label: 'database', value: maskedUrl });\n } else if (config.db?.connection && typeof config.db.connection === 'string') {\n // Mask password in URL for security\n const maskedUrl = config.db.connection.replace(/:([^:@]+)@/, ':****@');\n details.push({ label: 'database', value: maskedUrl });\n }\n const header = formatStyledHeader({\n command: 'db introspect',\n description: 'Inspect the database schema',\n url: 'https://pris.ly/db-introspect',\n details,\n flags,\n });\n console.log(header);\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 introspect (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 introspect' }));\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 // Introspect with connection and progress\n const schemaIR = await client.introspect({\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 // Call toSchemaView to convert schema IR to CoreSchemaView for tree rendering\n const schemaView = client.toSchemaView(schemaIR);\n\n const totalTime = Date.now() - startTime;\n\n // Get masked connection URL for meta (only for string connections)\n const connectionForMeta =\n typeof dbConnection === 'string' ? dbConnection.replace(/:([^:@]+)@/, ':****@') : undefined;\n\n const introspectResult: IntrospectSchemaResult<unknown> = {\n ok: true,\n summary: 'Schema introspected successfully',\n target: {\n familyId: config.family.familyId,\n id: config.target.targetId,\n },\n schema: schemaIR,\n meta: {\n ...(configPath ? { configPath } : {}),\n ...(connectionForMeta ? { dbUrl: connectionForMeta } : {}),\n },\n timings: {\n total: totalTime,\n },\n };\n\n return ok({ introspectResult, schemaView });\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 introspect: ${error instanceof Error ? error.message : String(error)}`,\n }),\n );\n } finally {\n await client.close();\n }\n}\n\nexport function createDbIntrospectCommand(): Command {\n const command = new Command('introspect');\n setCommandDescriptions(\n command,\n 'Inspect the database schema',\n 'Reads the live database schema and displays it as a tree structure. This command\\n' +\n 'does not check the schema against your contract - it only shows what exists in\\n' +\n 'the database. Use `db verify` or `db schema-verify` to compare against your contract.',\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: DbIntrospectOptions) => {\n const flags = parseGlobalFlags(options);\n const startTime = Date.now();\n\n // Validate JSON format option\n if (flags.json === 'ndjson') {\n const result = notOk(\n errorJsonFormatNotSupported({\n command: 'db introspect',\n format: 'ndjson',\n supportedFormats: ['object'],\n }),\n );\n const exitCode = handleResult(result, flags);\n process.exit(exitCode);\n }\n\n const result = await executeDbIntrospectCommand(options, flags, startTime);\n\n // Handle result - formats output and returns exit code\n const exitCode = handleResult(result, flags, (value) => {\n const { introspectResult, schemaView } = value;\n if (flags.json === 'object') {\n console.log(formatIntrospectJson(introspectResult));\n } else {\n const output = formatIntrospectOutput(introspectResult, schemaView, 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,UAAU,eAAe;AAGlC,SAAS,OAAO,UAAuB;AACvC,SAAS,eAAe;AA4CxB,eAAe,2BACb,SACA,OACA,WACgE;AAEhE,QAAM,SAAS,MAAM,WAAW,QAAQ,MAAM;AAC9C,QAAM,aAAa,QAAQ,SACvB,SAAS,QAAQ,IAAI,GAAG,QAAQ,QAAQ,MAAM,CAAC,IAC/C;AAGJ,MAAI,MAAM,SAAS,YAAY,CAAC,MAAM,OAAO;AAC3C,UAAM,UAAmD;AAAA,MACvD,EAAE,OAAO,UAAU,OAAO,WAAW;AAAA,IACvC;AACA,QAAI,QAAQ,IAAI;AAEd,YAAM,YAAY,QAAQ,GAAG,QAAQ,cAAc,QAAQ;AAC3D,cAAQ,KAAK,EAAE,OAAO,YAAY,OAAO,UAAU,CAAC;AAAA,IACtD,WAAW,OAAO,IAAI,cAAc,OAAO,OAAO,GAAG,eAAe,UAAU;AAE5E,YAAM,YAAY,OAAO,GAAG,WAAW,QAAQ,cAAc,QAAQ;AACrE,cAAQ,KAAK,EAAE,OAAO,YAAY,OAAO,UAAU,CAAC;AAAA,IACtD;AACA,UAAM,SAAS,mBAAmB;AAAA,MAChC,SAAS;AAAA,MACT,aAAa;AAAA,MACb,KAAK;AAAA,MACL;AAAA,MACA;AAAA,IACF,CAAC;AACD,YAAQ,IAAI,MAAM;AAAA,EACpB;AAGA,QAAM,eAAe,QAAQ,MAAM,OAAO,IAAI;AAC9C,MAAI,CAAC,cAAc;AACjB,WAAO;AAAA,MACL,gCAAgC;AAAA,QAC9B,KAAK,2EAA2E,UAAU;AAAA,MAC5F,CAAC;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UAAU;AAC3B,gBAAQ,IAAI,qBAAqB,gBAAgB,CAAC;AAAA,MACpD,OAAO;AACL,cAAM,SAAS,uBAAuB,kBAAkB,YAAY,KAAK;AACzE,YAAI,QAAQ;AACV,kBAAQ,IAAI,MAAM;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AACD,YAAQ,KAAK,QAAQ;AAAA,EACvB,CAAC;AAEH,SAAO;AACT;","names":["result","exitCode"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-schema-verify.d.ts","sourceRoot":"","sources":["../../src/commands/db-schema-verify.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAqKpC,wBAAgB,2BAA2B,IAAI,OAAO,CAoErD"}
|
|
@@ -1,33 +1,126 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
assertFrameworkComponentsCompatible
|
|
4
|
-
} from "../chunk-ZKYEJROM.js";
|
|
5
|
-
import {
|
|
2
|
+
createProgressAdapter,
|
|
6
3
|
formatCommandHelp,
|
|
7
4
|
formatSchemaVerifyJson,
|
|
8
5
|
formatSchemaVerifyOutput,
|
|
9
6
|
formatStyledHeader,
|
|
10
7
|
handleResult,
|
|
11
8
|
parseGlobalFlags,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
withSpinner
|
|
15
|
-
} from "../chunk-BZMBKEEQ.js";
|
|
9
|
+
setCommandDescriptions
|
|
10
|
+
} from "../chunk-AGOTG4L3.js";
|
|
16
11
|
import {
|
|
17
12
|
loadConfig
|
|
18
13
|
} from "../chunk-HWYQOCAJ.js";
|
|
19
|
-
|
|
20
|
-
// src/commands/db-schema-verify.ts
|
|
21
|
-
import { readFile } from "fs/promises";
|
|
22
|
-
import { relative, resolve } from "path";
|
|
23
14
|
import {
|
|
24
|
-
|
|
15
|
+
CliStructuredError,
|
|
16
|
+
createControlClient,
|
|
17
|
+
errorContractValidationFailed,
|
|
18
|
+
errorDatabaseConnectionRequired,
|
|
25
19
|
errorDriverRequired,
|
|
26
20
|
errorFileNotFound,
|
|
27
|
-
|
|
21
|
+
errorJsonFormatNotSupported,
|
|
28
22
|
errorUnexpected
|
|
29
|
-
} from "
|
|
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";
|
|
30
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
|
+
}
|
|
31
124
|
function createDbSchemaVerifyCommand() {
|
|
32
125
|
const command = new Command("schema-verify");
|
|
33
126
|
setCommandDescriptions(
|
|
@@ -40,106 +133,20 @@ function createDbSchemaVerifyCommand() {
|
|
|
40
133
|
const flags = parseGlobalFlags({});
|
|
41
134
|
return formatCommandHelp({ command: cmd, flags });
|
|
42
135
|
}
|
|
43
|
-
}).option("--db <url>", "Database connection string").option("--config <path>", "Path to prisma-next.config.ts").option("--json [format]", "Output as JSON (object
|
|
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) => {
|
|
44
137
|
const flags = parseGlobalFlags(options);
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
const contractPathAbsolute = config.contract?.output ? resolve(config.contract.output) : resolve("src/prisma/contract.json");
|
|
49
|
-
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
50
|
-
if (flags.json !== "object" && !flags.quiet) {
|
|
51
|
-
const details = [
|
|
52
|
-
{ label: "config", value: configPath },
|
|
53
|
-
{ label: "contract", value: contractPath }
|
|
54
|
-
];
|
|
55
|
-
if (options.db) {
|
|
56
|
-
details.push({ label: "database", value: options.db });
|
|
57
|
-
}
|
|
58
|
-
const header = formatStyledHeader({
|
|
138
|
+
if (flags.json === "ndjson") {
|
|
139
|
+
const result2 = notOk(
|
|
140
|
+
errorJsonFormatNotSupported({
|
|
59
141
|
command: "db schema-verify",
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
contractJsonContent = await readFile(contractPathAbsolute, "utf-8");
|
|
70
|
-
} catch (error) {
|
|
71
|
-
if (error instanceof Error && error.code === "ENOENT") {
|
|
72
|
-
throw errorFileNotFound(contractPathAbsolute, {
|
|
73
|
-
why: `Contract file not found at ${contractPathAbsolute}`
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
throw errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
77
|
-
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
const contractJson = JSON.parse(contractJsonContent);
|
|
81
|
-
if (!config.driver) {
|
|
82
|
-
throw errorDriverRequired();
|
|
83
|
-
}
|
|
84
|
-
const driverDescriptor = config.driver;
|
|
85
|
-
const familyInstance = config.family.create({
|
|
86
|
-
target: config.target,
|
|
87
|
-
adapter: config.adapter,
|
|
88
|
-
driver: driverDescriptor,
|
|
89
|
-
extensionPacks: config.extensionPacks ?? []
|
|
90
|
-
});
|
|
91
|
-
const contractIR = familyInstance.validateContractIR(contractJson);
|
|
92
|
-
assertContractRequirementsSatisfied({
|
|
93
|
-
contract: contractIR,
|
|
94
|
-
family: config.family,
|
|
95
|
-
target: config.target,
|
|
96
|
-
adapter: config.adapter,
|
|
97
|
-
extensionPacks: config.extensionPacks
|
|
98
|
-
});
|
|
99
|
-
const dbUrl = options.db ?? config.db?.url;
|
|
100
|
-
if (!dbUrl) {
|
|
101
|
-
throw errorDatabaseUrlRequired();
|
|
102
|
-
}
|
|
103
|
-
const driver = await withSpinner(() => driverDescriptor.create(dbUrl), {
|
|
104
|
-
message: "Connecting to database...",
|
|
105
|
-
flags
|
|
106
|
-
});
|
|
107
|
-
try {
|
|
108
|
-
const rawComponents = [config.target, config.adapter, ...config.extensionPacks ?? []];
|
|
109
|
-
const frameworkComponents = assertFrameworkComponentsCompatible(
|
|
110
|
-
config.family.familyId,
|
|
111
|
-
config.target.targetId,
|
|
112
|
-
rawComponents
|
|
113
|
-
);
|
|
114
|
-
let schemaVerifyResult;
|
|
115
|
-
try {
|
|
116
|
-
schemaVerifyResult = await withSpinner(
|
|
117
|
-
() => familyInstance.schemaVerify({
|
|
118
|
-
driver,
|
|
119
|
-
contractIR,
|
|
120
|
-
strict: options.strict ?? false,
|
|
121
|
-
contractPath: contractPathAbsolute,
|
|
122
|
-
configPath,
|
|
123
|
-
frameworkComponents
|
|
124
|
-
}),
|
|
125
|
-
{
|
|
126
|
-
message: "Verifying database schema...",
|
|
127
|
-
flags
|
|
128
|
-
}
|
|
129
|
-
);
|
|
130
|
-
} catch (error) {
|
|
131
|
-
throw errorRuntime(error instanceof Error ? error.message : String(error), {
|
|
132
|
-
why: `Failed to verify database schema: ${error instanceof Error ? error.message : String(error)}`
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
if (!flags.quiet && flags.json !== "object" && process.stdout.isTTY) {
|
|
136
|
-
console.log("");
|
|
137
|
-
}
|
|
138
|
-
return schemaVerifyResult;
|
|
139
|
-
} finally {
|
|
140
|
-
await driver.close();
|
|
141
|
-
}
|
|
142
|
-
});
|
|
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);
|
|
143
150
|
const exitCode = handleResult(result, flags, (schemaVerifyResult) => {
|
|
144
151
|
if (flags.json === "object") {
|
|
145
152
|
console.log(formatSchemaVerifyJson(schemaVerifyResult));
|