@prisma-next/cli 0.3.0-dev.3 → 0.3.0-dev.30
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 +30 -26
- 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,227 @@
|
|
|
1
|
+
import { relative, resolve } from 'node:path';
|
|
2
|
+
import type { CoreSchemaView } from '@prisma-next/core-control-plane/schema-view';
|
|
3
|
+
import type { IntrospectSchemaResult } from '@prisma-next/core-control-plane/types';
|
|
4
|
+
import { notOk, ok, type Result } from '@prisma-next/utils/result';
|
|
5
|
+
import { Command } from 'commander';
|
|
6
|
+
import { loadConfig } from '../config-loader';
|
|
7
|
+
import { createControlClient } from '../control-api/client';
|
|
8
|
+
import {
|
|
9
|
+
CliStructuredError,
|
|
10
|
+
errorDatabaseConnectionRequired,
|
|
11
|
+
errorDriverRequired,
|
|
12
|
+
errorJsonFormatNotSupported,
|
|
13
|
+
errorUnexpected,
|
|
14
|
+
} from '../utils/cli-errors';
|
|
15
|
+
import { setCommandDescriptions } from '../utils/command-helpers';
|
|
16
|
+
import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
|
|
17
|
+
import {
|
|
18
|
+
formatCommandHelp,
|
|
19
|
+
formatIntrospectJson,
|
|
20
|
+
formatIntrospectOutput,
|
|
21
|
+
formatStyledHeader,
|
|
22
|
+
} from '../utils/output';
|
|
23
|
+
import { createProgressAdapter } from '../utils/progress-adapter';
|
|
24
|
+
import { handleResult } from '../utils/result-handler';
|
|
25
|
+
|
|
26
|
+
interface DbIntrospectOptions {
|
|
27
|
+
readonly db?: string;
|
|
28
|
+
readonly config?: string;
|
|
29
|
+
readonly json?: string | boolean;
|
|
30
|
+
readonly quiet?: boolean;
|
|
31
|
+
readonly q?: boolean;
|
|
32
|
+
readonly verbose?: boolean;
|
|
33
|
+
readonly v?: boolean;
|
|
34
|
+
readonly vv?: boolean;
|
|
35
|
+
readonly trace?: boolean;
|
|
36
|
+
readonly timestamps?: boolean;
|
|
37
|
+
readonly color?: boolean;
|
|
38
|
+
readonly 'no-color'?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface DbIntrospectCommandResult {
|
|
42
|
+
readonly introspectResult: IntrospectSchemaResult<unknown>;
|
|
43
|
+
readonly schemaView: CoreSchemaView | undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Executes the db introspect command and returns a structured Result.
|
|
48
|
+
*/
|
|
49
|
+
async function executeDbIntrospectCommand(
|
|
50
|
+
options: DbIntrospectOptions,
|
|
51
|
+
flags: GlobalFlags,
|
|
52
|
+
startTime: number,
|
|
53
|
+
): Promise<Result<DbIntrospectCommandResult, CliStructuredError>> {
|
|
54
|
+
// Load config
|
|
55
|
+
const config = await loadConfig(options.config);
|
|
56
|
+
const configPath = options.config
|
|
57
|
+
? relative(process.cwd(), resolve(options.config))
|
|
58
|
+
: 'prisma-next.config.ts';
|
|
59
|
+
|
|
60
|
+
// Output header
|
|
61
|
+
if (flags.json !== 'object' && !flags.quiet) {
|
|
62
|
+
const details: Array<{ label: string; value: string }> = [
|
|
63
|
+
{ label: 'config', value: configPath },
|
|
64
|
+
];
|
|
65
|
+
if (options.db) {
|
|
66
|
+
// Mask password in URL for security
|
|
67
|
+
const maskedUrl = options.db.replace(/:([^:@]+)@/, ':****@');
|
|
68
|
+
details.push({ label: 'database', value: maskedUrl });
|
|
69
|
+
} else if (config.db?.connection && typeof config.db.connection === 'string') {
|
|
70
|
+
// Mask password in URL for security
|
|
71
|
+
const maskedUrl = config.db.connection.replace(/:([^:@]+)@/, ':****@');
|
|
72
|
+
details.push({ label: 'database', value: maskedUrl });
|
|
73
|
+
}
|
|
74
|
+
const header = formatStyledHeader({
|
|
75
|
+
command: 'db introspect',
|
|
76
|
+
description: 'Inspect the database schema',
|
|
77
|
+
url: 'https://pris.ly/db-introspect',
|
|
78
|
+
details,
|
|
79
|
+
flags,
|
|
80
|
+
});
|
|
81
|
+
console.log(header);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Resolve database connection (--db flag or config.db.connection)
|
|
85
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
86
|
+
if (!dbConnection) {
|
|
87
|
+
return notOk(
|
|
88
|
+
errorDatabaseConnectionRequired({
|
|
89
|
+
why: `Database connection is required for db introspect (set db.connection in ${configPath}, or pass --db <url>)`,
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Check for driver
|
|
95
|
+
if (!config.driver) {
|
|
96
|
+
return notOk(errorDriverRequired({ why: 'Config.driver is required for db introspect' }));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Create control client
|
|
100
|
+
const client = createControlClient({
|
|
101
|
+
family: config.family,
|
|
102
|
+
target: config.target,
|
|
103
|
+
adapter: config.adapter,
|
|
104
|
+
driver: config.driver,
|
|
105
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Create progress adapter
|
|
109
|
+
const onProgress = createProgressAdapter({ flags });
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
// Introspect with connection and progress
|
|
113
|
+
const schemaIR = await client.introspect({
|
|
114
|
+
connection: dbConnection,
|
|
115
|
+
onProgress,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Add blank line after all async operations if spinners were shown
|
|
119
|
+
if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
|
|
120
|
+
console.log('');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Call toSchemaView to convert schema IR to CoreSchemaView for tree rendering
|
|
124
|
+
const schemaView = client.toSchemaView(schemaIR);
|
|
125
|
+
|
|
126
|
+
const totalTime = Date.now() - startTime;
|
|
127
|
+
|
|
128
|
+
// Get masked connection URL for meta (only for string connections)
|
|
129
|
+
const connectionForMeta =
|
|
130
|
+
typeof dbConnection === 'string' ? dbConnection.replace(/:([^:@]+)@/, ':****@') : undefined;
|
|
131
|
+
|
|
132
|
+
const introspectResult: IntrospectSchemaResult<unknown> = {
|
|
133
|
+
ok: true,
|
|
134
|
+
summary: 'Schema introspected successfully',
|
|
135
|
+
target: {
|
|
136
|
+
familyId: config.family.familyId,
|
|
137
|
+
id: config.target.targetId,
|
|
138
|
+
},
|
|
139
|
+
schema: schemaIR,
|
|
140
|
+
meta: {
|
|
141
|
+
...(configPath ? { configPath } : {}),
|
|
142
|
+
...(connectionForMeta ? { dbUrl: connectionForMeta } : {}),
|
|
143
|
+
},
|
|
144
|
+
timings: {
|
|
145
|
+
total: totalTime,
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return ok({ introspectResult, schemaView });
|
|
150
|
+
} catch (error) {
|
|
151
|
+
// Driver already throws CliStructuredError for connection failures
|
|
152
|
+
if (error instanceof CliStructuredError) {
|
|
153
|
+
return notOk(error);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Wrap unexpected errors
|
|
157
|
+
return notOk(
|
|
158
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
159
|
+
why: `Unexpected error during db introspect: ${error instanceof Error ? error.message : String(error)}`,
|
|
160
|
+
}),
|
|
161
|
+
);
|
|
162
|
+
} finally {
|
|
163
|
+
await client.close();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function createDbIntrospectCommand(): Command {
|
|
168
|
+
const command = new Command('introspect');
|
|
169
|
+
setCommandDescriptions(
|
|
170
|
+
command,
|
|
171
|
+
'Inspect the database schema',
|
|
172
|
+
'Reads the live database schema and displays it as a tree structure. This command\n' +
|
|
173
|
+
'does not check the schema against your contract - it only shows what exists in\n' +
|
|
174
|
+
'the database. Use `db verify` or `db schema-verify` to compare against your contract.',
|
|
175
|
+
);
|
|
176
|
+
command
|
|
177
|
+
.configureHelp({
|
|
178
|
+
formatHelp: (cmd) => {
|
|
179
|
+
const flags = parseGlobalFlags({});
|
|
180
|
+
return formatCommandHelp({ command: cmd, flags });
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
.option('--db <url>', 'Database connection string')
|
|
184
|
+
.option('--config <path>', 'Path to prisma-next.config.ts')
|
|
185
|
+
.option('--json [format]', 'Output as JSON (object)', false)
|
|
186
|
+
.option('-q, --quiet', 'Quiet mode: errors only')
|
|
187
|
+
.option('-v, --verbose', 'Verbose output: debug info, timings')
|
|
188
|
+
.option('-vv, --trace', 'Trace output: deep internals, stack traces')
|
|
189
|
+
.option('--timestamps', 'Add timestamps to output')
|
|
190
|
+
.option('--color', 'Force color output')
|
|
191
|
+
.option('--no-color', 'Disable color output')
|
|
192
|
+
.action(async (options: DbIntrospectOptions) => {
|
|
193
|
+
const flags = parseGlobalFlags(options);
|
|
194
|
+
const startTime = Date.now();
|
|
195
|
+
|
|
196
|
+
// Validate JSON format option
|
|
197
|
+
if (flags.json === 'ndjson') {
|
|
198
|
+
const result = notOk(
|
|
199
|
+
errorJsonFormatNotSupported({
|
|
200
|
+
command: 'db introspect',
|
|
201
|
+
format: 'ndjson',
|
|
202
|
+
supportedFormats: ['object'],
|
|
203
|
+
}),
|
|
204
|
+
);
|
|
205
|
+
const exitCode = handleResult(result, flags);
|
|
206
|
+
process.exit(exitCode);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const result = await executeDbIntrospectCommand(options, flags, startTime);
|
|
210
|
+
|
|
211
|
+
// Handle result - formats output and returns exit code
|
|
212
|
+
const exitCode = handleResult(result, flags, (value) => {
|
|
213
|
+
const { introspectResult, schemaView } = value;
|
|
214
|
+
if (flags.json === 'object') {
|
|
215
|
+
console.log(formatIntrospectJson(introspectResult));
|
|
216
|
+
} else {
|
|
217
|
+
const output = formatIntrospectOutput(introspectResult, schemaView, flags);
|
|
218
|
+
if (output) {
|
|
219
|
+
console.log(output);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
process.exit(exitCode);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
return command;
|
|
227
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { relative, resolve } from 'node:path';
|
|
3
|
+
import type { VerifyDatabaseSchemaResult } from '@prisma-next/core-control-plane/types';
|
|
4
|
+
import { notOk, ok, type Result } from '@prisma-next/utils/result';
|
|
5
|
+
import { Command } from 'commander';
|
|
6
|
+
import { loadConfig } from '../config-loader';
|
|
7
|
+
import { createControlClient } from '../control-api/client';
|
|
8
|
+
import {
|
|
9
|
+
CliStructuredError,
|
|
10
|
+
errorContractValidationFailed,
|
|
11
|
+
errorDatabaseConnectionRequired,
|
|
12
|
+
errorDriverRequired,
|
|
13
|
+
errorFileNotFound,
|
|
14
|
+
errorJsonFormatNotSupported,
|
|
15
|
+
errorUnexpected,
|
|
16
|
+
} from '../utils/cli-errors';
|
|
17
|
+
import { setCommandDescriptions } from '../utils/command-helpers';
|
|
18
|
+
import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
|
|
19
|
+
import {
|
|
20
|
+
formatCommandHelp,
|
|
21
|
+
formatSchemaVerifyJson,
|
|
22
|
+
formatSchemaVerifyOutput,
|
|
23
|
+
formatStyledHeader,
|
|
24
|
+
} from '../utils/output';
|
|
25
|
+
import { createProgressAdapter } from '../utils/progress-adapter';
|
|
26
|
+
import { handleResult } from '../utils/result-handler';
|
|
27
|
+
|
|
28
|
+
interface DbSchemaVerifyOptions {
|
|
29
|
+
readonly db?: string;
|
|
30
|
+
readonly config?: string;
|
|
31
|
+
readonly json?: string | boolean;
|
|
32
|
+
readonly strict?: boolean;
|
|
33
|
+
readonly quiet?: boolean;
|
|
34
|
+
readonly q?: boolean;
|
|
35
|
+
readonly verbose?: boolean;
|
|
36
|
+
readonly v?: boolean;
|
|
37
|
+
readonly vv?: boolean;
|
|
38
|
+
readonly trace?: boolean;
|
|
39
|
+
readonly timestamps?: boolean;
|
|
40
|
+
readonly color?: boolean;
|
|
41
|
+
readonly 'no-color'?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Executes the db schema-verify command and returns a structured Result.
|
|
46
|
+
*/
|
|
47
|
+
async function executeDbSchemaVerifyCommand(
|
|
48
|
+
options: DbSchemaVerifyOptions,
|
|
49
|
+
flags: GlobalFlags,
|
|
50
|
+
): Promise<Result<VerifyDatabaseSchemaResult, CliStructuredError>> {
|
|
51
|
+
// Load config
|
|
52
|
+
const config = await loadConfig(options.config);
|
|
53
|
+
const configPath = options.config
|
|
54
|
+
? relative(process.cwd(), resolve(options.config))
|
|
55
|
+
: 'prisma-next.config.ts';
|
|
56
|
+
const contractPathAbsolute = config.contract?.output
|
|
57
|
+
? resolve(config.contract.output)
|
|
58
|
+
: resolve('src/prisma/contract.json');
|
|
59
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
60
|
+
|
|
61
|
+
// Output header
|
|
62
|
+
if (flags.json !== 'object' && !flags.quiet) {
|
|
63
|
+
const details: Array<{ label: string; value: string }> = [
|
|
64
|
+
{ label: 'config', value: configPath },
|
|
65
|
+
{ label: 'contract', value: contractPath },
|
|
66
|
+
];
|
|
67
|
+
if (options.db) {
|
|
68
|
+
details.push({ label: 'database', value: options.db });
|
|
69
|
+
}
|
|
70
|
+
const header = formatStyledHeader({
|
|
71
|
+
command: 'db schema-verify',
|
|
72
|
+
description: 'Check whether the database schema satisfies your contract',
|
|
73
|
+
url: 'https://pris.ly/db-schema-verify',
|
|
74
|
+
details,
|
|
75
|
+
flags,
|
|
76
|
+
});
|
|
77
|
+
console.log(header);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Load contract file
|
|
81
|
+
let contractJsonContent: string;
|
|
82
|
+
try {
|
|
83
|
+
contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
86
|
+
return notOk(
|
|
87
|
+
errorFileNotFound(contractPathAbsolute, {
|
|
88
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
89
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`,
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return notOk(
|
|
94
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
95
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let contractJson: Record<string, unknown>;
|
|
101
|
+
try {
|
|
102
|
+
contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return notOk(
|
|
105
|
+
errorContractValidationFailed(
|
|
106
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
107
|
+
{ where: { path: contractPathAbsolute } },
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Resolve database connection (--db flag or config.db.connection)
|
|
113
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
114
|
+
if (!dbConnection) {
|
|
115
|
+
return notOk(
|
|
116
|
+
errorDatabaseConnectionRequired({
|
|
117
|
+
why: `Database connection is required for db schema-verify (set db.connection in ${configPath}, or pass --db <url>)`,
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Check for driver
|
|
123
|
+
if (!config.driver) {
|
|
124
|
+
return notOk(errorDriverRequired({ why: 'Config.driver is required for db schema-verify' }));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Create control client
|
|
128
|
+
const client = createControlClient({
|
|
129
|
+
family: config.family,
|
|
130
|
+
target: config.target,
|
|
131
|
+
adapter: config.adapter,
|
|
132
|
+
driver: config.driver,
|
|
133
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Create progress adapter
|
|
137
|
+
const onProgress = createProgressAdapter({ flags });
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
const schemaVerifyResult = await client.schemaVerify({
|
|
141
|
+
contractIR: contractJson,
|
|
142
|
+
strict: options.strict ?? false,
|
|
143
|
+
connection: dbConnection,
|
|
144
|
+
onProgress,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Add blank line after all async operations if spinners were shown
|
|
148
|
+
if (!flags.quiet && flags.json !== 'object' && process.stdout.isTTY) {
|
|
149
|
+
console.log('');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return ok(schemaVerifyResult);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
// Driver already throws CliStructuredError for connection failures
|
|
155
|
+
if (error instanceof CliStructuredError) {
|
|
156
|
+
return notOk(error);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Wrap unexpected errors
|
|
160
|
+
return notOk(
|
|
161
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
162
|
+
why: `Unexpected error during db schema-verify: ${error instanceof Error ? error.message : String(error)}`,
|
|
163
|
+
}),
|
|
164
|
+
);
|
|
165
|
+
} finally {
|
|
166
|
+
await client.close();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function createDbSchemaVerifyCommand(): Command {
|
|
171
|
+
const command = new Command('schema-verify');
|
|
172
|
+
setCommandDescriptions(
|
|
173
|
+
command,
|
|
174
|
+
'Check whether the database schema satisfies your contract',
|
|
175
|
+
'Verifies that your database schema satisfies the emitted contract. Compares table structures,\n' +
|
|
176
|
+
'column types, constraints, and extensions. Reports any mismatches via a contract-shaped\n' +
|
|
177
|
+
'verification tree. This is a read-only operation that does not modify the database.',
|
|
178
|
+
);
|
|
179
|
+
command
|
|
180
|
+
.configureHelp({
|
|
181
|
+
formatHelp: (cmd) => {
|
|
182
|
+
const flags = parseGlobalFlags({});
|
|
183
|
+
return formatCommandHelp({ command: cmd, flags });
|
|
184
|
+
},
|
|
185
|
+
})
|
|
186
|
+
.option('--db <url>', 'Database connection string')
|
|
187
|
+
.option('--config <path>', 'Path to prisma-next.config.ts')
|
|
188
|
+
.option('--json [format]', 'Output as JSON (object)', false)
|
|
189
|
+
.option('--strict', 'Strict mode: extra schema elements cause failures', false)
|
|
190
|
+
.option('-q, --quiet', 'Quiet mode: errors only')
|
|
191
|
+
.option('-v, --verbose', 'Verbose output: debug info, timings')
|
|
192
|
+
.option('-vv, --trace', 'Trace output: deep internals, stack traces')
|
|
193
|
+
.option('--timestamps', 'Add timestamps to output')
|
|
194
|
+
.option('--color', 'Force color output')
|
|
195
|
+
.option('--no-color', 'Disable color output')
|
|
196
|
+
.action(async (options: DbSchemaVerifyOptions) => {
|
|
197
|
+
const flags = parseGlobalFlags(options);
|
|
198
|
+
|
|
199
|
+
// Validate JSON format option
|
|
200
|
+
if (flags.json === 'ndjson') {
|
|
201
|
+
const result = notOk(
|
|
202
|
+
errorJsonFormatNotSupported({
|
|
203
|
+
command: 'db schema-verify',
|
|
204
|
+
format: 'ndjson',
|
|
205
|
+
supportedFormats: ['object'],
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
const exitCode = handleResult(result, flags);
|
|
209
|
+
process.exit(exitCode);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const result = await executeDbSchemaVerifyCommand(options, flags);
|
|
213
|
+
|
|
214
|
+
// Handle result - formats output and returns exit code
|
|
215
|
+
const exitCode = handleResult(result, flags, (schemaVerifyResult) => {
|
|
216
|
+
if (flags.json === 'object') {
|
|
217
|
+
console.log(formatSchemaVerifyJson(schemaVerifyResult));
|
|
218
|
+
} else {
|
|
219
|
+
const output = formatSchemaVerifyOutput(schemaVerifyResult, flags);
|
|
220
|
+
if (output) {
|
|
221
|
+
console.log(output);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// For logical schema mismatches, check if verification passed
|
|
227
|
+
// Infra errors already handled by handleResult (returns non-zero exit code)
|
|
228
|
+
if (result.ok && !result.value.ok) {
|
|
229
|
+
// Schema verification failed - exit with code 1
|
|
230
|
+
process.exit(1);
|
|
231
|
+
} else {
|
|
232
|
+
// Success or infra error - use exit code from handleResult
|
|
233
|
+
process.exit(exitCode);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return command;
|
|
238
|
+
}
|