@prisma-next/cli 0.3.0-dev.7 → 0.3.0-dev.71
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 +314 -80
- package/dist/cli-errors-JlPTsazx.mjs +3 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.js +1 -2376
- package/dist/cli.mjs +198 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/client-PimzSD1f.mjs +981 -0
- package/dist/client-PimzSD1f.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 +150 -0
- package/dist/commands/contract-emit.mjs.map +1 -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 +132 -0
- package/dist/commands/db-init.mjs.map +1 -0
- package/dist/commands/db-introspect.d.mts +7 -0
- package/dist/commands/db-introspect.d.mts.map +1 -0
- package/dist/commands/db-introspect.mjs +117 -0
- package/dist/commands/db-introspect.mjs.map +1 -0
- package/dist/commands/db-schema-verify.d.mts +7 -0
- package/dist/commands/db-schema-verify.d.mts.map +1 -0
- package/dist/commands/db-schema-verify.mjs +119 -0
- package/dist/commands/db-schema-verify.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 +141 -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 +121 -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 +132 -0
- package/dist/commands/db-verify.mjs.map +1 -0
- package/dist/commands/migration-apply.d.mts +23 -0
- package/dist/commands/migration-apply.d.mts.map +1 -0
- package/dist/commands/migration-apply.mjs +249 -0
- package/dist/commands/migration-apply.mjs.map +1 -0
- package/dist/commands/migration-plan.d.mts +25 -0
- package/dist/commands/migration-plan.d.mts.map +1 -0
- package/dist/commands/migration-plan.mjs +266 -0
- package/dist/commands/migration-plan.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 +138 -0
- package/dist/commands/migration-show.mjs.map +1 -0
- package/dist/commands/migration-status.d.mts +35 -0
- package/dist/commands/migration-status.d.mts.map +1 -0
- package/dist/commands/migration-status.mjs +259 -0
- package/dist/commands/migration-status.mjs.map +1 -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 +86 -0
- package/dist/commands/migration-verify.mjs.map +1 -0
- package/dist/config-loader-PPf4CtDj.mjs +43 -0
- package/dist/config-loader-PPf4CtDj.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/exports/config-types.d.mts +2 -0
- package/dist/exports/config-types.mjs +3 -0
- package/dist/exports/control-api.d.mts +621 -0
- package/dist/exports/control-api.d.mts.map +1 -0
- package/dist/exports/control-api.mjs +97 -0
- package/dist/exports/control-api.mjs.map +1 -0
- package/dist/{load-ts-contract.d.ts → exports/index.d.mts} +10 -5
- package/dist/exports/index.d.mts.map +1 -0
- package/dist/exports/index.mjs +130 -0
- package/dist/exports/index.mjs.map +1 -0
- package/dist/extract-sql-ddl-BmlKvk4o.mjs +26 -0
- package/dist/extract-sql-ddl-BmlKvk4o.mjs.map +1 -0
- package/dist/framework-components-CjV_jD8f.mjs +59 -0
- package/dist/framework-components-CjV_jD8f.mjs.map +1 -0
- package/dist/migration-command-scaffold-DfY_F3ev.mjs +97 -0
- package/dist/migration-command-scaffold-DfY_F3ev.mjs.map +1 -0
- package/dist/progress-adapter-DENrzF6I.mjs +49 -0
- package/dist/progress-adapter-DENrzF6I.mjs.map +1 -0
- package/dist/result-handler-iA9JtUC7.mjs +1186 -0
- package/dist/result-handler-iA9JtUC7.mjs.map +1 -0
- package/package.json +75 -38
- package/src/cli.ts +43 -0
- package/src/commands/contract-emit.ts +221 -111
- package/src/commands/db-init.ts +217 -426
- package/src/commands/db-introspect.ts +148 -185
- package/src/commands/db-schema-verify.ts +162 -149
- package/src/commands/db-sign.ts +215 -202
- package/src/commands/db-update.ts +220 -0
- package/src/commands/db-verify.ts +193 -156
- package/src/commands/migration-apply.ts +431 -0
- package/src/commands/migration-plan.ts +446 -0
- package/src/commands/migration-show.ts +255 -0
- package/src/commands/migration-status.ts +436 -0
- package/src/commands/migration-verify.ts +151 -0
- package/src/config-loader.ts +13 -3
- package/src/control-api/client.ts +605 -0
- package/src/control-api/errors.ts +9 -0
- package/src/control-api/operations/contract-emit.ts +161 -0
- package/src/control-api/operations/db-init.ts +286 -0
- package/src/control-api/operations/db-update.ts +221 -0
- package/src/control-api/operations/extract-sql-ddl.ts +47 -0
- package/src/control-api/operations/migration-apply.ts +195 -0
- package/src/control-api/operations/migration-helpers.ts +49 -0
- package/src/control-api/types.ts +687 -0
- package/src/exports/config-types.ts +3 -3
- package/src/exports/control-api.ts +53 -0
- package/src/load-ts-contract.ts +16 -11
- package/src/utils/cli-errors.ts +3 -1
- package/src/utils/command-helpers.ts +92 -3
- package/src/utils/framework-components.ts +11 -30
- package/src/utils/migration-command-scaffold.ts +190 -0
- package/src/utils/output.ts +363 -25
- package/src/utils/progress-adapter.ts +86 -0
- package/dist/chunk-464LNZCE.js +0 -134
- package/dist/chunk-464LNZCE.js.map +0 -1
- package/dist/chunk-BZMBKEEQ.js +0 -997
- package/dist/chunk-BZMBKEEQ.js.map +0 -1
- package/dist/chunk-HWYQOCAJ.js +0 -47
- package/dist/chunk-HWYQOCAJ.js.map +0 -1
- package/dist/chunk-ZKYEJROM.js +0 -94
- package/dist/chunk-ZKYEJROM.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 -9
- 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 -341
- 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 -190
- 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 -164
- 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 -199
- 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 -173
- 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/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/index.d.ts +0 -4
- package/dist/exports/index.d.ts.map +0 -1
- package/dist/exports/index.js +0 -175
- 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 -81
- 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/utils/action.ts +0 -43
- package/src/utils/spinner.ts +0 -67
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
import type { TargetBoundComponentDescriptor } from '@prisma-next/contract/framework-components';
|
|
2
|
+
import type { ContractIR } from '@prisma-next/contract/ir';
|
|
3
|
+
import type { ContractMarkerRecord } from '@prisma-next/contract/types';
|
|
4
|
+
import type { CoreSchemaView } from '@prisma-next/core-control-plane/schema-view';
|
|
5
|
+
import { createControlPlaneStack } from '@prisma-next/core-control-plane/stack';
|
|
6
|
+
import type {
|
|
7
|
+
ControlDriverInstance,
|
|
8
|
+
ControlFamilyInstance,
|
|
9
|
+
ControlPlaneStack,
|
|
10
|
+
SignDatabaseResult,
|
|
11
|
+
VerifyDatabaseResult,
|
|
12
|
+
VerifyDatabaseSchemaResult,
|
|
13
|
+
} from '@prisma-next/core-control-plane/types';
|
|
14
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
15
|
+
import { notOk, ok } from '@prisma-next/utils/result';
|
|
16
|
+
import { assertFrameworkComponentsCompatible } from '../utils/framework-components';
|
|
17
|
+
import { ContractValidationError } from './errors';
|
|
18
|
+
import { executeDbInit } from './operations/db-init';
|
|
19
|
+
import { executeDbUpdate } from './operations/db-update';
|
|
20
|
+
import { executeMigrationApply } from './operations/migration-apply';
|
|
21
|
+
import type {
|
|
22
|
+
ControlActionName,
|
|
23
|
+
ControlClient,
|
|
24
|
+
ControlClientOptions,
|
|
25
|
+
DbInitOptions,
|
|
26
|
+
DbInitResult,
|
|
27
|
+
DbUpdateOptions,
|
|
28
|
+
DbUpdateResult,
|
|
29
|
+
EmitOptions,
|
|
30
|
+
EmitResult,
|
|
31
|
+
IntrospectOptions,
|
|
32
|
+
MigrationApplyOptions,
|
|
33
|
+
MigrationApplyResult,
|
|
34
|
+
OnControlProgress,
|
|
35
|
+
SchemaVerifyOptions,
|
|
36
|
+
SignOptions,
|
|
37
|
+
VerifyOptions,
|
|
38
|
+
} from './types';
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a programmatic control client for Prisma Next operations.
|
|
42
|
+
*
|
|
43
|
+
* The client accepts framework component descriptors at creation time,
|
|
44
|
+
* manages driver lifecycle via connect()/close(), and exposes domain
|
|
45
|
+
* operations that delegate to the existing family instance methods.
|
|
46
|
+
*
|
|
47
|
+
* @see {@link ControlClient} for the client interface
|
|
48
|
+
* @see README.md "Programmatic Control API" section for usage examples
|
|
49
|
+
*/
|
|
50
|
+
export function createControlClient(options: ControlClientOptions): ControlClient {
|
|
51
|
+
return new ControlClientImpl(options);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Implementation of ControlClient.
|
|
56
|
+
* Manages initialization and connection state, delegates operations to family instance.
|
|
57
|
+
*/
|
|
58
|
+
class ControlClientImpl implements ControlClient {
|
|
59
|
+
private readonly options: ControlClientOptions;
|
|
60
|
+
private stack: ControlPlaneStack<string, string> | null = null;
|
|
61
|
+
private driver: ControlDriverInstance<string, string> | null = null;
|
|
62
|
+
private familyInstance: ControlFamilyInstance<string> | null = null;
|
|
63
|
+
private frameworkComponents: ReadonlyArray<
|
|
64
|
+
TargetBoundComponentDescriptor<string, string>
|
|
65
|
+
> | null = null;
|
|
66
|
+
private initialized = false;
|
|
67
|
+
private readonly defaultConnection: unknown;
|
|
68
|
+
|
|
69
|
+
constructor(options: ControlClientOptions) {
|
|
70
|
+
this.options = options;
|
|
71
|
+
this.defaultConnection = options.connection;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
init(): void {
|
|
75
|
+
if (this.initialized) {
|
|
76
|
+
return; // Idempotent
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Create the control plane stack
|
|
80
|
+
this.stack = createControlPlaneStack({
|
|
81
|
+
target: this.options.target,
|
|
82
|
+
adapter: this.options.adapter,
|
|
83
|
+
driver: this.options.driver,
|
|
84
|
+
extensionPacks: this.options.extensionPacks,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Create family instance using the stack
|
|
88
|
+
this.familyInstance = this.options.family.create(this.stack);
|
|
89
|
+
|
|
90
|
+
// Validate and type-narrow framework components
|
|
91
|
+
const rawComponents = [
|
|
92
|
+
this.options.target,
|
|
93
|
+
this.options.adapter,
|
|
94
|
+
...(this.options.extensionPacks ?? []),
|
|
95
|
+
];
|
|
96
|
+
this.frameworkComponents = assertFrameworkComponentsCompatible(
|
|
97
|
+
this.options.family.familyId,
|
|
98
|
+
this.options.target.targetId,
|
|
99
|
+
rawComponents,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
this.initialized = true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async connect(connection?: unknown): Promise<void> {
|
|
106
|
+
// Auto-init if needed
|
|
107
|
+
this.init();
|
|
108
|
+
|
|
109
|
+
if (this.driver) {
|
|
110
|
+
throw new Error('Already connected. Call close() before reconnecting.');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Resolve connection: argument > default from options
|
|
114
|
+
const resolvedConnection = connection ?? this.defaultConnection;
|
|
115
|
+
if (resolvedConnection === undefined) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
'No connection provided. Pass a connection to connect() or provide a default connection when creating the client.',
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Check for driver descriptor
|
|
122
|
+
if (!this.stack?.driver) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
'Driver is not configured. Pass a driver descriptor when creating the control client to enable database operations.',
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Create driver instance
|
|
129
|
+
// Cast through any since connection type is driver-specific at runtime.
|
|
130
|
+
// The driver descriptor is typed with any for TConnection in ControlClientOptions,
|
|
131
|
+
// but createControlPlaneStack defaults it to string. We bridge this at runtime.
|
|
132
|
+
// biome-ignore lint/suspicious/noExplicitAny: required for runtime connection type flexibility
|
|
133
|
+
this.driver = await this.stack?.driver.create(resolvedConnection as any);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async close(): Promise<void> {
|
|
137
|
+
if (this.driver) {
|
|
138
|
+
await this.driver.close();
|
|
139
|
+
this.driver = null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private async ensureConnected(): Promise<{
|
|
144
|
+
driver: ControlDriverInstance<string, string>;
|
|
145
|
+
familyInstance: ControlFamilyInstance<string>;
|
|
146
|
+
frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<string, string>>;
|
|
147
|
+
}> {
|
|
148
|
+
// Auto-init if needed
|
|
149
|
+
this.init();
|
|
150
|
+
|
|
151
|
+
// Auto-connect if not connected and default connection is available
|
|
152
|
+
if (!this.driver && this.defaultConnection !== undefined) {
|
|
153
|
+
await this.connect(this.defaultConnection);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!this.driver || !this.familyInstance || !this.frameworkComponents) {
|
|
157
|
+
throw new Error('Not connected. Call connect(connection) first.');
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
driver: this.driver,
|
|
161
|
+
familyInstance: this.familyInstance,
|
|
162
|
+
frameworkComponents: this.frameworkComponents,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private async connectWithProgress(
|
|
167
|
+
connection: unknown | undefined,
|
|
168
|
+
action: ControlActionName,
|
|
169
|
+
onProgress?: OnControlProgress,
|
|
170
|
+
): Promise<void> {
|
|
171
|
+
if (connection === undefined) return;
|
|
172
|
+
onProgress?.({
|
|
173
|
+
action,
|
|
174
|
+
kind: 'spanStart',
|
|
175
|
+
spanId: 'connect',
|
|
176
|
+
label: 'Connecting to database...',
|
|
177
|
+
});
|
|
178
|
+
try {
|
|
179
|
+
await this.connect(connection);
|
|
180
|
+
onProgress?.({ action, kind: 'spanEnd', spanId: 'connect', outcome: 'ok' });
|
|
181
|
+
} catch (error) {
|
|
182
|
+
onProgress?.({ action, kind: 'spanEnd', spanId: 'connect', outcome: 'error' });
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async verify(options: VerifyOptions): Promise<VerifyDatabaseResult> {
|
|
188
|
+
const { onProgress } = options;
|
|
189
|
+
await this.connectWithProgress(options.connection, 'verify', onProgress);
|
|
190
|
+
const { driver, familyInstance } = await this.ensureConnected();
|
|
191
|
+
|
|
192
|
+
// Validate contract using family instance
|
|
193
|
+
let contractIR: ContractIR;
|
|
194
|
+
try {
|
|
195
|
+
contractIR = familyInstance.validateContractIR(options.contractIR);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
198
|
+
throw new ContractValidationError(message, error);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Emit verify span
|
|
202
|
+
onProgress?.({
|
|
203
|
+
action: 'verify',
|
|
204
|
+
kind: 'spanStart',
|
|
205
|
+
spanId: 'verify',
|
|
206
|
+
label: 'Verifying database signature...',
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
// Delegate to family instance verify method
|
|
211
|
+
// Note: We pass empty strings for contractPath/configPath since the programmatic
|
|
212
|
+
// API doesn't deal with file paths. The family instance accepts these as optional
|
|
213
|
+
// metadata for error reporting.
|
|
214
|
+
const result = await familyInstance.verify({
|
|
215
|
+
driver,
|
|
216
|
+
contractIR,
|
|
217
|
+
expectedTargetId: this.options.target.targetId,
|
|
218
|
+
contractPath: '',
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
onProgress?.({
|
|
222
|
+
action: 'verify',
|
|
223
|
+
kind: 'spanEnd',
|
|
224
|
+
spanId: 'verify',
|
|
225
|
+
outcome: result.ok ? 'ok' : 'error',
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
return result;
|
|
229
|
+
} catch (error) {
|
|
230
|
+
onProgress?.({
|
|
231
|
+
action: 'verify',
|
|
232
|
+
kind: 'spanEnd',
|
|
233
|
+
spanId: 'verify',
|
|
234
|
+
outcome: 'error',
|
|
235
|
+
});
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async schemaVerify(options: SchemaVerifyOptions): Promise<VerifyDatabaseSchemaResult> {
|
|
241
|
+
const { onProgress } = options;
|
|
242
|
+
await this.connectWithProgress(options.connection, 'schemaVerify', onProgress);
|
|
243
|
+
const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
|
|
244
|
+
|
|
245
|
+
// Validate contract using family instance
|
|
246
|
+
let contractIR: ContractIR;
|
|
247
|
+
try {
|
|
248
|
+
contractIR = familyInstance.validateContractIR(options.contractIR);
|
|
249
|
+
} catch (error) {
|
|
250
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
251
|
+
throw new ContractValidationError(message, error);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Emit schemaVerify span
|
|
255
|
+
onProgress?.({
|
|
256
|
+
action: 'schemaVerify',
|
|
257
|
+
kind: 'spanStart',
|
|
258
|
+
spanId: 'schemaVerify',
|
|
259
|
+
label: 'Verifying database schema...',
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
try {
|
|
263
|
+
// Delegate to family instance schemaVerify method
|
|
264
|
+
const result = await familyInstance.schemaVerify({
|
|
265
|
+
driver,
|
|
266
|
+
contractIR,
|
|
267
|
+
strict: options.strict ?? false,
|
|
268
|
+
contractPath: '',
|
|
269
|
+
frameworkComponents,
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
onProgress?.({
|
|
273
|
+
action: 'schemaVerify',
|
|
274
|
+
kind: 'spanEnd',
|
|
275
|
+
spanId: 'schemaVerify',
|
|
276
|
+
outcome: result.ok ? 'ok' : 'error',
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
return result;
|
|
280
|
+
} catch (error) {
|
|
281
|
+
onProgress?.({
|
|
282
|
+
action: 'schemaVerify',
|
|
283
|
+
kind: 'spanEnd',
|
|
284
|
+
spanId: 'schemaVerify',
|
|
285
|
+
outcome: 'error',
|
|
286
|
+
});
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async sign(options: SignOptions): Promise<SignDatabaseResult> {
|
|
292
|
+
const { onProgress } = options;
|
|
293
|
+
await this.connectWithProgress(options.connection, 'sign', onProgress);
|
|
294
|
+
const { driver, familyInstance } = await this.ensureConnected();
|
|
295
|
+
|
|
296
|
+
// Validate contract using family instance
|
|
297
|
+
let contractIR: ContractIR;
|
|
298
|
+
try {
|
|
299
|
+
contractIR = familyInstance.validateContractIR(options.contractIR);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
302
|
+
throw new ContractValidationError(message, error);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Emit sign span
|
|
306
|
+
onProgress?.({
|
|
307
|
+
action: 'sign',
|
|
308
|
+
kind: 'spanStart',
|
|
309
|
+
spanId: 'sign',
|
|
310
|
+
label: 'Signing database...',
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
// Delegate to family instance sign method
|
|
315
|
+
const result = await familyInstance.sign({
|
|
316
|
+
driver,
|
|
317
|
+
contractIR,
|
|
318
|
+
contractPath: options.contractPath ?? '',
|
|
319
|
+
...ifDefined('configPath', options.configPath),
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
onProgress?.({
|
|
323
|
+
action: 'sign',
|
|
324
|
+
kind: 'spanEnd',
|
|
325
|
+
spanId: 'sign',
|
|
326
|
+
outcome: 'ok',
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
return result;
|
|
330
|
+
} catch (error) {
|
|
331
|
+
onProgress?.({
|
|
332
|
+
action: 'sign',
|
|
333
|
+
kind: 'spanEnd',
|
|
334
|
+
spanId: 'sign',
|
|
335
|
+
outcome: 'error',
|
|
336
|
+
});
|
|
337
|
+
throw error;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async dbInit(options: DbInitOptions): Promise<DbInitResult> {
|
|
342
|
+
const { onProgress } = options;
|
|
343
|
+
await this.connectWithProgress(options.connection, 'dbInit', onProgress);
|
|
344
|
+
const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
|
|
345
|
+
|
|
346
|
+
// Check target supports migrations
|
|
347
|
+
if (!this.options.target.migrations) {
|
|
348
|
+
throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Validate contract using family instance
|
|
352
|
+
let contractIR: ContractIR;
|
|
353
|
+
try {
|
|
354
|
+
contractIR = familyInstance.validateContractIR(options.contractIR);
|
|
355
|
+
} catch (error) {
|
|
356
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
357
|
+
throw new ContractValidationError(message, error);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Delegate to extracted dbInit operation
|
|
361
|
+
return executeDbInit({
|
|
362
|
+
driver,
|
|
363
|
+
familyInstance,
|
|
364
|
+
contractIR,
|
|
365
|
+
mode: options.mode,
|
|
366
|
+
migrations: this.options.target.migrations,
|
|
367
|
+
frameworkComponents,
|
|
368
|
+
...ifDefined('onProgress', onProgress),
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async dbUpdate(options: DbUpdateOptions): Promise<DbUpdateResult> {
|
|
373
|
+
const { onProgress } = options;
|
|
374
|
+
await this.connectWithProgress(options.connection, 'dbUpdate', onProgress);
|
|
375
|
+
const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
|
|
376
|
+
|
|
377
|
+
if (!this.options.target.migrations) {
|
|
378
|
+
throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
let contractIR: ContractIR;
|
|
382
|
+
try {
|
|
383
|
+
contractIR = familyInstance.validateContractIR(options.contractIR);
|
|
384
|
+
} catch (error) {
|
|
385
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
386
|
+
throw new ContractValidationError(message, error);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return executeDbUpdate({
|
|
390
|
+
driver,
|
|
391
|
+
familyInstance,
|
|
392
|
+
contractIR,
|
|
393
|
+
mode: options.mode,
|
|
394
|
+
migrations: this.options.target.migrations,
|
|
395
|
+
frameworkComponents,
|
|
396
|
+
...ifDefined('acceptDataLoss', options.acceptDataLoss),
|
|
397
|
+
...ifDefined('onProgress', onProgress),
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async readMarker(): Promise<ContractMarkerRecord | null> {
|
|
402
|
+
const { driver, familyInstance } = await this.ensureConnected();
|
|
403
|
+
return familyInstance.readMarker({ driver });
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async migrationApply(options: MigrationApplyOptions): Promise<MigrationApplyResult> {
|
|
407
|
+
const { onProgress } = options;
|
|
408
|
+
await this.connectWithProgress(options.connection, 'migrationApply', onProgress);
|
|
409
|
+
const { driver, familyInstance, frameworkComponents } = await this.ensureConnected();
|
|
410
|
+
|
|
411
|
+
if (!this.options.target.migrations) {
|
|
412
|
+
throw new Error(`Target "${this.options.target.targetId}" does not support migrations`);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return executeMigrationApply({
|
|
416
|
+
driver,
|
|
417
|
+
familyInstance,
|
|
418
|
+
originHash: options.originHash,
|
|
419
|
+
destinationHash: options.destinationHash,
|
|
420
|
+
pendingMigrations: options.pendingMigrations,
|
|
421
|
+
migrations: this.options.target.migrations,
|
|
422
|
+
frameworkComponents,
|
|
423
|
+
targetId: this.options.target.targetId,
|
|
424
|
+
...(onProgress ? { onProgress } : {}),
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async introspect(options?: IntrospectOptions): Promise<unknown> {
|
|
429
|
+
const onProgress = options?.onProgress;
|
|
430
|
+
await this.connectWithProgress(options?.connection, 'introspect', onProgress);
|
|
431
|
+
const { driver, familyInstance } = await this.ensureConnected();
|
|
432
|
+
|
|
433
|
+
// TODO: Pass schema option to familyInstance.introspect when schema filtering is implemented
|
|
434
|
+
const _schema = options?.schema;
|
|
435
|
+
void _schema;
|
|
436
|
+
|
|
437
|
+
// Emit introspect span
|
|
438
|
+
onProgress?.({
|
|
439
|
+
action: 'introspect',
|
|
440
|
+
kind: 'spanStart',
|
|
441
|
+
spanId: 'introspect',
|
|
442
|
+
label: 'Introspecting database schema...',
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
try {
|
|
446
|
+
const result = await familyInstance.introspect({ driver });
|
|
447
|
+
|
|
448
|
+
onProgress?.({
|
|
449
|
+
action: 'introspect',
|
|
450
|
+
kind: 'spanEnd',
|
|
451
|
+
spanId: 'introspect',
|
|
452
|
+
outcome: 'ok',
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
return result;
|
|
456
|
+
} catch (error) {
|
|
457
|
+
onProgress?.({
|
|
458
|
+
action: 'introspect',
|
|
459
|
+
kind: 'spanEnd',
|
|
460
|
+
spanId: 'introspect',
|
|
461
|
+
outcome: 'error',
|
|
462
|
+
});
|
|
463
|
+
throw error;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
toSchemaView(schemaIR: unknown): CoreSchemaView | undefined {
|
|
468
|
+
this.init();
|
|
469
|
+
if (this.familyInstance?.toSchemaView) {
|
|
470
|
+
return this.familyInstance.toSchemaView(schemaIR);
|
|
471
|
+
}
|
|
472
|
+
return undefined;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async emit(options: EmitOptions): Promise<EmitResult> {
|
|
476
|
+
const { onProgress, contractConfig } = options;
|
|
477
|
+
|
|
478
|
+
// Ensure initialized (creates stack and family instance)
|
|
479
|
+
// emit() does NOT require a database connection
|
|
480
|
+
this.init();
|
|
481
|
+
|
|
482
|
+
if (!this.familyInstance) {
|
|
483
|
+
throw new Error('Family instance was not initialized. This is a bug.');
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
let contractRaw: unknown;
|
|
487
|
+
onProgress?.({
|
|
488
|
+
action: 'emit',
|
|
489
|
+
kind: 'spanStart',
|
|
490
|
+
spanId: 'resolveSource',
|
|
491
|
+
label: 'Resolving contract source...',
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
try {
|
|
495
|
+
const providerResult = await contractConfig.sourceProvider();
|
|
496
|
+
if (!providerResult.ok) {
|
|
497
|
+
onProgress?.({
|
|
498
|
+
action: 'emit',
|
|
499
|
+
kind: 'spanEnd',
|
|
500
|
+
spanId: 'resolveSource',
|
|
501
|
+
outcome: 'error',
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
return notOk({
|
|
505
|
+
code: 'CONTRACT_SOURCE_INVALID',
|
|
506
|
+
summary: providerResult.failure.summary,
|
|
507
|
+
why: providerResult.failure.summary,
|
|
508
|
+
meta: providerResult.failure.meta,
|
|
509
|
+
diagnostics: providerResult.failure,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
contractRaw = providerResult.value;
|
|
513
|
+
|
|
514
|
+
onProgress?.({
|
|
515
|
+
action: 'emit',
|
|
516
|
+
kind: 'spanEnd',
|
|
517
|
+
spanId: 'resolveSource',
|
|
518
|
+
outcome: 'ok',
|
|
519
|
+
});
|
|
520
|
+
} catch (error) {
|
|
521
|
+
onProgress?.({
|
|
522
|
+
action: 'emit',
|
|
523
|
+
kind: 'spanEnd',
|
|
524
|
+
spanId: 'resolveSource',
|
|
525
|
+
outcome: 'error',
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
529
|
+
return notOk({
|
|
530
|
+
code: 'CONTRACT_SOURCE_INVALID',
|
|
531
|
+
summary: 'Failed to resolve contract source',
|
|
532
|
+
why: message,
|
|
533
|
+
diagnostics: {
|
|
534
|
+
summary: 'Contract source provider threw an exception',
|
|
535
|
+
diagnostics: [
|
|
536
|
+
{
|
|
537
|
+
code: 'PROVIDER_THROW',
|
|
538
|
+
message,
|
|
539
|
+
},
|
|
540
|
+
],
|
|
541
|
+
},
|
|
542
|
+
meta: undefined,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Emit contract
|
|
547
|
+
onProgress?.({
|
|
548
|
+
action: 'emit',
|
|
549
|
+
kind: 'spanStart',
|
|
550
|
+
spanId: 'emit',
|
|
551
|
+
label: 'Emitting contract...',
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
try {
|
|
555
|
+
try {
|
|
556
|
+
this.familyInstance.validateContractIR(contractRaw);
|
|
557
|
+
} catch (error) {
|
|
558
|
+
onProgress?.({
|
|
559
|
+
action: 'emit',
|
|
560
|
+
kind: 'spanEnd',
|
|
561
|
+
spanId: 'emit',
|
|
562
|
+
outcome: 'error',
|
|
563
|
+
});
|
|
564
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
565
|
+
return notOk({
|
|
566
|
+
code: 'CONTRACT_VALIDATION_FAILED',
|
|
567
|
+
summary: 'Contract validation failed',
|
|
568
|
+
why: message,
|
|
569
|
+
meta: undefined,
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const emitResult = await this.familyInstance.emitContract({ contractIR: contractRaw });
|
|
574
|
+
|
|
575
|
+
onProgress?.({
|
|
576
|
+
action: 'emit',
|
|
577
|
+
kind: 'spanEnd',
|
|
578
|
+
spanId: 'emit',
|
|
579
|
+
outcome: 'ok',
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
return ok({
|
|
583
|
+
storageHash: emitResult.storageHash,
|
|
584
|
+
...ifDefined('executionHash', emitResult.executionHash),
|
|
585
|
+
profileHash: emitResult.profileHash,
|
|
586
|
+
contractJson: emitResult.contractJson,
|
|
587
|
+
contractDts: emitResult.contractDts,
|
|
588
|
+
});
|
|
589
|
+
} catch (error) {
|
|
590
|
+
onProgress?.({
|
|
591
|
+
action: 'emit',
|
|
592
|
+
kind: 'spanEnd',
|
|
593
|
+
spanId: 'emit',
|
|
594
|
+
outcome: 'error',
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
return notOk({
|
|
598
|
+
code: 'EMIT_FAILED',
|
|
599
|
+
summary: 'Failed to emit contract',
|
|
600
|
+
why: error instanceof Error ? error.message : String(error),
|
|
601
|
+
meta: undefined,
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|