@prisma-next/cli 0.3.0-dev.15 → 0.3.0-dev.162
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/LICENSE +201 -0
- package/README.md +381 -128
- package/dist/cli-errors-BDCYR5ap.mjs +4 -0
- package/dist/cli-errors-Dzs7Oxz7.d.mts +3 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.js +1 -2346
- package/dist/cli.mjs +245 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/client-yYtotiSX.mjs +1063 -0
- package/dist/client-yYtotiSX.mjs.map +1 -0
- package/dist/commands/contract-emit.d.mts +7 -0
- package/dist/commands/contract-emit.d.mts.map +1 -0
- package/dist/commands/contract-emit.mjs +8 -0
- package/dist/commands/contract-infer.d.mts +7 -0
- package/dist/commands/contract-infer.d.mts.map +1 -0
- package/dist/commands/contract-infer.mjs +9 -0
- package/dist/commands/db-init.d.mts +7 -0
- package/dist/commands/db-init.d.mts.map +1 -0
- package/dist/commands/db-init.mjs +125 -0
- package/dist/commands/db-init.mjs.map +1 -0
- package/dist/commands/db-schema.d.mts +7 -0
- package/dist/commands/db-schema.d.mts.map +1 -0
- package/dist/commands/db-schema.mjs +55 -0
- package/dist/commands/db-schema.mjs.map +1 -0
- package/dist/commands/db-sign.d.mts +7 -0
- package/dist/commands/db-sign.d.mts.map +1 -0
- package/dist/commands/db-sign.mjs +136 -0
- package/dist/commands/db-sign.mjs.map +1 -0
- package/dist/commands/db-update.d.mts +7 -0
- package/dist/commands/db-update.d.mts.map +1 -0
- package/dist/commands/db-update.mjs +122 -0
- package/dist/commands/db-update.mjs.map +1 -0
- package/dist/commands/db-verify.d.mts +7 -0
- package/dist/commands/db-verify.d.mts.map +1 -0
- package/dist/commands/db-verify.mjs +322 -0
- package/dist/commands/db-verify.mjs.map +1 -0
- package/dist/commands/migration-apply.d.mts +36 -0
- package/dist/commands/migration-apply.d.mts.map +1 -0
- package/dist/commands/migration-apply.mjs +244 -0
- package/dist/commands/migration-apply.mjs.map +1 -0
- package/dist/commands/migration-new.d.mts +8 -0
- package/dist/commands/migration-new.d.mts.map +1 -0
- package/dist/commands/migration-new.mjs +151 -0
- package/dist/commands/migration-new.mjs.map +1 -0
- package/dist/commands/migration-plan.d.mts +47 -0
- package/dist/commands/migration-plan.d.mts.map +1 -0
- package/dist/commands/migration-plan.mjs +312 -0
- package/dist/commands/migration-plan.mjs.map +1 -0
- package/dist/commands/migration-ref.d.mts +43 -0
- package/dist/commands/migration-ref.d.mts.map +1 -0
- package/dist/commands/migration-ref.mjs +194 -0
- package/dist/commands/migration-ref.mjs.map +1 -0
- package/dist/commands/migration-show.d.mts +28 -0
- package/dist/commands/migration-show.d.mts.map +1 -0
- package/dist/commands/migration-show.mjs +139 -0
- package/dist/commands/migration-show.mjs.map +1 -0
- package/dist/commands/migration-status.d.mts +86 -0
- package/dist/commands/migration-status.d.mts.map +1 -0
- package/dist/commands/migration-status.mjs +8 -0
- package/dist/commands/migration-verify.d.mts +16 -0
- package/dist/commands/migration-verify.d.mts.map +1 -0
- package/dist/commands/migration-verify.mjs +109 -0
- package/dist/commands/migration-verify.mjs.map +1 -0
- package/dist/config-loader-C4VXKl8f.mjs +43 -0
- package/dist/config-loader-C4VXKl8f.mjs.map +1 -0
- package/dist/{config-loader.d.ts → config-loader.d.mts} +8 -3
- package/dist/config-loader.d.mts.map +1 -0
- package/dist/config-loader.mjs +3 -0
- package/dist/contract-emit-Bk_eEDKu.mjs +187 -0
- package/dist/contract-emit-Bk_eEDKu.mjs.map +1 -0
- package/dist/contract-infer-suMDmFSG.mjs +89 -0
- package/dist/contract-infer-suMDmFSG.mjs.map +1 -0
- package/dist/exports/config-types.d.mts +2 -0
- package/dist/exports/config-types.mjs +3 -0
- package/dist/exports/control-api.d.mts +624 -0
- package/dist/exports/control-api.d.mts.map +1 -0
- package/dist/exports/control-api.mjs +109 -0
- package/dist/exports/control-api.mjs.map +1 -0
- package/dist/{load-ts-contract.d.ts → exports/index.d.mts} +12 -7
- package/dist/exports/index.d.mts.map +1 -0
- package/dist/exports/index.mjs +141 -0
- package/dist/exports/index.mjs.map +1 -0
- package/dist/extract-operation-statements-BVlb3jxp.mjs +13 -0
- package/dist/extract-operation-statements-BVlb3jxp.mjs.map +1 -0
- package/dist/extract-sql-ddl-6EVSOThm.mjs +26 -0
- package/dist/extract-sql-ddl-6EVSOThm.mjs.map +1 -0
- package/dist/framework-components-BAsliT4V.mjs +59 -0
- package/dist/framework-components-BAsliT4V.mjs.map +1 -0
- package/dist/inspect-live-schema-HMutsJYh.mjs +91 -0
- package/dist/inspect-live-schema-HMutsJYh.mjs.map +1 -0
- package/dist/migration-command-scaffold-Dg7CKKCg.mjs +105 -0
- package/dist/migration-command-scaffold-Dg7CKKCg.mjs.map +1 -0
- package/dist/migration-status-BqfVmC0w.mjs +1582 -0
- package/dist/migration-status-BqfVmC0w.mjs.map +1 -0
- package/dist/migrations-Bv8oeiY_.mjs +173 -0
- package/dist/migrations-Bv8oeiY_.mjs.map +1 -0
- package/dist/progress-adapter-D4x8SbJa.mjs +43 -0
- package/dist/progress-adapter-D4x8SbJa.mjs.map +1 -0
- package/dist/terminal-ui-N5tR-ob5.mjs +967 -0
- package/dist/terminal-ui-N5tR-ob5.mjs.map +1 -0
- package/dist/verify-WARh5TjK.mjs +385 -0
- package/dist/verify-WARh5TjK.mjs.map +1 -0
- package/package.json +87 -41
- package/src/cli.ts +113 -58
- package/src/commands/contract-emit.ts +237 -144
- package/src/commands/contract-infer-paths.ts +32 -0
- package/src/commands/contract-infer.ts +143 -0
- package/src/commands/db-init.ts +211 -426
- package/src/commands/db-schema.ts +77 -0
- package/src/commands/db-sign.ts +208 -229
- package/src/commands/db-update.ts +236 -0
- package/src/commands/db-verify.ts +504 -184
- package/src/commands/inspect-live-schema.ts +170 -0
- package/src/commands/migration-apply.ts +427 -0
- package/src/commands/migration-new.ts +260 -0
- package/src/commands/migration-plan.ts +519 -0
- package/src/commands/migration-ref.ts +305 -0
- package/src/commands/migration-show.ts +246 -0
- package/src/commands/migration-status.ts +864 -0
- package/src/commands/migration-verify.ts +180 -0
- package/src/config-loader.ts +13 -3
- package/src/control-api/client.ts +428 -46
- package/src/control-api/contract-enrichment.ts +119 -0
- package/src/control-api/errors.ts +9 -0
- package/src/control-api/operations/contract-emit.ts +174 -0
- package/src/control-api/operations/db-init.ts +144 -26
- package/src/control-api/operations/db-update.ts +220 -0
- package/src/control-api/operations/extract-operation-statements.ts +14 -0
- package/src/control-api/operations/extract-sql-ddl.ts +47 -0
- package/src/control-api/operations/migration-apply.ts +191 -0
- package/src/control-api/operations/migration-helpers.ts +49 -0
- package/src/control-api/types.ts +450 -18
- package/src/exports/config-types.ts +4 -3
- package/src/exports/control-api.ts +24 -2
- package/src/load-ts-contract.ts +30 -19
- package/src/utils/cli-errors.ts +14 -8
- package/src/utils/command-helpers.ts +302 -3
- package/src/utils/formatters/emit.ts +67 -0
- package/src/utils/formatters/errors.ts +82 -0
- package/src/utils/formatters/graph-migration-mapper.ts +240 -0
- package/src/utils/formatters/graph-render.ts +1323 -0
- package/src/utils/formatters/graph-types.ts +120 -0
- package/src/utils/formatters/help.ts +380 -0
- package/src/utils/formatters/helpers.ts +28 -0
- package/src/utils/formatters/migrations.ts +346 -0
- package/src/utils/formatters/styled.ts +212 -0
- package/src/utils/formatters/verify.ts +621 -0
- package/src/utils/framework-components.ts +13 -10
- package/src/utils/global-flags.ts +41 -23
- package/src/utils/migration-command-scaffold.ts +184 -0
- package/src/utils/migration-types.ts +12 -0
- package/src/utils/progress-adapter.ts +75 -0
- package/src/utils/result-handler.ts +12 -13
- package/src/utils/shutdown.ts +92 -0
- package/src/utils/suggest-command.ts +31 -0
- package/src/utils/terminal-ui.ts +276 -0
- package/dist/chunk-6EPKRATC.js +0 -91
- package/dist/chunk-6EPKRATC.js.map +0 -1
- package/dist/chunk-DIJPT5TZ.js +0 -967
- package/dist/chunk-DIJPT5TZ.js.map +0 -1
- package/dist/chunk-HWYQOCAJ.js +0 -47
- package/dist/chunk-HWYQOCAJ.js.map +0 -1
- package/dist/chunk-MG7PBERL.js +0 -131
- package/dist/chunk-MG7PBERL.js.map +0 -1
- package/dist/chunk-VI2YETW7.js +0 -38
- package/dist/chunk-VI2YETW7.js.map +0 -1
- package/dist/cli.d.ts +0 -2
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js.map +0 -1
- package/dist/commands/contract-emit.d.ts +0 -3
- package/dist/commands/contract-emit.d.ts.map +0 -1
- package/dist/commands/contract-emit.js +0 -10
- package/dist/commands/contract-emit.js.map +0 -1
- package/dist/commands/db-init.d.ts +0 -3
- package/dist/commands/db-init.d.ts.map +0 -1
- package/dist/commands/db-init.js +0 -339
- package/dist/commands/db-init.js.map +0 -1
- package/dist/commands/db-introspect.d.ts +0 -3
- package/dist/commands/db-introspect.d.ts.map +0 -1
- package/dist/commands/db-introspect.js +0 -183
- package/dist/commands/db-introspect.js.map +0 -1
- package/dist/commands/db-schema-verify.d.ts +0 -3
- package/dist/commands/db-schema-verify.d.ts.map +0 -1
- package/dist/commands/db-schema-verify.js +0 -161
- package/dist/commands/db-schema-verify.js.map +0 -1
- package/dist/commands/db-sign.d.ts +0 -3
- package/dist/commands/db-sign.d.ts.map +0 -1
- package/dist/commands/db-sign.js +0 -196
- package/dist/commands/db-sign.js.map +0 -1
- package/dist/commands/db-verify.d.ts +0 -3
- package/dist/commands/db-verify.d.ts.map +0 -1
- package/dist/commands/db-verify.js +0 -170
- package/dist/commands/db-verify.js.map +0 -1
- package/dist/config-loader.d.ts.map +0 -1
- package/dist/config-loader.js +0 -7
- package/dist/config-loader.js.map +0 -1
- package/dist/control-api/client.d.ts +0 -13
- package/dist/control-api/client.d.ts.map +0 -1
- package/dist/control-api/operations/db-init.d.ts +0 -27
- package/dist/control-api/operations/db-init.d.ts.map +0 -1
- package/dist/control-api/types.d.ts +0 -203
- package/dist/control-api/types.d.ts.map +0 -1
- package/dist/exports/config-types.d.ts +0 -3
- package/dist/exports/config-types.d.ts.map +0 -1
- package/dist/exports/config-types.js +0 -6
- package/dist/exports/config-types.js.map +0 -1
- package/dist/exports/control-api.d.ts +0 -13
- package/dist/exports/control-api.d.ts.map +0 -1
- package/dist/exports/control-api.js +0 -240
- package/dist/exports/control-api.js.map +0 -1
- package/dist/exports/index.d.ts +0 -4
- package/dist/exports/index.d.ts.map +0 -1
- package/dist/exports/index.js +0 -176
- package/dist/exports/index.js.map +0 -1
- package/dist/load-ts-contract.d.ts.map +0 -1
- package/dist/utils/action.d.ts +0 -16
- package/dist/utils/action.d.ts.map +0 -1
- package/dist/utils/cli-errors.d.ts +0 -7
- package/dist/utils/cli-errors.d.ts.map +0 -1
- package/dist/utils/command-helpers.d.ts +0 -12
- package/dist/utils/command-helpers.d.ts.map +0 -1
- package/dist/utils/framework-components.d.ts +0 -70
- package/dist/utils/framework-components.d.ts.map +0 -1
- package/dist/utils/global-flags.d.ts +0 -25
- package/dist/utils/global-flags.d.ts.map +0 -1
- package/dist/utils/output.d.ts +0 -142
- package/dist/utils/output.d.ts.map +0 -1
- package/dist/utils/result-handler.d.ts +0 -15
- package/dist/utils/result-handler.d.ts.map +0 -1
- package/dist/utils/spinner.d.ts +0 -29
- package/dist/utils/spinner.d.ts.map +0 -1
- package/src/commands/db-introspect.ts +0 -254
- package/src/commands/db-schema-verify.ts +0 -231
- package/src/utils/action.ts +0 -43
- package/src/utils/output.ts +0 -1471
- package/src/utils/spinner.ts +0 -67
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { IntrospectSchemaResult } from '@prisma-next/framework-components/control';
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import {
|
|
4
|
+
addGlobalOptions,
|
|
5
|
+
setCommandDescriptions,
|
|
6
|
+
setCommandExamples,
|
|
7
|
+
} from '../utils/command-helpers';
|
|
8
|
+
import { formatIntrospectJson, formatIntrospectOutput } from '../utils/formatters/verify';
|
|
9
|
+
import { parseGlobalFlags } from '../utils/global-flags';
|
|
10
|
+
import { handleResult } from '../utils/result-handler';
|
|
11
|
+
import { TerminalUI } from '../utils/terminal-ui';
|
|
12
|
+
import {
|
|
13
|
+
type InspectLiveSchemaOptions,
|
|
14
|
+
type InspectLiveSchemaResult,
|
|
15
|
+
inspectLiveSchema,
|
|
16
|
+
} from './inspect-live-schema';
|
|
17
|
+
|
|
18
|
+
function toIntrospectSchemaResult(
|
|
19
|
+
result: InspectLiveSchemaResult,
|
|
20
|
+
): IntrospectSchemaResult<unknown> {
|
|
21
|
+
return {
|
|
22
|
+
ok: true,
|
|
23
|
+
summary: 'Schema read successfully',
|
|
24
|
+
target: result.target,
|
|
25
|
+
schema: result.schema,
|
|
26
|
+
meta: result.meta,
|
|
27
|
+
timings: result.timings,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createDbSchemaCommand(): Command {
|
|
32
|
+
const command = new Command('schema');
|
|
33
|
+
setCommandDescriptions(
|
|
34
|
+
command,
|
|
35
|
+
'Inspect the live database schema',
|
|
36
|
+
'Reads the live database schema and prints it as a tree by default or as JSON with\n' +
|
|
37
|
+
'--json. This command is always read-only and never writes files. To save machine-\n' +
|
|
38
|
+
'readable output, use shell redirection, for example `prisma-next db schema --json > schema.json`.',
|
|
39
|
+
);
|
|
40
|
+
setCommandExamples(command, [
|
|
41
|
+
'prisma-next db schema --db $DATABASE_URL',
|
|
42
|
+
'prisma-next db schema --db $DATABASE_URL --json',
|
|
43
|
+
'prisma-next db schema --db $DATABASE_URL --json > schema.json',
|
|
44
|
+
]);
|
|
45
|
+
addGlobalOptions(command)
|
|
46
|
+
.option('--db <url>', 'Database connection string')
|
|
47
|
+
.option('--config <path>', 'Path to prisma-next.config.ts')
|
|
48
|
+
.action(async (options: InspectLiveSchemaOptions) => {
|
|
49
|
+
const flags = parseGlobalFlags(options);
|
|
50
|
+
const ui = new TerminalUI({ color: flags.color, interactive: flags.interactive });
|
|
51
|
+
const startTime = Date.now();
|
|
52
|
+
|
|
53
|
+
const result = await inspectLiveSchema(options, flags, ui, startTime, {
|
|
54
|
+
commandName: 'db schema',
|
|
55
|
+
description: 'Inspect the live database schema',
|
|
56
|
+
url: 'https://pris.ly/db-schema',
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const exitCode = handleResult(result, flags, ui, (value) => {
|
|
60
|
+
const introspectResult = toIntrospectSchemaResult(value);
|
|
61
|
+
|
|
62
|
+
if (flags.json) {
|
|
63
|
+
ui.output(formatIntrospectJson(introspectResult));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const output = formatIntrospectOutput(introspectResult, value.schemaView, flags);
|
|
68
|
+
if (output) {
|
|
69
|
+
ui.log(output);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
process.exit(exitCode);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return command;
|
|
77
|
+
}
|
package/src/commands/db-sign.ts
CHANGED
|
@@ -1,50 +1,196 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { relative, resolve } from 'node:path';
|
|
3
|
-
import {
|
|
4
|
-
errorDatabaseConnectionRequired,
|
|
5
|
-
errorDriverRequired,
|
|
6
|
-
errorFileNotFound,
|
|
7
|
-
errorRuntime,
|
|
8
|
-
errorUnexpected,
|
|
9
|
-
} from '@prisma-next/core-control-plane/errors';
|
|
10
2
|
import type {
|
|
11
3
|
SignDatabaseResult,
|
|
12
4
|
VerifyDatabaseSchemaResult,
|
|
13
|
-
} from '@prisma-next/
|
|
14
|
-
import {
|
|
5
|
+
} from '@prisma-next/framework-components/control';
|
|
6
|
+
import { notOk, ok, type Result } from '@prisma-next/utils/result';
|
|
15
7
|
import { Command } from 'commander';
|
|
8
|
+
import { relative, resolve } from 'pathe';
|
|
16
9
|
import { loadConfig } from '../config-loader';
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
10
|
+
import { createControlClient } from '../control-api/client';
|
|
11
|
+
import { ContractValidationError } from '../control-api/errors';
|
|
12
|
+
import {
|
|
13
|
+
CliStructuredError,
|
|
14
|
+
errorContractValidationFailed,
|
|
15
|
+
errorDatabaseConnectionRequired,
|
|
16
|
+
errorDriverRequired,
|
|
17
|
+
errorFileNotFound,
|
|
18
|
+
errorUnexpected,
|
|
19
|
+
} from '../utils/cli-errors';
|
|
19
20
|
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
addGlobalOptions,
|
|
22
|
+
maskConnectionUrl,
|
|
23
|
+
resolveContractPath,
|
|
24
|
+
setCommandDescriptions,
|
|
25
|
+
setCommandExamples,
|
|
26
|
+
} from '../utils/command-helpers';
|
|
27
|
+
import { formatStyledHeader } from '../utils/formatters/styled';
|
|
24
28
|
import {
|
|
25
|
-
formatCommandHelp,
|
|
26
29
|
formatSchemaVerifyJson,
|
|
27
30
|
formatSchemaVerifyOutput,
|
|
28
31
|
formatSignJson,
|
|
29
32
|
formatSignOutput,
|
|
30
|
-
|
|
31
|
-
} from '../utils/
|
|
33
|
+
} from '../utils/formatters/verify';
|
|
34
|
+
import type { CommonCommandOptions } from '../utils/global-flags';
|
|
35
|
+
import { type GlobalFlags, parseGlobalFlags } from '../utils/global-flags';
|
|
36
|
+
import { createProgressAdapter } from '../utils/progress-adapter';
|
|
32
37
|
import { handleResult } from '../utils/result-handler';
|
|
33
|
-
import {
|
|
38
|
+
import { TerminalUI } from '../utils/terminal-ui';
|
|
34
39
|
|
|
35
|
-
interface DbSignOptions {
|
|
40
|
+
interface DbSignOptions extends CommonCommandOptions {
|
|
36
41
|
readonly db?: string;
|
|
37
42
|
readonly config?: string;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Failure type for db sign command.
|
|
47
|
+
* Either an infrastructure error (CliStructuredError) or a logical failure (schema verification failed).
|
|
48
|
+
*/
|
|
49
|
+
type DbSignFailure = CliStructuredError | VerifyDatabaseSchemaResult;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Executes the db sign command and returns a structured Result.
|
|
53
|
+
* Success: SignDatabaseResult (sign happened)
|
|
54
|
+
* Failure: CliStructuredError (infra error) or VerifyDatabaseSchemaResult (schema mismatch)
|
|
55
|
+
*/
|
|
56
|
+
async function executeDbSignCommand(
|
|
57
|
+
options: DbSignOptions,
|
|
58
|
+
flags: GlobalFlags,
|
|
59
|
+
ui: TerminalUI,
|
|
60
|
+
): Promise<Result<SignDatabaseResult, DbSignFailure>> {
|
|
61
|
+
// Load config
|
|
62
|
+
const config = await loadConfig(options.config);
|
|
63
|
+
const configPath = options.config
|
|
64
|
+
? relative(process.cwd(), resolve(options.config))
|
|
65
|
+
: 'prisma-next.config.ts';
|
|
66
|
+
const contractPathAbsolute = resolveContractPath(config);
|
|
67
|
+
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
68
|
+
|
|
69
|
+
// Output header
|
|
70
|
+
if (!flags.json && !flags.quiet) {
|
|
71
|
+
const details: Array<{ label: string; value: string }> = [
|
|
72
|
+
{ label: 'config', value: configPath },
|
|
73
|
+
{ label: 'contract', value: contractPath },
|
|
74
|
+
];
|
|
75
|
+
if (options.db) {
|
|
76
|
+
details.push({ label: 'database', value: maskConnectionUrl(options.db) });
|
|
77
|
+
}
|
|
78
|
+
const header = formatStyledHeader({
|
|
79
|
+
command: 'db sign',
|
|
80
|
+
description: 'Sign the database with your contract so you can safely run queries',
|
|
81
|
+
url: 'https://pris.ly/db-sign',
|
|
82
|
+
details,
|
|
83
|
+
flags,
|
|
84
|
+
});
|
|
85
|
+
ui.stderr(header);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Load contract file
|
|
89
|
+
let contractJsonContent: string;
|
|
90
|
+
try {
|
|
91
|
+
contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
94
|
+
return notOk(
|
|
95
|
+
errorFileNotFound(contractPathAbsolute, {
|
|
96
|
+
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
97
|
+
fix: `Run \`prisma-next contract emit\` to generate ${contractPath}, or update \`config.contract.output\` in ${configPath}`,
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return notOk(
|
|
102
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
103
|
+
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let contractJson: Record<string, unknown>;
|
|
109
|
+
try {
|
|
110
|
+
contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
return notOk(
|
|
113
|
+
errorContractValidationFailed(
|
|
114
|
+
`Contract JSON is invalid: ${error instanceof Error ? error.message : String(error)}`,
|
|
115
|
+
{ where: { path: contractPathAbsolute } },
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Resolve database connection (--db flag or config.db.connection)
|
|
121
|
+
const dbConnection = options.db ?? config.db?.connection;
|
|
122
|
+
if (!dbConnection) {
|
|
123
|
+
return notOk(
|
|
124
|
+
errorDatabaseConnectionRequired({
|
|
125
|
+
why: `Database connection is required for db sign (set db.connection in ${configPath}, or pass --db <url>)`,
|
|
126
|
+
commandName: 'db sign',
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Check for driver
|
|
132
|
+
if (!config.driver) {
|
|
133
|
+
return notOk(errorDriverRequired({ why: 'Config.driver is required for db sign' }));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Create control client
|
|
137
|
+
const client = createControlClient({
|
|
138
|
+
family: config.family,
|
|
139
|
+
target: config.target,
|
|
140
|
+
adapter: config.adapter,
|
|
141
|
+
driver: config.driver,
|
|
142
|
+
extensionPacks: config.extensionPacks ?? [],
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// Create progress adapter
|
|
146
|
+
const onProgress = createProgressAdapter({ ui, flags });
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
// Step 1: Schema verification - connect here
|
|
150
|
+
const schemaVerifyResult = await client.schemaVerify({
|
|
151
|
+
contract: contractJson,
|
|
152
|
+
strict: false,
|
|
153
|
+
connection: dbConnection,
|
|
154
|
+
onProgress,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// If schema verification failed, return as failure
|
|
158
|
+
if (!schemaVerifyResult.ok) {
|
|
159
|
+
return notOk(schemaVerifyResult);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Step 2: Sign (already connected from schemaVerify)
|
|
163
|
+
const signResult = await client.sign({
|
|
164
|
+
contract: contractJson,
|
|
165
|
+
contractPath,
|
|
166
|
+
configPath,
|
|
167
|
+
onProgress,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return ok(signResult);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
// Driver already throws CliStructuredError for connection failures
|
|
173
|
+
if (error instanceof CliStructuredError) {
|
|
174
|
+
return notOk(error);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (error instanceof ContractValidationError) {
|
|
178
|
+
return notOk(
|
|
179
|
+
errorContractValidationFailed(`Contract validation failed: ${error.message}`, {
|
|
180
|
+
where: { path: contractPathAbsolute },
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Wrap unexpected errors
|
|
186
|
+
return notOk(
|
|
187
|
+
errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
188
|
+
why: `Unexpected error during db sign: ${error instanceof Error ? error.message : String(error)}`,
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
} finally {
|
|
192
|
+
await client.close();
|
|
193
|
+
}
|
|
48
194
|
}
|
|
49
195
|
|
|
50
196
|
export function createDbSignCommand(): Command {
|
|
@@ -53,220 +199,53 @@ export function createDbSignCommand(): Command {
|
|
|
53
199
|
command,
|
|
54
200
|
'Sign the database with your contract so you can safely run queries',
|
|
55
201
|
'Verifies that your database schema satisfies the emitted contract, and if so, writes or\n' +
|
|
56
|
-
'updates the
|
|
57
|
-
'in CI/deployment pipelines. The
|
|
202
|
+
'updates the database signature. This command is idempotent and safe to run\n' +
|
|
203
|
+
'in CI/deployment pipelines. The signature records that this database instance is aligned\n' +
|
|
58
204
|
'with a specific contract version.',
|
|
59
205
|
);
|
|
60
|
-
command
|
|
61
|
-
|
|
62
|
-
formatHelp: (cmd) => {
|
|
63
|
-
const flags = parseGlobalFlags({});
|
|
64
|
-
return formatCommandHelp({ command: cmd, flags });
|
|
65
|
-
},
|
|
66
|
-
})
|
|
206
|
+
setCommandExamples(command, ['prisma-next db sign --db $DATABASE_URL']);
|
|
207
|
+
addGlobalOptions(command)
|
|
67
208
|
.option('--db <url>', 'Database connection string')
|
|
68
209
|
.option('--config <path>', 'Path to prisma-next.config.ts')
|
|
69
|
-
.option('--json [format]', 'Output as JSON (object or ndjson)', false)
|
|
70
|
-
.option('-q, --quiet', 'Quiet mode: errors only')
|
|
71
|
-
.option('-v, --verbose', 'Verbose output: debug info, timings')
|
|
72
|
-
.option('-vv, --trace', 'Trace output: deep internals, stack traces')
|
|
73
|
-
.option('--timestamps', 'Add timestamps to output')
|
|
74
|
-
.option('--color', 'Force color output')
|
|
75
|
-
.option('--no-color', 'Disable color output')
|
|
76
210
|
.action(async (options: DbSignOptions) => {
|
|
77
211
|
const flags = parseGlobalFlags(options);
|
|
78
212
|
|
|
79
|
-
const
|
|
80
|
-
// Load config (file I/O)
|
|
81
|
-
const config = await loadConfig(options.config);
|
|
82
|
-
// Normalize config path for display (match contract path format - no ./ prefix)
|
|
83
|
-
const configPath = options.config
|
|
84
|
-
? relative(process.cwd(), resolve(options.config))
|
|
85
|
-
: 'prisma-next.config.ts';
|
|
86
|
-
const contractPathAbsolute = config.contract?.output
|
|
87
|
-
? resolve(config.contract.output)
|
|
88
|
-
: resolve('src/prisma/contract.json');
|
|
89
|
-
// Convert to relative path for display
|
|
90
|
-
const contractPath = relative(process.cwd(), contractPathAbsolute);
|
|
91
|
-
|
|
92
|
-
// Output header (only for human-readable output)
|
|
93
|
-
if (flags.json !== 'object' && !flags.quiet) {
|
|
94
|
-
const details: Array<{ label: string; value: string }> = [
|
|
95
|
-
{ label: 'config', value: configPath },
|
|
96
|
-
{ label: 'contract', value: contractPath },
|
|
97
|
-
];
|
|
98
|
-
if (options.db) {
|
|
99
|
-
details.push({ label: 'database', value: options.db });
|
|
100
|
-
}
|
|
101
|
-
const header = formatStyledHeader({
|
|
102
|
-
command: 'db sign',
|
|
103
|
-
description: 'Sign the database with your contract so you can safely run queries',
|
|
104
|
-
url: 'https://pris.ly/db-sign',
|
|
105
|
-
details,
|
|
106
|
-
flags,
|
|
107
|
-
});
|
|
108
|
-
console.log(header);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Load contract file (file I/O)
|
|
112
|
-
let contractJsonContent: string;
|
|
113
|
-
try {
|
|
114
|
-
contractJsonContent = await readFile(contractPathAbsolute, 'utf-8');
|
|
115
|
-
} catch (error) {
|
|
116
|
-
if (error instanceof Error && (error as { code?: string }).code === 'ENOENT') {
|
|
117
|
-
throw errorFileNotFound(contractPathAbsolute, {
|
|
118
|
-
why: `Contract file not found at ${contractPathAbsolute}`,
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
throw errorUnexpected(error instanceof Error ? error.message : String(error), {
|
|
122
|
-
why: `Failed to read contract file: ${error instanceof Error ? error.message : String(error)}`,
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
const contractJson = JSON.parse(contractJsonContent) as Record<string, unknown>;
|
|
126
|
-
|
|
127
|
-
// Resolve database connection (--db flag or config.db.connection)
|
|
128
|
-
const dbConnection = options.db ?? config.db?.connection;
|
|
129
|
-
if (!dbConnection) {
|
|
130
|
-
throw errorDatabaseConnectionRequired();
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Check for driver
|
|
134
|
-
if (!config.driver) {
|
|
135
|
-
throw errorDriverRequired();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Store driver descriptor after null check
|
|
139
|
-
const driverDescriptor = config.driver;
|
|
140
|
-
|
|
141
|
-
// Create family instance (needed for contract validation - no DB connection required)
|
|
142
|
-
const stack = createControlPlaneStack({
|
|
143
|
-
target: config.target,
|
|
144
|
-
adapter: config.adapter,
|
|
145
|
-
driver: driverDescriptor,
|
|
146
|
-
extensionPacks: config.extensionPacks,
|
|
147
|
-
});
|
|
148
|
-
const familyInstance = config.family.create(stack);
|
|
149
|
-
|
|
150
|
-
// Validate contract using instance validator (fail-fast before DB connection)
|
|
151
|
-
const contractIR = familyInstance.validateContractIR(contractJson);
|
|
152
|
-
assertContractRequirementsSatisfied({ contract: contractIR, stack });
|
|
153
|
-
|
|
154
|
-
const rawComponents = [config.target, config.adapter, ...(config.extensionPacks ?? [])];
|
|
155
|
-
const frameworkComponents = assertFrameworkComponentsCompatible(
|
|
156
|
-
config.family.familyId,
|
|
157
|
-
config.target.targetId,
|
|
158
|
-
rawComponents,
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
// Create driver (expensive operation - done after validation)
|
|
162
|
-
const driver = await driverDescriptor.create(dbConnection);
|
|
163
|
-
|
|
164
|
-
try {
|
|
165
|
-
// Schema verification precondition with spinner
|
|
166
|
-
let schemaVerifyResult: VerifyDatabaseSchemaResult;
|
|
167
|
-
try {
|
|
168
|
-
schemaVerifyResult = await withSpinner(
|
|
169
|
-
() =>
|
|
170
|
-
familyInstance.schemaVerify({
|
|
171
|
-
driver,
|
|
172
|
-
contractIR,
|
|
173
|
-
strict: false,
|
|
174
|
-
contractPath: contractPathAbsolute,
|
|
175
|
-
configPath,
|
|
176
|
-
frameworkComponents,
|
|
177
|
-
}),
|
|
178
|
-
{
|
|
179
|
-
message: 'Verifying database satisfies contract',
|
|
180
|
-
flags,
|
|
181
|
-
},
|
|
182
|
-
);
|
|
183
|
-
} catch (error) {
|
|
184
|
-
// Wrap errors from schemaVerify() in structured error
|
|
185
|
-
throw errorRuntime(error instanceof Error ? error.message : String(error), {
|
|
186
|
-
why: `Failed to verify database schema: ${error instanceof Error ? error.message : String(error)}`,
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// If schema verification failed, return both results for handling outside performAction
|
|
191
|
-
if (!schemaVerifyResult.ok) {
|
|
192
|
-
return { schemaVerifyResult, signResult: undefined };
|
|
193
|
-
}
|
|
213
|
+
const ui = new TerminalUI({ color: flags.color, interactive: flags.interactive });
|
|
194
214
|
|
|
195
|
-
|
|
196
|
-
let signResult: SignDatabaseResult;
|
|
197
|
-
try {
|
|
198
|
-
signResult = await withSpinner(
|
|
199
|
-
() =>
|
|
200
|
-
familyInstance.sign({
|
|
201
|
-
driver,
|
|
202
|
-
contractIR,
|
|
203
|
-
contractPath: contractPathAbsolute,
|
|
204
|
-
configPath,
|
|
205
|
-
}),
|
|
206
|
-
{
|
|
207
|
-
message: 'Signing database...',
|
|
208
|
-
flags,
|
|
209
|
-
},
|
|
210
|
-
);
|
|
211
|
-
} catch (error) {
|
|
212
|
-
// Wrap errors from sign() in structured error
|
|
213
|
-
throw errorRuntime(error instanceof Error ? error.message : String(error), {
|
|
214
|
-
why: `Failed to sign database: ${error instanceof Error ? error.message : String(error)}`,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
215
|
+
const result = await executeDbSignCommand(options, flags, ui);
|
|
217
216
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
217
|
+
if (result.ok) {
|
|
218
|
+
// Success - format sign output
|
|
219
|
+
if (flags.json) {
|
|
220
|
+
ui.output(formatSignJson(result.value));
|
|
221
|
+
} else {
|
|
222
|
+
const output = formatSignOutput(result.value, flags);
|
|
223
|
+
if (output) {
|
|
224
|
+
ui.log(output);
|
|
221
225
|
}
|
|
222
|
-
|
|
223
|
-
return { schemaVerifyResult: undefined, signResult };
|
|
224
|
-
} finally {
|
|
225
|
-
// Ensure driver connection is closed
|
|
226
|
-
await driver.close();
|
|
227
226
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
// Handle result - formats output and returns exit code
|
|
231
|
-
const exitCode = handleResult(result, flags, (value) => {
|
|
232
|
-
const { schemaVerifyResult, signResult } = value;
|
|
227
|
+
process.exit(0);
|
|
228
|
+
}
|
|
233
229
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
if (flags.json === 'object') {
|
|
237
|
-
console.log(formatSchemaVerifyJson(schemaVerifyResult));
|
|
238
|
-
} else {
|
|
239
|
-
const output = formatSchemaVerifyOutput(schemaVerifyResult, flags);
|
|
240
|
-
if (output) {
|
|
241
|
-
console.log(output);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
// Don't proceed to sign output formatting
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
230
|
+
// Failure - determine type and handle appropriately
|
|
231
|
+
const failure = result.failure;
|
|
247
232
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const output = formatSignOutput(signResult, flags);
|
|
254
|
-
if (output) {
|
|
255
|
-
console.log(output);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
});
|
|
233
|
+
if (failure instanceof CliStructuredError) {
|
|
234
|
+
// Infrastructure error - use standard handler
|
|
235
|
+
const exitCode = handleResult(result as Result<never, CliStructuredError>, flags, ui);
|
|
236
|
+
process.exit(exitCode);
|
|
237
|
+
}
|
|
260
238
|
|
|
261
|
-
//
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
// Schema verification failed - exit with code 1
|
|
265
|
-
process.exit(1);
|
|
239
|
+
// Schema verification failed - format and print schema verification output
|
|
240
|
+
if (flags.json) {
|
|
241
|
+
ui.output(formatSchemaVerifyJson(failure));
|
|
266
242
|
} else {
|
|
267
|
-
|
|
268
|
-
|
|
243
|
+
const output = formatSchemaVerifyOutput(failure, flags);
|
|
244
|
+
if (output) {
|
|
245
|
+
ui.log(output);
|
|
246
|
+
}
|
|
269
247
|
}
|
|
248
|
+
process.exit(1);
|
|
270
249
|
});
|
|
271
250
|
|
|
272
251
|
return command;
|