@prisma-next/family-sql 0.3.0-dev.12 → 0.3.0-dev.122
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 +34 -7
- package/dist/assembly-Dzumaba1.mjs +159 -0
- package/dist/assembly-Dzumaba1.mjs.map +1 -0
- package/dist/control-adapter.d.mts +60 -0
- package/dist/control-adapter.d.mts.map +1 -0
- package/dist/control-adapter.mjs +1 -0
- package/dist/control-instance-BKuHINR7.d.mts +411 -0
- package/dist/control-instance-BKuHINR7.d.mts.map +1 -0
- package/dist/control.d.mts +128 -0
- package/dist/control.d.mts.map +1 -0
- package/dist/control.mjs +683 -0
- package/dist/control.mjs.map +1 -0
- package/dist/runtime.d.mts +27 -0
- package/dist/runtime.d.mts.map +1 -0
- package/dist/runtime.mjs +38 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/schema-verify.d.mts +48 -0
- package/dist/schema-verify.d.mts.map +1 -0
- package/dist/schema-verify.mjs +4 -0
- package/dist/test-utils.d.mts +2 -0
- package/dist/test-utils.mjs +3 -0
- package/dist/verify-BfMETJcM.mjs +108 -0
- package/dist/verify-BfMETJcM.mjs.map +1 -0
- package/dist/verify-sql-schema-C3Pit9o4.mjs +1085 -0
- package/dist/verify-sql-schema-C3Pit9o4.mjs.map +1 -0
- package/dist/verify-sql-schema-DhHnkpPa.d.mts +67 -0
- package/dist/verify-sql-schema-DhHnkpPa.d.mts.map +1 -0
- package/dist/verify.d.mts +31 -0
- package/dist/verify.d.mts.map +1 -0
- package/dist/verify.mjs +3 -0
- package/package.json +35 -46
- package/src/core/assembly.ts +265 -59
- package/src/core/control-adapter.ts +15 -0
- package/src/core/{descriptor.ts → control-descriptor.ts} +15 -11
- package/src/core/{instance.ts → control-instance.ts} +106 -248
- package/src/core/migrations/contract-to-schema-ir.ts +265 -0
- package/src/core/migrations/types.ts +193 -168
- package/src/core/runtime-descriptor.ts +19 -41
- package/src/core/runtime-instance.ts +11 -133
- package/src/core/schema-verify/verify-helpers.ts +201 -105
- package/src/core/schema-verify/verify-sql-schema.ts +918 -413
- package/src/core/verify.ts +4 -13
- package/src/exports/control.ts +29 -6
- package/src/exports/runtime.ts +2 -6
- package/src/exports/schema-verify.ts +10 -2
- package/src/exports/test-utils.ts +1 -1
- package/dist/chunk-BHEGVBY7.js +0 -772
- package/dist/chunk-BHEGVBY7.js.map +0 -1
- package/dist/chunk-SQ2VWYDV.js +0 -589
- package/dist/chunk-SQ2VWYDV.js.map +0 -1
- package/dist/chunk-SU7LN2UH.js +0 -96
- package/dist/chunk-SU7LN2UH.js.map +0 -1
- package/dist/core/assembly.d.ts +0 -25
- package/dist/core/assembly.d.ts.map +0 -1
- package/dist/core/control-adapter.d.ts +0 -42
- package/dist/core/control-adapter.d.ts.map +0 -1
- package/dist/core/descriptor.d.ts +0 -24
- package/dist/core/descriptor.d.ts.map +0 -1
- package/dist/core/instance.d.ts +0 -140
- package/dist/core/instance.d.ts.map +0 -1
- package/dist/core/migrations/plan-helpers.d.ts +0 -20
- package/dist/core/migrations/plan-helpers.d.ts.map +0 -1
- package/dist/core/migrations/policies.d.ts +0 -6
- package/dist/core/migrations/policies.d.ts.map +0 -1
- package/dist/core/migrations/types.d.ts +0 -280
- package/dist/core/migrations/types.d.ts.map +0 -1
- package/dist/core/runtime-descriptor.d.ts +0 -19
- package/dist/core/runtime-descriptor.d.ts.map +0 -1
- package/dist/core/runtime-instance.d.ts +0 -54
- package/dist/core/runtime-instance.d.ts.map +0 -1
- package/dist/core/schema-verify/verify-helpers.d.ts +0 -50
- package/dist/core/schema-verify/verify-helpers.d.ts.map +0 -1
- package/dist/core/schema-verify/verify-sql-schema.d.ts +0 -45
- package/dist/core/schema-verify/verify-sql-schema.d.ts.map +0 -1
- package/dist/core/verify.d.ts +0 -39
- package/dist/core/verify.d.ts.map +0 -1
- package/dist/exports/control-adapter.d.ts +0 -2
- package/dist/exports/control-adapter.d.ts.map +0 -1
- package/dist/exports/control-adapter.js +0 -1
- package/dist/exports/control-adapter.js.map +0 -1
- package/dist/exports/control.d.ts +0 -13
- package/dist/exports/control.d.ts.map +0 -1
- package/dist/exports/control.js +0 -149
- package/dist/exports/control.js.map +0 -1
- package/dist/exports/runtime.d.ts +0 -8
- package/dist/exports/runtime.d.ts.map +0 -1
- package/dist/exports/runtime.js +0 -64
- package/dist/exports/runtime.js.map +0 -1
- package/dist/exports/schema-verify.d.ts +0 -11
- package/dist/exports/schema-verify.d.ts.map +0 -1
- package/dist/exports/schema-verify.js +0 -11
- package/dist/exports/schema-verify.js.map +0 -1
- package/dist/exports/test-utils.d.ts +0 -7
- package/dist/exports/test-utils.d.ts.map +0 -1
- package/dist/exports/test-utils.js +0 -17
- package/dist/exports/test-utils.js.map +0 -1
- package/dist/exports/verify.d.ts +0 -2
- package/dist/exports/verify.d.ts.map +0 -1
- package/dist/exports/verify.js +0 -11
- package/dist/exports/verify.js.map +0 -1
package/dist/control.mjs
ADDED
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
import { a as extractCodecTypeImports, c as extractParameterizedRenderers, i as extractCodecControlHooks, l as extractParameterizedTypeImports, n as assembleOperationRegistry, o as extractExtensionIds, r as assemblePslInterpretationContributions, s as extractOperationTypeImports, t as assembleControlMutationDefaultContributions, u as extractQueryOperationTypeImports } from "./assembly-Dzumaba1.mjs";
|
|
2
|
+
import { o as collectInitDependencies, s as isDatabaseDependencyProvider, t as verifySqlSchema } from "./verify-sql-schema-C3Pit9o4.mjs";
|
|
3
|
+
import { r as readMarker, t as collectSupportedCodecTypeIds } from "./verify-BfMETJcM.mjs";
|
|
4
|
+
import { sqlTargetFamilyHook } from "@prisma-next/sql-contract-emitter";
|
|
5
|
+
import { emit } from "@prisma-next/core-control-plane/emission";
|
|
6
|
+
import { validateContract } from "@prisma-next/sql-contract/validate";
|
|
7
|
+
import { ensureSchemaStatement, ensureTableStatement, writeContractMarker } from "@prisma-next/sql-runtime";
|
|
8
|
+
import { defaultIndexName } from "@prisma-next/sql-schema-ir/naming";
|
|
9
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
10
|
+
import { notOk, ok } from "@prisma-next/utils/result";
|
|
11
|
+
|
|
12
|
+
//#region src/core/control-instance.ts
|
|
13
|
+
function extractCodecTypeIdsFromContract(contract) {
|
|
14
|
+
const typeIds = /* @__PURE__ */ new Set();
|
|
15
|
+
if (typeof contract === "object" && contract !== null && "storage" in contract && typeof contract.storage === "object" && contract.storage !== null && "tables" in contract.storage) {
|
|
16
|
+
const storage = contract.storage;
|
|
17
|
+
if (storage.tables && typeof storage.tables === "object") {
|
|
18
|
+
for (const table of Object.values(storage.tables)) if (typeof table === "object" && table !== null && "columns" in table && typeof table.columns === "object" && table.columns !== null) {
|
|
19
|
+
const columns = table.columns;
|
|
20
|
+
for (const column of Object.values(columns)) if (column && typeof column === "object" && "codecId" in column && typeof column.codecId === "string") typeIds.add(column.codecId);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return Array.from(typeIds).sort();
|
|
25
|
+
}
|
|
26
|
+
function createVerifyResult(options) {
|
|
27
|
+
const contract = { storageHash: options.contractStorageHash };
|
|
28
|
+
if (options.contractProfileHash) contract.profileHash = options.contractProfileHash;
|
|
29
|
+
const target = { expected: options.expectedTargetId };
|
|
30
|
+
if (options.actualTargetId) target.actual = options.actualTargetId;
|
|
31
|
+
const meta = { contractPath: options.contractPath };
|
|
32
|
+
if (options.configPath) meta.configPath = options.configPath;
|
|
33
|
+
const result = {
|
|
34
|
+
ok: options.ok,
|
|
35
|
+
summary: options.summary,
|
|
36
|
+
contract,
|
|
37
|
+
target,
|
|
38
|
+
meta,
|
|
39
|
+
timings: { total: options.totalTime }
|
|
40
|
+
};
|
|
41
|
+
if (options.code) result.code = options.code;
|
|
42
|
+
if (options.marker) result.marker = {
|
|
43
|
+
storageHash: options.marker.storageHash,
|
|
44
|
+
profileHash: options.marker.profileHash
|
|
45
|
+
};
|
|
46
|
+
if (options.missingCodecs) result.missingCodecs = options.missingCodecs;
|
|
47
|
+
if (options.codecCoverageSkipped) result.codecCoverageSkipped = options.codecCoverageSkipped;
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
function isSqlControlAdapter(value) {
|
|
51
|
+
return typeof value === "object" && value !== null && "introspect" in value && typeof value.introspect === "function";
|
|
52
|
+
}
|
|
53
|
+
function buildSqlTypeMetadataRegistry(options) {
|
|
54
|
+
const { target, adapter, extensionPacks: extensions } = options;
|
|
55
|
+
const registry = /* @__PURE__ */ new Map();
|
|
56
|
+
const targetId = adapter.targetId;
|
|
57
|
+
const descriptors = [
|
|
58
|
+
target,
|
|
59
|
+
adapter,
|
|
60
|
+
...extensions
|
|
61
|
+
];
|
|
62
|
+
for (const descriptor of descriptors) {
|
|
63
|
+
const storageTypes = descriptor.types?.storage;
|
|
64
|
+
if (!storageTypes) continue;
|
|
65
|
+
for (const storageType of storageTypes) if (storageType.familyId === "sql" && storageType.targetId === targetId) registry.set(storageType.typeId, {
|
|
66
|
+
typeId: storageType.typeId,
|
|
67
|
+
familyId: "sql",
|
|
68
|
+
targetId: storageType.targetId,
|
|
69
|
+
...storageType.nativeType !== void 0 ? { nativeType: storageType.nativeType } : {}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return registry;
|
|
73
|
+
}
|
|
74
|
+
function createSqlFamilyInstance(options) {
|
|
75
|
+
const { target, adapter, extensionPacks: extensions = [] } = options;
|
|
76
|
+
const descriptors = [
|
|
77
|
+
target,
|
|
78
|
+
adapter,
|
|
79
|
+
...extensions
|
|
80
|
+
];
|
|
81
|
+
const operationRegistry = assembleOperationRegistry(descriptors);
|
|
82
|
+
const codecTypeImports = extractCodecTypeImports(descriptors);
|
|
83
|
+
const operationTypeImports = extractOperationTypeImports(descriptors);
|
|
84
|
+
const queryOperationTypeImports = extractQueryOperationTypeImports(descriptors);
|
|
85
|
+
const extensionIds = extractExtensionIds(adapter, target, extensions);
|
|
86
|
+
const parameterizedRenderers = extractParameterizedRenderers(descriptors);
|
|
87
|
+
const parameterizedTypeImports = extractParameterizedTypeImports(descriptors);
|
|
88
|
+
const typeMetadataRegistry = buildSqlTypeMetadataRegistry({
|
|
89
|
+
target,
|
|
90
|
+
adapter,
|
|
91
|
+
extensionPacks: extensions
|
|
92
|
+
});
|
|
93
|
+
function stripMappings(contract) {
|
|
94
|
+
if (typeof contract === "object" && contract !== null && "mappings" in contract) {
|
|
95
|
+
const { mappings: _mappings, ...contractIR } = contract;
|
|
96
|
+
return contractIR;
|
|
97
|
+
}
|
|
98
|
+
return contract;
|
|
99
|
+
}
|
|
100
|
+
function normalizeProviderContractIR(contract) {
|
|
101
|
+
const { mappings: _mappings, ...contractIR } = validateContract(stripMappings(contract));
|
|
102
|
+
return contractIR;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
familyId: "sql",
|
|
106
|
+
operationRegistry,
|
|
107
|
+
codecTypeImports,
|
|
108
|
+
operationTypeImports,
|
|
109
|
+
extensionIds,
|
|
110
|
+
typeMetadataRegistry,
|
|
111
|
+
validateContractIR(contractJson) {
|
|
112
|
+
return normalizeProviderContractIR(contractJson);
|
|
113
|
+
},
|
|
114
|
+
async verify(verifyOptions) {
|
|
115
|
+
const { driver, contractIR, expectedTargetId, contractPath, configPath } = verifyOptions;
|
|
116
|
+
const startTime = Date.now();
|
|
117
|
+
if (typeof contractIR !== "object" || contractIR === null || !("storageHash" in contractIR) || !("target" in contractIR) || typeof contractIR.storageHash !== "string" || typeof contractIR.target !== "string") throw new Error("Contract is missing required fields: storageHash or target");
|
|
118
|
+
const contractStorageHash = contractIR.storageHash;
|
|
119
|
+
const contractProfileHash = "profileHash" in contractIR && typeof contractIR.profileHash === "string" ? contractIR.profileHash : void 0;
|
|
120
|
+
const contractTarget = contractIR.target;
|
|
121
|
+
const marker = await readMarker(driver);
|
|
122
|
+
let missingCodecs;
|
|
123
|
+
let codecCoverageSkipped = false;
|
|
124
|
+
const supportedTypeIds = collectSupportedCodecTypeIds([
|
|
125
|
+
adapter,
|
|
126
|
+
target,
|
|
127
|
+
...extensions
|
|
128
|
+
]);
|
|
129
|
+
if (supportedTypeIds.length === 0) codecCoverageSkipped = true;
|
|
130
|
+
else {
|
|
131
|
+
const supportedSet = new Set(supportedTypeIds);
|
|
132
|
+
const missing = extractCodecTypeIdsFromContract(contractIR).filter((id) => !supportedSet.has(id));
|
|
133
|
+
if (missing.length > 0) missingCodecs = missing;
|
|
134
|
+
}
|
|
135
|
+
if (!marker) return createVerifyResult({
|
|
136
|
+
ok: false,
|
|
137
|
+
code: "PN-RUN-3001",
|
|
138
|
+
summary: "Marker missing",
|
|
139
|
+
contractStorageHash,
|
|
140
|
+
expectedTargetId,
|
|
141
|
+
contractPath,
|
|
142
|
+
totalTime: Date.now() - startTime,
|
|
143
|
+
...contractProfileHash ? { contractProfileHash } : {},
|
|
144
|
+
...missingCodecs ? { missingCodecs } : {},
|
|
145
|
+
...codecCoverageSkipped ? { codecCoverageSkipped } : {},
|
|
146
|
+
...configPath ? { configPath } : {}
|
|
147
|
+
});
|
|
148
|
+
if (contractTarget !== expectedTargetId) return createVerifyResult({
|
|
149
|
+
ok: false,
|
|
150
|
+
code: "PN-RUN-3003",
|
|
151
|
+
summary: "Target mismatch",
|
|
152
|
+
contractStorageHash,
|
|
153
|
+
marker,
|
|
154
|
+
expectedTargetId,
|
|
155
|
+
actualTargetId: contractTarget,
|
|
156
|
+
contractPath,
|
|
157
|
+
totalTime: Date.now() - startTime,
|
|
158
|
+
...contractProfileHash ? { contractProfileHash } : {},
|
|
159
|
+
...missingCodecs ? { missingCodecs } : {},
|
|
160
|
+
...codecCoverageSkipped ? { codecCoverageSkipped } : {},
|
|
161
|
+
...configPath ? { configPath } : {}
|
|
162
|
+
});
|
|
163
|
+
if (marker.storageHash !== contractStorageHash) return createVerifyResult({
|
|
164
|
+
ok: false,
|
|
165
|
+
code: "PN-RUN-3002",
|
|
166
|
+
summary: "Hash mismatch",
|
|
167
|
+
contractStorageHash,
|
|
168
|
+
marker,
|
|
169
|
+
expectedTargetId,
|
|
170
|
+
contractPath,
|
|
171
|
+
totalTime: Date.now() - startTime,
|
|
172
|
+
...contractProfileHash ? { contractProfileHash } : {},
|
|
173
|
+
...missingCodecs ? { missingCodecs } : {},
|
|
174
|
+
...codecCoverageSkipped ? { codecCoverageSkipped } : {},
|
|
175
|
+
...configPath ? { configPath } : {}
|
|
176
|
+
});
|
|
177
|
+
if (contractProfileHash && marker.profileHash !== contractProfileHash) return createVerifyResult({
|
|
178
|
+
ok: false,
|
|
179
|
+
code: "PN-RUN-3002",
|
|
180
|
+
summary: "Hash mismatch",
|
|
181
|
+
contractStorageHash,
|
|
182
|
+
contractProfileHash,
|
|
183
|
+
marker,
|
|
184
|
+
expectedTargetId,
|
|
185
|
+
contractPath,
|
|
186
|
+
totalTime: Date.now() - startTime,
|
|
187
|
+
...missingCodecs ? { missingCodecs } : {},
|
|
188
|
+
...codecCoverageSkipped ? { codecCoverageSkipped } : {},
|
|
189
|
+
...configPath ? { configPath } : {}
|
|
190
|
+
});
|
|
191
|
+
return createVerifyResult({
|
|
192
|
+
ok: true,
|
|
193
|
+
summary: "Database matches contract",
|
|
194
|
+
contractStorageHash,
|
|
195
|
+
marker,
|
|
196
|
+
expectedTargetId,
|
|
197
|
+
contractPath,
|
|
198
|
+
totalTime: Date.now() - startTime,
|
|
199
|
+
...contractProfileHash ? { contractProfileHash } : {},
|
|
200
|
+
...missingCodecs ? { missingCodecs } : {},
|
|
201
|
+
...codecCoverageSkipped ? { codecCoverageSkipped } : {},
|
|
202
|
+
...configPath ? { configPath } : {}
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
async schemaVerify(options$1) {
|
|
206
|
+
const { driver, contractIR, strict, context, frameworkComponents } = options$1;
|
|
207
|
+
const contract = validateContract(contractIR);
|
|
208
|
+
const controlAdapter = adapter.create();
|
|
209
|
+
if (!isSqlControlAdapter(controlAdapter)) throw new Error("Adapter does not implement SqlControlAdapter.introspect()");
|
|
210
|
+
return verifySqlSchema({
|
|
211
|
+
contract,
|
|
212
|
+
schema: await controlAdapter.introspect(driver, contractIR),
|
|
213
|
+
strict,
|
|
214
|
+
...ifDefined("context", context),
|
|
215
|
+
typeMetadataRegistry,
|
|
216
|
+
frameworkComponents,
|
|
217
|
+
...ifDefined("normalizeDefault", controlAdapter.normalizeDefault),
|
|
218
|
+
...ifDefined("normalizeNativeType", controlAdapter.normalizeNativeType)
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
async sign(options$1) {
|
|
222
|
+
const { driver, contractIR, contractPath, configPath } = options$1;
|
|
223
|
+
const startTime = Date.now();
|
|
224
|
+
const contract = validateContract(contractIR);
|
|
225
|
+
const contractStorageHash = contract.storageHash;
|
|
226
|
+
const contractProfileHash = "profileHash" in contract && typeof contract.profileHash === "string" ? contract.profileHash : contractStorageHash;
|
|
227
|
+
const contractTarget = contract.target;
|
|
228
|
+
await driver.query(ensureSchemaStatement.sql, ensureSchemaStatement.params);
|
|
229
|
+
await driver.query(ensureTableStatement.sql, ensureTableStatement.params);
|
|
230
|
+
const existingMarker = await readMarker(driver);
|
|
231
|
+
let markerCreated = false;
|
|
232
|
+
let markerUpdated = false;
|
|
233
|
+
let previousHashes;
|
|
234
|
+
if (!existingMarker) {
|
|
235
|
+
const write = writeContractMarker({
|
|
236
|
+
storageHash: contractStorageHash,
|
|
237
|
+
profileHash: contractProfileHash,
|
|
238
|
+
contractJson: contractIR,
|
|
239
|
+
canonicalVersion: 1
|
|
240
|
+
});
|
|
241
|
+
await driver.query(write.insert.sql, write.insert.params);
|
|
242
|
+
markerCreated = true;
|
|
243
|
+
} else {
|
|
244
|
+
const existingStorageHash = existingMarker.storageHash;
|
|
245
|
+
const existingProfileHash = existingMarker.profileHash;
|
|
246
|
+
if (!(existingStorageHash === contractStorageHash) || !(existingProfileHash === contractProfileHash)) {
|
|
247
|
+
previousHashes = {
|
|
248
|
+
storageHash: existingStorageHash,
|
|
249
|
+
profileHash: existingProfileHash
|
|
250
|
+
};
|
|
251
|
+
const write = writeContractMarker({
|
|
252
|
+
storageHash: contractStorageHash,
|
|
253
|
+
profileHash: contractProfileHash,
|
|
254
|
+
contractJson: contractIR,
|
|
255
|
+
canonicalVersion: existingMarker.canonicalVersion ?? 1
|
|
256
|
+
});
|
|
257
|
+
await driver.query(write.update.sql, write.update.params);
|
|
258
|
+
markerUpdated = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
let summary;
|
|
262
|
+
if (markerCreated) summary = "Database signed (marker created)";
|
|
263
|
+
else if (markerUpdated) summary = `Database signed (marker updated from ${previousHashes?.storageHash ?? "unknown"})`;
|
|
264
|
+
else summary = "Database already signed with this contract";
|
|
265
|
+
const totalTime = Date.now() - startTime;
|
|
266
|
+
return {
|
|
267
|
+
ok: true,
|
|
268
|
+
summary,
|
|
269
|
+
contract: {
|
|
270
|
+
storageHash: contractStorageHash,
|
|
271
|
+
profileHash: contractProfileHash
|
|
272
|
+
},
|
|
273
|
+
target: {
|
|
274
|
+
expected: contractTarget,
|
|
275
|
+
actual: contractTarget
|
|
276
|
+
},
|
|
277
|
+
marker: {
|
|
278
|
+
created: markerCreated,
|
|
279
|
+
updated: markerUpdated,
|
|
280
|
+
...previousHashes ? { previous: previousHashes } : {}
|
|
281
|
+
},
|
|
282
|
+
meta: {
|
|
283
|
+
contractPath,
|
|
284
|
+
...configPath ? { configPath } : {}
|
|
285
|
+
},
|
|
286
|
+
timings: { total: totalTime }
|
|
287
|
+
};
|
|
288
|
+
},
|
|
289
|
+
async readMarker(options$1) {
|
|
290
|
+
return readMarker(options$1.driver);
|
|
291
|
+
},
|
|
292
|
+
async introspect(options$1) {
|
|
293
|
+
const { driver, contractIR } = options$1;
|
|
294
|
+
const controlAdapter = adapter.create();
|
|
295
|
+
if (!isSqlControlAdapter(controlAdapter)) throw new Error("Adapter does not implement SqlControlAdapter.introspect()");
|
|
296
|
+
return controlAdapter.introspect(driver, contractIR);
|
|
297
|
+
},
|
|
298
|
+
toSchemaView(schema) {
|
|
299
|
+
const rootLabel = "contract";
|
|
300
|
+
const tableNodes = Object.entries(schema.tables).map(([tableName, table]) => {
|
|
301
|
+
const children = [];
|
|
302
|
+
const columnNodes = [];
|
|
303
|
+
for (const [columnName, column] of Object.entries(table.columns)) {
|
|
304
|
+
const label = `${columnName}: ${column.nativeType} (${column.nullable ? "nullable" : "not nullable"})`;
|
|
305
|
+
columnNodes.push({
|
|
306
|
+
kind: "field",
|
|
307
|
+
id: `column-${tableName}-${columnName}`,
|
|
308
|
+
label,
|
|
309
|
+
meta: {
|
|
310
|
+
nativeType: column.nativeType,
|
|
311
|
+
nullable: column.nullable,
|
|
312
|
+
...ifDefined("default", column.default)
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
if (columnNodes.length > 0) children.push({
|
|
317
|
+
kind: "collection",
|
|
318
|
+
id: `columns-${tableName}`,
|
|
319
|
+
label: "columns",
|
|
320
|
+
children: columnNodes
|
|
321
|
+
});
|
|
322
|
+
if (table.primaryKey) {
|
|
323
|
+
const pkColumns = table.primaryKey.columns.join(", ");
|
|
324
|
+
children.push({
|
|
325
|
+
kind: "index",
|
|
326
|
+
id: `primary-key-${tableName}`,
|
|
327
|
+
label: `primary key: ${pkColumns}`,
|
|
328
|
+
meta: {
|
|
329
|
+
columns: table.primaryKey.columns,
|
|
330
|
+
...table.primaryKey.name ? { name: table.primaryKey.name } : {}
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
for (const unique of table.uniques) {
|
|
335
|
+
const name = unique.name ?? `${tableName}_${unique.columns.join("_")}_unique`;
|
|
336
|
+
const label = `unique ${name}`;
|
|
337
|
+
children.push({
|
|
338
|
+
kind: "index",
|
|
339
|
+
id: `unique-${tableName}-${name}`,
|
|
340
|
+
label,
|
|
341
|
+
meta: {
|
|
342
|
+
columns: unique.columns,
|
|
343
|
+
unique: true
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
for (const index of table.indexes) {
|
|
348
|
+
const name = index.name ?? defaultIndexName(tableName, index.columns);
|
|
349
|
+
const label = index.unique ? `unique index ${name}` : `index ${name}`;
|
|
350
|
+
children.push({
|
|
351
|
+
kind: "index",
|
|
352
|
+
id: `index-${tableName}-${name}`,
|
|
353
|
+
label,
|
|
354
|
+
meta: {
|
|
355
|
+
columns: index.columns,
|
|
356
|
+
unique: index.unique
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
const tableMeta = {};
|
|
361
|
+
if (table.primaryKey) {
|
|
362
|
+
tableMeta["primaryKey"] = table.primaryKey.columns;
|
|
363
|
+
if (table.primaryKey.name) tableMeta["primaryKeyName"] = table.primaryKey.name;
|
|
364
|
+
}
|
|
365
|
+
if (table.foreignKeys.length > 0) tableMeta["foreignKeys"] = table.foreignKeys.map((fk) => ({
|
|
366
|
+
columns: fk.columns,
|
|
367
|
+
referencedTable: fk.referencedTable,
|
|
368
|
+
referencedColumns: fk.referencedColumns,
|
|
369
|
+
...fk.name ? { name: fk.name } : {}
|
|
370
|
+
}));
|
|
371
|
+
return {
|
|
372
|
+
kind: "entity",
|
|
373
|
+
id: `table-${tableName}`,
|
|
374
|
+
label: `table ${tableName}`,
|
|
375
|
+
...Object.keys(tableMeta).length > 0 ? { meta: tableMeta } : {},
|
|
376
|
+
...children.length > 0 ? { children } : {}
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
const dependencyNodes = schema.dependencies.map((dep) => {
|
|
380
|
+
const shortName = dep.id.split(".").pop() ?? dep.id;
|
|
381
|
+
return {
|
|
382
|
+
kind: "dependency",
|
|
383
|
+
id: `dependency-${dep.id}`,
|
|
384
|
+
label: `${shortName} dependency is installed`
|
|
385
|
+
};
|
|
386
|
+
});
|
|
387
|
+
const rootChildren = [...tableNodes, ...dependencyNodes];
|
|
388
|
+
return { root: {
|
|
389
|
+
kind: "root",
|
|
390
|
+
id: "sql-schema",
|
|
391
|
+
label: rootLabel,
|
|
392
|
+
...rootChildren.length > 0 ? { children: rootChildren } : {}
|
|
393
|
+
} };
|
|
394
|
+
},
|
|
395
|
+
async emitContract({ contractIR }) {
|
|
396
|
+
const result = await emit(normalizeProviderContractIR(contractIR), {
|
|
397
|
+
outputDir: "",
|
|
398
|
+
operationRegistry,
|
|
399
|
+
codecTypeImports,
|
|
400
|
+
operationTypeImports,
|
|
401
|
+
queryOperationTypeImports,
|
|
402
|
+
extensionIds,
|
|
403
|
+
parameterizedRenderers,
|
|
404
|
+
parameterizedTypeImports
|
|
405
|
+
}, sqlTargetFamilyHook);
|
|
406
|
+
return {
|
|
407
|
+
contractJson: result.contractJson,
|
|
408
|
+
contractDts: result.contractDts,
|
|
409
|
+
storageHash: result.storageHash,
|
|
410
|
+
...result.executionHash ? { executionHash: result.executionHash } : {},
|
|
411
|
+
profileHash: result.profileHash
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/core/control-descriptor.ts
|
|
419
|
+
var SqlFamilyDescriptor = class {
|
|
420
|
+
kind = "family";
|
|
421
|
+
id = "sql";
|
|
422
|
+
familyId = "sql";
|
|
423
|
+
version = "0.0.1";
|
|
424
|
+
hook = sqlTargetFamilyHook;
|
|
425
|
+
create(stack) {
|
|
426
|
+
const target = stack.target;
|
|
427
|
+
const adapter = stack.adapter;
|
|
428
|
+
const extensionPacks = stack.extensionPacks;
|
|
429
|
+
return createSqlFamilyInstance({
|
|
430
|
+
target,
|
|
431
|
+
adapter,
|
|
432
|
+
extensionPacks
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region src/core/migrations/contract-to-schema-ir.ts
|
|
439
|
+
function convertColumn(name, column, expandNativeType, renderDefault) {
|
|
440
|
+
return {
|
|
441
|
+
name,
|
|
442
|
+
nativeType: expandNativeType ? expandNativeType({
|
|
443
|
+
nativeType: column.nativeType,
|
|
444
|
+
codecId: column.codecId,
|
|
445
|
+
...ifDefined("typeParams", column.typeParams)
|
|
446
|
+
}) : column.nativeType,
|
|
447
|
+
nullable: column.nullable,
|
|
448
|
+
...ifDefined("default", column.default != null && renderDefault ? renderDefault(column.default, column) : void 0)
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
function convertUnique(unique) {
|
|
452
|
+
return {
|
|
453
|
+
columns: unique.columns,
|
|
454
|
+
...ifDefined("name", unique.name)
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function convertIndex(index) {
|
|
458
|
+
return {
|
|
459
|
+
columns: index.columns,
|
|
460
|
+
unique: false,
|
|
461
|
+
...ifDefined("name", index.name)
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function convertForeignKey(fk) {
|
|
465
|
+
return {
|
|
466
|
+
columns: fk.columns,
|
|
467
|
+
referencedTable: fk.references.table,
|
|
468
|
+
referencedColumns: fk.references.columns,
|
|
469
|
+
...ifDefined("name", fk.name)
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
function convertTable(name, table, expandNativeType, renderDefault) {
|
|
473
|
+
const columns = {};
|
|
474
|
+
for (const [colName, colDef] of Object.entries(table.columns)) columns[colName] = convertColumn(colName, colDef, expandNativeType, renderDefault);
|
|
475
|
+
const satisfiedIndexColumns = new Set([
|
|
476
|
+
...table.indexes.map((idx) => idx.columns.join(",")),
|
|
477
|
+
...table.uniques.map((unique) => unique.columns.join(",")),
|
|
478
|
+
...table.primaryKey ? [table.primaryKey.columns.join(",")] : []
|
|
479
|
+
]);
|
|
480
|
+
const fkBackingIndexes = [];
|
|
481
|
+
for (const fk of table.foreignKeys) {
|
|
482
|
+
if (fk.index === false) continue;
|
|
483
|
+
const key = fk.columns.join(",");
|
|
484
|
+
if (satisfiedIndexColumns.has(key)) continue;
|
|
485
|
+
fkBackingIndexes.push({
|
|
486
|
+
columns: fk.columns,
|
|
487
|
+
unique: false,
|
|
488
|
+
name: defaultIndexName(name, fk.columns)
|
|
489
|
+
});
|
|
490
|
+
satisfiedIndexColumns.add(key);
|
|
491
|
+
}
|
|
492
|
+
return {
|
|
493
|
+
name,
|
|
494
|
+
columns,
|
|
495
|
+
...ifDefined("primaryKey", table.primaryKey),
|
|
496
|
+
foreignKeys: table.foreignKeys.map(convertForeignKey),
|
|
497
|
+
uniques: table.uniques.map(convertUnique),
|
|
498
|
+
indexes: [...table.indexes.map(convertIndex), ...fkBackingIndexes]
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Detects destructive changes between two contract storages.
|
|
503
|
+
*
|
|
504
|
+
* The additive-only planner silently ignores removals (tables, columns).
|
|
505
|
+
* This function detects those removals so callers can report them as conflicts
|
|
506
|
+
* rather than silently producing an empty plan.
|
|
507
|
+
*
|
|
508
|
+
* Returns an empty array if no destructive changes are found.
|
|
509
|
+
*/
|
|
510
|
+
function detectDestructiveChanges(from, to) {
|
|
511
|
+
if (!from) return [];
|
|
512
|
+
const hasOwn = (value, key) => Object.hasOwn(value, key);
|
|
513
|
+
const conflicts = [];
|
|
514
|
+
for (const tableName of Object.keys(from.tables)) {
|
|
515
|
+
if (!hasOwn(to.tables, tableName)) {
|
|
516
|
+
conflicts.push({
|
|
517
|
+
kind: "tableRemoved",
|
|
518
|
+
summary: `Table "${tableName}" was removed`
|
|
519
|
+
});
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const toTable = to.tables[tableName];
|
|
523
|
+
const fromTable = from.tables[tableName];
|
|
524
|
+
if (!fromTable) continue;
|
|
525
|
+
for (const columnName of Object.keys(fromTable.columns)) if (!hasOwn(toTable.columns, columnName)) conflicts.push({
|
|
526
|
+
kind: "columnRemoved",
|
|
527
|
+
summary: `Column "${tableName}"."${columnName}" was removed`
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
return conflicts;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Converts an `SqlContract` to `SqlSchemaIR`.
|
|
534
|
+
*
|
|
535
|
+
* Reads `contract.storage` for tables, `contract.storage.types` for type
|
|
536
|
+
* annotations, and derives database dependencies from `frameworkComponents`
|
|
537
|
+
* (each component's `databaseDependencies.init[].id`).
|
|
538
|
+
* Storage-type annotations are written under `options.annotationNamespace`.
|
|
539
|
+
*
|
|
540
|
+
* Drops codec metadata (`codecId`, `typeRef`) since the schema IR only represents
|
|
541
|
+
* structural information. When `expandNativeType` is provided, parameterized types
|
|
542
|
+
* are expanded (e.g. `character` + `{ length: 36 }` → `character(36)`) so the
|
|
543
|
+
* resulting IR compares correctly against the "to" contract during planning.
|
|
544
|
+
*
|
|
545
|
+
* Returns an empty schema IR when `contract` is `null` (new project).
|
|
546
|
+
*/
|
|
547
|
+
function contractToSchemaIR(contract, options) {
|
|
548
|
+
if (options.annotationNamespace.length === 0) throw new Error("annotationNamespace must be a non-empty string");
|
|
549
|
+
if (!contract) return {
|
|
550
|
+
tables: {},
|
|
551
|
+
dependencies: []
|
|
552
|
+
};
|
|
553
|
+
const storage = contract.storage;
|
|
554
|
+
const tables = {};
|
|
555
|
+
for (const [tableName, tableDef] of Object.entries(storage.tables)) tables[tableName] = convertTable(tableName, tableDef, options.expandNativeType, options.renderDefault);
|
|
556
|
+
return {
|
|
557
|
+
tables,
|
|
558
|
+
dependencies: deduplicateDependencyIRs(collectInitDependencies(options.frameworkComponents ?? [])),
|
|
559
|
+
...ifDefined("annotations", deriveAnnotations(storage, options.annotationNamespace))
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
function deduplicateDependencyIRs(deps) {
|
|
563
|
+
const seen = /* @__PURE__ */ new Set();
|
|
564
|
+
const result = [];
|
|
565
|
+
for (const dep of deps) {
|
|
566
|
+
if (dep.id.trim().length === 0) throw new Error("Dependency id must be a non-empty string");
|
|
567
|
+
if (seen.has(dep.id)) continue;
|
|
568
|
+
seen.add(dep.id);
|
|
569
|
+
result.push({ id: dep.id });
|
|
570
|
+
}
|
|
571
|
+
return result;
|
|
572
|
+
}
|
|
573
|
+
function deriveAnnotations(storage, annotationNamespace) {
|
|
574
|
+
if (!storage.types || Object.keys(storage.types).length === 0) return void 0;
|
|
575
|
+
return { [annotationNamespace]: { storageTypes: storage.types } };
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
//#endregion
|
|
579
|
+
//#region src/core/migrations/plan-helpers.ts
|
|
580
|
+
const readOnlyEmptyObject = Object.freeze({});
|
|
581
|
+
function cloneRecord(value) {
|
|
582
|
+
if (value === readOnlyEmptyObject) return value;
|
|
583
|
+
return Object.freeze({ ...value });
|
|
584
|
+
}
|
|
585
|
+
function freezeSteps(steps) {
|
|
586
|
+
if (steps.length === 0) return Object.freeze([]);
|
|
587
|
+
return Object.freeze(steps.map((step) => Object.freeze({
|
|
588
|
+
description: step.description,
|
|
589
|
+
sql: step.sql,
|
|
590
|
+
...step.meta ? { meta: cloneRecord(step.meta) } : {}
|
|
591
|
+
})));
|
|
592
|
+
}
|
|
593
|
+
function freezeDetailsValue(value) {
|
|
594
|
+
if (value === null || value === void 0) return value;
|
|
595
|
+
if (typeof value !== "object") return value;
|
|
596
|
+
if (Array.isArray(value)) return Object.freeze([...value]);
|
|
597
|
+
return Object.freeze({ ...value });
|
|
598
|
+
}
|
|
599
|
+
function freezeTargetDetails(target) {
|
|
600
|
+
return Object.freeze({
|
|
601
|
+
id: target.id,
|
|
602
|
+
...target.details !== void 0 ? { details: freezeDetailsValue(target.details) } : {}
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
function freezeOperation(operation) {
|
|
606
|
+
return Object.freeze({
|
|
607
|
+
id: operation.id,
|
|
608
|
+
label: operation.label,
|
|
609
|
+
...operation.summary ? { summary: operation.summary } : {},
|
|
610
|
+
operationClass: operation.operationClass,
|
|
611
|
+
target: freezeTargetDetails(operation.target),
|
|
612
|
+
precheck: freezeSteps(operation.precheck),
|
|
613
|
+
execute: freezeSteps(operation.execute),
|
|
614
|
+
postcheck: freezeSteps(operation.postcheck),
|
|
615
|
+
...operation.meta ? { meta: cloneRecord(operation.meta) } : {}
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
function freezeOperations(operations) {
|
|
619
|
+
if (operations.length === 0) return Object.freeze([]);
|
|
620
|
+
return Object.freeze(operations.map((operation) => freezeOperation(operation)));
|
|
621
|
+
}
|
|
622
|
+
function createMigrationPlan(options) {
|
|
623
|
+
return Object.freeze({
|
|
624
|
+
targetId: options.targetId,
|
|
625
|
+
...options.origin !== void 0 ? { origin: options.origin ? Object.freeze({ ...options.origin }) : null } : {},
|
|
626
|
+
destination: Object.freeze({ ...options.destination }),
|
|
627
|
+
operations: freezeOperations(options.operations),
|
|
628
|
+
...options.meta ? { meta: cloneRecord(options.meta) } : {}
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
function plannerSuccess(plan) {
|
|
632
|
+
return Object.freeze({
|
|
633
|
+
kind: "success",
|
|
634
|
+
plan
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
function plannerFailure(conflicts) {
|
|
638
|
+
return Object.freeze({
|
|
639
|
+
kind: "failure",
|
|
640
|
+
conflicts: Object.freeze(conflicts.map((conflict) => Object.freeze({
|
|
641
|
+
kind: conflict.kind,
|
|
642
|
+
summary: conflict.summary,
|
|
643
|
+
...conflict.why ? { why: conflict.why } : {},
|
|
644
|
+
...conflict.location ? { location: Object.freeze({ ...conflict.location }) } : {},
|
|
645
|
+
...conflict.meta ? { meta: cloneRecord(conflict.meta) } : {}
|
|
646
|
+
})))
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Creates a successful migration runner result.
|
|
651
|
+
*/
|
|
652
|
+
function runnerSuccess(value) {
|
|
653
|
+
return ok(Object.freeze({
|
|
654
|
+
operationsPlanned: value.operationsPlanned,
|
|
655
|
+
operationsExecuted: value.operationsExecuted
|
|
656
|
+
}));
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Creates a failed migration runner result.
|
|
660
|
+
*/
|
|
661
|
+
function runnerFailure(code, summary, options) {
|
|
662
|
+
return notOk(Object.freeze({
|
|
663
|
+
code,
|
|
664
|
+
summary,
|
|
665
|
+
...options?.why ? { why: options.why } : {},
|
|
666
|
+
...options?.meta ? { meta: cloneRecord(options.meta) } : {}
|
|
667
|
+
}));
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
//#endregion
|
|
671
|
+
//#region src/core/migrations/policies.ts
|
|
672
|
+
/**
|
|
673
|
+
* Policy used by `db init`: additive-only operations, no widening/destructive steps.
|
|
674
|
+
*/
|
|
675
|
+
const INIT_ADDITIVE_POLICY = Object.freeze({ allowedOperationClasses: Object.freeze(["additive"]) });
|
|
676
|
+
|
|
677
|
+
//#endregion
|
|
678
|
+
//#region src/exports/control.ts
|
|
679
|
+
var control_default = new SqlFamilyDescriptor();
|
|
680
|
+
|
|
681
|
+
//#endregion
|
|
682
|
+
export { INIT_ADDITIVE_POLICY, assembleControlMutationDefaultContributions, assemblePslInterpretationContributions, collectInitDependencies, contractToSchemaIR, createMigrationPlan, control_default as default, detectDestructiveChanges, extractCodecControlHooks, isDatabaseDependencyProvider, plannerFailure, plannerSuccess, runnerFailure, runnerSuccess };
|
|
683
|
+
//# sourceMappingURL=control.mjs.map
|