@prisma-next/target-sqlite 0.5.0-dev.6 → 0.5.0-dev.60
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/dist/codec-ids-B1-OiN8Q.mjs +14 -0
- package/dist/codec-ids-B1-OiN8Q.mjs.map +1 -0
- package/dist/codec-ids.d.mts +13 -0
- package/dist/codec-ids.d.mts.map +1 -0
- package/dist/codec-ids.mjs +3 -0
- package/dist/codec-types.d.mts +7 -0
- package/dist/codec-types.d.mts.map +1 -0
- package/dist/codec-types.mjs +3 -0
- package/dist/codecs-5GJysiEg.mjs +95 -0
- package/dist/codecs-5GJysiEg.mjs.map +1 -0
- package/dist/codecs-D4jgBM6T.d.mts +139 -0
- package/dist/codecs-D4jgBM6T.d.mts.map +1 -0
- package/dist/codecs.d.mts +2 -0
- package/dist/codecs.mjs +3 -0
- package/dist/control.d.mts +4 -3
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +343 -1
- package/dist/control.mjs.map +1 -1
- package/dist/default-normalizer-R-sQXAYt.mjs +69 -0
- package/dist/default-normalizer-R-sQXAYt.mjs.map +1 -0
- package/dist/default-normalizer.d.mts +8 -0
- package/dist/default-normalizer.d.mts.map +1 -0
- package/dist/default-normalizer.mjs +3 -0
- package/dist/descriptor-meta-BA2YAFQq.mjs +24 -0
- package/dist/descriptor-meta-BA2YAFQq.mjs.map +1 -0
- package/dist/migration.d.mts +76 -0
- package/dist/migration.d.mts.map +1 -0
- package/dist/migration.mjs +38 -0
- package/dist/migration.mjs.map +1 -0
- package/dist/native-type-normalizer-BMovohPm.mjs +14 -0
- package/dist/native-type-normalizer-BMovohPm.mjs.map +1 -0
- package/dist/native-type-normalizer.d.mts +11 -0
- package/dist/native-type-normalizer.d.mts.map +1 -0
- package/dist/native-type-normalizer.mjs +3 -0
- package/dist/op-factory-call-BPPSCdTB.d.mts +134 -0
- package/dist/op-factory-call-BPPSCdTB.d.mts.map +1 -0
- package/dist/op-factory-call-BUVV-W9F.mjs +252 -0
- package/dist/op-factory-call-BUVV-W9F.mjs.map +1 -0
- package/dist/op-factory-call.d.mts +3 -0
- package/dist/op-factory-call.mjs +3 -0
- package/dist/pack.d.mts +35 -1
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/planner-CuchCrpN.mjs +522 -0
- package/dist/planner-CuchCrpN.mjs.map +1 -0
- package/dist/planner-produced-sqlite-migration-C3AAaQoW.mjs +107 -0
- package/dist/planner-produced-sqlite-migration-C3AAaQoW.mjs.map +1 -0
- package/dist/planner-produced-sqlite-migration-RVneETNy.d.mts +24 -0
- package/dist/planner-produced-sqlite-migration-RVneETNy.d.mts.map +1 -0
- package/dist/planner-produced-sqlite-migration.d.mts +5 -0
- package/dist/planner-produced-sqlite-migration.mjs +3 -0
- package/dist/planner-target-details-BQIWQlBu.mjs +16 -0
- package/dist/planner-target-details-BQIWQlBu.mjs.map +1 -0
- package/dist/planner-target-details-DTIFFx4L.d.mts +12 -0
- package/dist/planner-target-details-DTIFFx4L.d.mts.map +1 -0
- package/dist/planner-target-details.d.mts +2 -0
- package/dist/planner-target-details.mjs +3 -0
- package/dist/planner.d.mts +56 -0
- package/dist/planner.d.mts.map +1 -0
- package/dist/planner.mjs +3 -0
- package/dist/render-ops-CXOv7SRC.mjs +8 -0
- package/dist/render-ops-CXOv7SRC.mjs.map +1 -0
- package/dist/render-ops.d.mts +11 -0
- package/dist/render-ops.d.mts.map +1 -0
- package/dist/render-ops.mjs +3 -0
- package/dist/runtime.mjs +1 -1
- package/dist/shared-BNtoZqdo.d.mts +69 -0
- package/dist/shared-BNtoZqdo.d.mts.map +1 -0
- package/dist/sql-utils-D3SMPFDD.mjs +33 -0
- package/dist/sql-utils-D3SMPFDD.mjs.map +1 -0
- package/dist/sql-utils.d.mts +22 -0
- package/dist/sql-utils.d.mts.map +1 -0
- package/dist/sql-utils.mjs +3 -0
- package/dist/sqlite-migration-BYgrMZdR.d.mts +18 -0
- package/dist/sqlite-migration-BYgrMZdR.d.mts.map +1 -0
- package/dist/sqlite-migration-CnLhIrJF.mjs +17 -0
- package/dist/sqlite-migration-CnLhIrJF.mjs.map +1 -0
- package/dist/statement-builders-B3OGOp7n.mjs +148 -0
- package/dist/statement-builders-B3OGOp7n.mjs.map +1 -0
- package/dist/statement-builders.d.mts +48 -0
- package/dist/statement-builders.d.mts.map +1 -0
- package/dist/statement-builders.mjs +3 -0
- package/dist/tables-sKIg_lWE.mjs +408 -0
- package/dist/tables-sKIg_lWE.mjs.map +1 -0
- package/package.json +29 -7
- package/src/core/authoring.ts +9 -0
- package/src/core/codec-ids.ts +13 -0
- package/src/core/codecs.ts +119 -0
- package/src/core/control-target.ts +54 -11
- package/src/core/default-normalizer.ts +92 -0
- package/src/core/descriptor-meta.ts +5 -1
- package/src/core/migrations/issue-planner.ts +586 -0
- package/src/core/migrations/op-factory-call.ts +332 -0
- package/src/core/migrations/operations/columns.ts +62 -0
- package/src/core/migrations/operations/data-transform.ts +51 -0
- package/src/core/migrations/operations/indexes.ts +52 -0
- package/src/core/migrations/operations/shared.ts +120 -0
- package/src/core/migrations/operations/tables.ts +388 -0
- package/src/core/migrations/planner-ddl-builders.ts +142 -0
- package/src/core/migrations/planner-produced-sqlite-migration.ts +56 -0
- package/src/core/migrations/planner-strategies.ts +231 -0
- package/src/core/migrations/planner-target-details.ts +33 -0
- package/src/core/migrations/planner.ts +149 -0
- package/src/core/migrations/render-ops.ts +9 -0
- package/src/core/migrations/render-typescript.ts +91 -0
- package/src/core/migrations/runner.ts +593 -0
- package/src/core/migrations/sqlite-migration.ts +13 -0
- package/src/core/migrations/statement-builders.ts +190 -0
- package/src/core/native-type-normalizer.ts +9 -0
- package/src/core/sql-utils.ts +47 -0
- package/src/exports/codec-ids.ts +13 -0
- package/src/exports/codec-types.ts +6 -0
- package/src/exports/codecs.ts +1 -0
- package/src/exports/control.ts +1 -0
- package/src/exports/default-normalizer.ts +1 -0
- package/src/exports/migration.ts +23 -0
- package/src/exports/native-type-normalizer.ts +1 -0
- package/src/exports/op-factory-call.ts +11 -0
- package/src/exports/planner-produced-sqlite-migration.ts +4 -0
- package/src/exports/planner-target-details.ts +2 -0
- package/src/exports/planner.ts +2 -0
- package/src/exports/render-ops.ts +1 -0
- package/src/exports/sql-utils.ts +1 -0
- package/src/exports/statement-builders.ts +11 -0
- package/dist/descriptor-meta-DbuuziYA.mjs +0 -14
- package/dist/descriptor-meta-DbuuziYA.mjs.map +0 -1
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import type { ContractMarkerRecord } from '@prisma-next/contract/types';
|
|
2
|
+
import type {
|
|
3
|
+
MigrationOperationPolicy,
|
|
4
|
+
SqlControlFamilyInstance,
|
|
5
|
+
SqlMigrationPlanContractInfo,
|
|
6
|
+
SqlMigrationPlanOperation,
|
|
7
|
+
SqlMigrationPlanOperationStep,
|
|
8
|
+
SqlMigrationRunner,
|
|
9
|
+
SqlMigrationRunnerExecuteOptions,
|
|
10
|
+
SqlMigrationRunnerFailure,
|
|
11
|
+
SqlMigrationRunnerResult,
|
|
12
|
+
} from '@prisma-next/family-sql/control';
|
|
13
|
+
import { runnerFailure, runnerSuccess } from '@prisma-next/family-sql/control';
|
|
14
|
+
import { verifySqlSchema } from '@prisma-next/family-sql/schema-verify';
|
|
15
|
+
import { type ContractMarkerRow, parseContractMarkerRow } from '@prisma-next/family-sql/verify';
|
|
16
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
17
|
+
import type { Result } from '@prisma-next/utils/result';
|
|
18
|
+
import { ok, okVoid } from '@prisma-next/utils/result';
|
|
19
|
+
import { parseSqliteDefault } from '../default-normalizer';
|
|
20
|
+
import { normalizeSqliteNativeType } from '../native-type-normalizer';
|
|
21
|
+
import type { SqlitePlanTargetDetails } from './planner-target-details';
|
|
22
|
+
import {
|
|
23
|
+
buildLedgerInsertStatement,
|
|
24
|
+
buildWriteMarkerStatements,
|
|
25
|
+
ensureLedgerTableStatement,
|
|
26
|
+
ensureMarkerTableStatement,
|
|
27
|
+
readMarkerStatement,
|
|
28
|
+
type SqlStatement,
|
|
29
|
+
} from './statement-builders';
|
|
30
|
+
|
|
31
|
+
export function createSqliteMigrationRunner(
|
|
32
|
+
family: SqlControlFamilyInstance,
|
|
33
|
+
): SqlMigrationRunner<SqlitePlanTargetDetails> {
|
|
34
|
+
return new SqliteMigrationRunner(family);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class SqliteMigrationRunner implements SqlMigrationRunner<SqlitePlanTargetDetails> {
|
|
38
|
+
constructor(private readonly family: SqlControlFamilyInstance) {}
|
|
39
|
+
|
|
40
|
+
async execute(
|
|
41
|
+
options: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>,
|
|
42
|
+
): Promise<SqlMigrationRunnerResult> {
|
|
43
|
+
const driver = options.driver;
|
|
44
|
+
|
|
45
|
+
const destinationCheck = this.ensurePlanMatchesDestinationContract(
|
|
46
|
+
options.plan.destination,
|
|
47
|
+
options.destinationContract,
|
|
48
|
+
);
|
|
49
|
+
if (!destinationCheck.ok) {
|
|
50
|
+
return destinationCheck;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const policyCheck = this.enforcePolicyCompatibility(options.policy, options.plan.operations);
|
|
54
|
+
if (!policyCheck.ok) {
|
|
55
|
+
return policyCheck;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// SQLite recreate-table drops and rebuilds the table. If foreign_keys is ON,
|
|
59
|
+
// dropping a referenced parent cascade-deletes child rows; we must disable FK
|
|
60
|
+
// enforcement for the duration of the migration and validate integrity before
|
|
61
|
+
// committing. PRAGMA foreign_keys is a no-op inside a transaction, so toggle
|
|
62
|
+
// around BEGIN/COMMIT.
|
|
63
|
+
const fkWasEnabled = await this.readForeignKeysEnabled(driver);
|
|
64
|
+
if (fkWasEnabled) {
|
|
65
|
+
await driver.query('PRAGMA foreign_keys = OFF');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
await this.beginExclusiveTransaction(driver);
|
|
70
|
+
let committed = false;
|
|
71
|
+
try {
|
|
72
|
+
await this.ensureControlTables(driver);
|
|
73
|
+
const existingMarker = await this.readMarker(driver);
|
|
74
|
+
|
|
75
|
+
const markerCheck = this.ensureMarkerCompatibility(existingMarker, options.plan);
|
|
76
|
+
if (!markerCheck.ok) {
|
|
77
|
+
return markerCheck;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const markerAtDestination = this.markerMatchesDestination(existingMarker, options.plan);
|
|
81
|
+
const isSelfEdge =
|
|
82
|
+
options.plan.origin?.storageHash === options.plan.destination.storageHash;
|
|
83
|
+
const skipOperations = markerAtDestination && options.plan.origin != null && !isSelfEdge;
|
|
84
|
+
|
|
85
|
+
let operationsExecuted: number;
|
|
86
|
+
let executedOperations: readonly SqlMigrationPlanOperation<SqlitePlanTargetDetails>[];
|
|
87
|
+
|
|
88
|
+
if (skipOperations) {
|
|
89
|
+
operationsExecuted = 0;
|
|
90
|
+
executedOperations = [];
|
|
91
|
+
} else {
|
|
92
|
+
const applyResult = await this.applyPlan(driver, options);
|
|
93
|
+
if (!applyResult.ok) {
|
|
94
|
+
return applyResult;
|
|
95
|
+
}
|
|
96
|
+
operationsExecuted = applyResult.value.operationsExecuted;
|
|
97
|
+
executedOperations = applyResult.value.executedOperations;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const schemaIR = await this.family.introspect({
|
|
101
|
+
driver,
|
|
102
|
+
contract: options.destinationContract,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const schemaVerifyResult = verifySqlSchema({
|
|
106
|
+
contract: options.destinationContract,
|
|
107
|
+
schema: schemaIR,
|
|
108
|
+
strict: options.strictVerification ?? true,
|
|
109
|
+
context: options.context ?? {},
|
|
110
|
+
typeMetadataRegistry: this.family.typeMetadataRegistry,
|
|
111
|
+
frameworkComponents: options.frameworkComponents,
|
|
112
|
+
normalizeDefault: parseSqliteDefault,
|
|
113
|
+
normalizeNativeType: normalizeSqliteNativeType,
|
|
114
|
+
});
|
|
115
|
+
if (!schemaVerifyResult.ok) {
|
|
116
|
+
return runnerFailure('SCHEMA_VERIFY_FAILED', schemaVerifyResult.summary, {
|
|
117
|
+
why: 'The resulting database schema does not satisfy the destination contract.',
|
|
118
|
+
meta: {
|
|
119
|
+
issues: schemaVerifyResult.schema.issues,
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Self-edge no-op detection: a self-edge migration whose ops had no
|
|
125
|
+
// ops to begin with and brings no new invariants produced no
|
|
126
|
+
// observable change. Skip the marker + ledger writes so an idempotent
|
|
127
|
+
// re-apply of a self-edge data transform doesn't churn updatedAt or
|
|
128
|
+
// pile up empty ledger entries. db update no-ops still write a
|
|
129
|
+
// ledger entry as audit trail.
|
|
130
|
+
const incomingInvariants = options.plan.providedInvariants;
|
|
131
|
+
const existingInvariants = new Set(existingMarker?.invariants ?? []);
|
|
132
|
+
const incomingIsSubsetOfExisting = incomingInvariants.every((id) =>
|
|
133
|
+
existingInvariants.has(id),
|
|
134
|
+
);
|
|
135
|
+
const isSelfEdgeNoOp = isSelfEdge && operationsExecuted === 0 && incomingIsSubsetOfExisting;
|
|
136
|
+
|
|
137
|
+
if (!isSelfEdgeNoOp) {
|
|
138
|
+
await this.upsertMarker(driver, options, existingMarker);
|
|
139
|
+
await this.recordLedgerEntry(driver, options, existingMarker, executedOperations);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (fkWasEnabled) {
|
|
143
|
+
const fkIntegrityCheck = await this.verifyForeignKeyIntegrity(driver);
|
|
144
|
+
if (!fkIntegrityCheck.ok) {
|
|
145
|
+
return fkIntegrityCheck;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
await this.commitTransaction(driver);
|
|
150
|
+
committed = true;
|
|
151
|
+
return runnerSuccess({
|
|
152
|
+
operationsPlanned: options.plan.operations.length,
|
|
153
|
+
operationsExecuted,
|
|
154
|
+
});
|
|
155
|
+
} finally {
|
|
156
|
+
if (!committed) {
|
|
157
|
+
await this.rollbackTransaction(driver);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
} finally {
|
|
161
|
+
if (fkWasEnabled) {
|
|
162
|
+
await driver.query('PRAGMA foreign_keys = ON');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private async readForeignKeysEnabled(
|
|
168
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
169
|
+
): Promise<boolean> {
|
|
170
|
+
const result = await driver.query<{ foreign_keys: number }>('PRAGMA foreign_keys');
|
|
171
|
+
const row = result.rows[0];
|
|
172
|
+
return row?.foreign_keys === 1;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private async verifyForeignKeyIntegrity(
|
|
176
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
177
|
+
): Promise<Result<void, SqlMigrationRunnerFailure>> {
|
|
178
|
+
const result = await driver.query<Record<string, unknown>>('PRAGMA foreign_key_check');
|
|
179
|
+
if (result.rows.length === 0) {
|
|
180
|
+
return okVoid();
|
|
181
|
+
}
|
|
182
|
+
return runnerFailure(
|
|
183
|
+
'FOREIGN_KEY_VIOLATION',
|
|
184
|
+
`Foreign key integrity check failed after migration: ${result.rows.length} violation(s).`,
|
|
185
|
+
{
|
|
186
|
+
why: 'PRAGMA foreign_key_check reported violations after applying recreate-table operations.',
|
|
187
|
+
meta: { violations: result.rows },
|
|
188
|
+
},
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async applyPlan(
|
|
193
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
194
|
+
options: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>,
|
|
195
|
+
): Promise<
|
|
196
|
+
Result<
|
|
197
|
+
{
|
|
198
|
+
readonly operationsExecuted: number;
|
|
199
|
+
readonly executedOperations: readonly SqlMigrationPlanOperation<SqlitePlanTargetDetails>[];
|
|
200
|
+
},
|
|
201
|
+
SqlMigrationRunnerFailure
|
|
202
|
+
>
|
|
203
|
+
> {
|
|
204
|
+
const checks = options.executionChecks;
|
|
205
|
+
const runPrechecks = checks?.prechecks !== false;
|
|
206
|
+
const runPostchecks = checks?.postchecks !== false;
|
|
207
|
+
const runIdempotency = checks?.idempotencyChecks !== false;
|
|
208
|
+
|
|
209
|
+
let operationsExecuted = 0;
|
|
210
|
+
const executedOperations: Array<SqlMigrationPlanOperation<SqlitePlanTargetDetails>> = [];
|
|
211
|
+
|
|
212
|
+
for (const operation of options.plan.operations) {
|
|
213
|
+
options.callbacks?.onOperationStart?.(operation);
|
|
214
|
+
try {
|
|
215
|
+
if (runPostchecks && runIdempotency) {
|
|
216
|
+
const postcheckAlreadySatisfied = await this.expectationsAreSatisfied(
|
|
217
|
+
driver,
|
|
218
|
+
operation.postcheck,
|
|
219
|
+
);
|
|
220
|
+
if (postcheckAlreadySatisfied) {
|
|
221
|
+
executedOperations.push(this.createSkipRecord(operation));
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (runPrechecks) {
|
|
227
|
+
const precheckResult = await this.runExpectationSteps(
|
|
228
|
+
driver,
|
|
229
|
+
operation.precheck,
|
|
230
|
+
operation,
|
|
231
|
+
'precheck',
|
|
232
|
+
);
|
|
233
|
+
if (!precheckResult.ok) {
|
|
234
|
+
return precheckResult;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const executeResult = await this.runExecuteSteps(driver, operation.execute, operation);
|
|
239
|
+
if (!executeResult.ok) {
|
|
240
|
+
return executeResult;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (runPostchecks) {
|
|
244
|
+
const postcheckResult = await this.runExpectationSteps(
|
|
245
|
+
driver,
|
|
246
|
+
operation.postcheck,
|
|
247
|
+
operation,
|
|
248
|
+
'postcheck',
|
|
249
|
+
);
|
|
250
|
+
if (!postcheckResult.ok) {
|
|
251
|
+
return postcheckResult;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
executedOperations.push(operation);
|
|
256
|
+
operationsExecuted += 1;
|
|
257
|
+
} finally {
|
|
258
|
+
options.callbacks?.onOperationComplete?.(operation);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return ok({ operationsExecuted, executedOperations });
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private async ensureControlTables(
|
|
265
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
266
|
+
): Promise<void> {
|
|
267
|
+
await this.executeStatement(driver, ensureMarkerTableStatement);
|
|
268
|
+
await this.executeStatement(driver, ensureLedgerTableStatement);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private async readMarker(
|
|
272
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
273
|
+
): Promise<ContractMarkerRecord | null> {
|
|
274
|
+
const stmt = readMarkerStatement();
|
|
275
|
+
try {
|
|
276
|
+
const result = await driver.query<ContractMarkerRow>(stmt.sql, stmt.params);
|
|
277
|
+
const row = result.rows[0];
|
|
278
|
+
if (!row) return null;
|
|
279
|
+
// SQLite stores arrays as JSON-encoded TEXT (no native array type), so
|
|
280
|
+
// the driver returns `invariants` as a string. Decode before delegating
|
|
281
|
+
// to the shared row schema, which expects `string[]`.
|
|
282
|
+
const invariants =
|
|
283
|
+
typeof row.invariants === 'string'
|
|
284
|
+
? (JSON.parse(row.invariants) as unknown)
|
|
285
|
+
: row.invariants;
|
|
286
|
+
return parseContractMarkerRow({ ...row, invariants });
|
|
287
|
+
} catch (error) {
|
|
288
|
+
// Table might not exist yet
|
|
289
|
+
if (error instanceof Error && error.message.includes('no such table')) {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private async runExpectationSteps(
|
|
297
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
298
|
+
steps: readonly SqlMigrationPlanOperationStep[],
|
|
299
|
+
operation: SqlMigrationPlanOperation<SqlitePlanTargetDetails>,
|
|
300
|
+
phase: 'precheck' | 'postcheck',
|
|
301
|
+
): Promise<Result<void, SqlMigrationRunnerFailure>> {
|
|
302
|
+
for (const step of steps) {
|
|
303
|
+
const result = await driver.query(step.sql, step.params ?? []);
|
|
304
|
+
if (!this.stepResultIsTrue(result.rows)) {
|
|
305
|
+
const code = phase === 'precheck' ? 'PRECHECK_FAILED' : 'POSTCHECK_FAILED';
|
|
306
|
+
return runnerFailure(
|
|
307
|
+
code,
|
|
308
|
+
`Operation ${operation.id} failed during ${phase}: ${step.description}`,
|
|
309
|
+
{
|
|
310
|
+
meta: {
|
|
311
|
+
operationId: operation.id,
|
|
312
|
+
phase,
|
|
313
|
+
stepDescription: step.description,
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return okVoid();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private async runExecuteSteps(
|
|
323
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
324
|
+
steps: readonly SqlMigrationPlanOperationStep[],
|
|
325
|
+
operation: SqlMigrationPlanOperation<SqlitePlanTargetDetails>,
|
|
326
|
+
): Promise<Result<void, SqlMigrationRunnerFailure>> {
|
|
327
|
+
for (const step of steps) {
|
|
328
|
+
try {
|
|
329
|
+
await driver.query(step.sql, step.params ?? []);
|
|
330
|
+
} catch (error: unknown) {
|
|
331
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
332
|
+
return runnerFailure(
|
|
333
|
+
'EXECUTION_FAILED',
|
|
334
|
+
`Operation ${operation.id} failed during execution: ${step.description}`,
|
|
335
|
+
{
|
|
336
|
+
why: message,
|
|
337
|
+
meta: {
|
|
338
|
+
operationId: operation.id,
|
|
339
|
+
stepDescription: step.description,
|
|
340
|
+
sql: step.sql,
|
|
341
|
+
},
|
|
342
|
+
},
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return okVoid();
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private stepResultIsTrue(rows: readonly Record<string, unknown>[]): boolean {
|
|
350
|
+
if (!rows || rows.length === 0) {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
const firstRow = rows[0];
|
|
354
|
+
const firstValue = firstRow ? Object.values(firstRow)[0] : undefined;
|
|
355
|
+
if (typeof firstValue === 'number') {
|
|
356
|
+
return firstValue !== 0;
|
|
357
|
+
}
|
|
358
|
+
if (typeof firstValue === 'boolean') {
|
|
359
|
+
return firstValue;
|
|
360
|
+
}
|
|
361
|
+
if (typeof firstValue === 'string') {
|
|
362
|
+
const lower = firstValue.toLowerCase();
|
|
363
|
+
if (lower === 'true' || lower === '1') return true;
|
|
364
|
+
if (lower === 'false' || lower === '0') return false;
|
|
365
|
+
return firstValue.length > 0;
|
|
366
|
+
}
|
|
367
|
+
return Boolean(firstValue);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private async expectationsAreSatisfied(
|
|
371
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
372
|
+
steps: readonly SqlMigrationPlanOperationStep[],
|
|
373
|
+
): Promise<boolean> {
|
|
374
|
+
if (steps.length === 0) {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
for (const step of steps) {
|
|
378
|
+
const result = await driver.query(step.sql, step.params ?? []);
|
|
379
|
+
if (!this.stepResultIsTrue(result.rows)) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private createSkipRecord(
|
|
387
|
+
operation: SqlMigrationPlanOperation<SqlitePlanTargetDetails>,
|
|
388
|
+
): SqlMigrationPlanOperation<SqlitePlanTargetDetails> {
|
|
389
|
+
return Object.freeze({
|
|
390
|
+
id: operation.id,
|
|
391
|
+
label: operation.label,
|
|
392
|
+
...ifDefined('summary', operation.summary),
|
|
393
|
+
operationClass: operation.operationClass,
|
|
394
|
+
target: operation.target,
|
|
395
|
+
precheck: Object.freeze([]),
|
|
396
|
+
execute: Object.freeze([]),
|
|
397
|
+
postcheck: Object.freeze([...operation.postcheck]),
|
|
398
|
+
meta: Object.freeze({
|
|
399
|
+
...(operation.meta ?? {}),
|
|
400
|
+
runner: Object.freeze({ skipped: true, reason: 'postcheck_pre_satisfied' }),
|
|
401
|
+
}),
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
private markerMatchesDestination(
|
|
406
|
+
marker: ContractMarkerRecord | null,
|
|
407
|
+
plan: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['plan'],
|
|
408
|
+
): boolean {
|
|
409
|
+
if (!marker) return false;
|
|
410
|
+
if (marker.storageHash !== plan.destination.storageHash) return false;
|
|
411
|
+
if (plan.destination.profileHash && marker.profileHash !== plan.destination.profileHash) {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
private enforcePolicyCompatibility(
|
|
418
|
+
policy: MigrationOperationPolicy,
|
|
419
|
+
operations: readonly SqlMigrationPlanOperation<SqlitePlanTargetDetails>[],
|
|
420
|
+
): Result<void, SqlMigrationRunnerFailure> {
|
|
421
|
+
const allowedClasses = new Set(policy.allowedOperationClasses);
|
|
422
|
+
for (const operation of operations) {
|
|
423
|
+
if (!allowedClasses.has(operation.operationClass)) {
|
|
424
|
+
return runnerFailure(
|
|
425
|
+
'POLICY_VIOLATION',
|
|
426
|
+
`Operation ${operation.id} has class "${operation.operationClass}" which is not allowed by policy.`,
|
|
427
|
+
{
|
|
428
|
+
why: `Policy only allows: ${policy.allowedOperationClasses.join(', ')}.`,
|
|
429
|
+
meta: {
|
|
430
|
+
operationId: operation.id,
|
|
431
|
+
operationClass: operation.operationClass,
|
|
432
|
+
allowedClasses: policy.allowedOperationClasses,
|
|
433
|
+
},
|
|
434
|
+
},
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
return okVoid();
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private ensureMarkerCompatibility(
|
|
442
|
+
marker: ContractMarkerRecord | null,
|
|
443
|
+
plan: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['plan'],
|
|
444
|
+
): Result<void, SqlMigrationRunnerFailure> {
|
|
445
|
+
const origin = plan.origin ?? null;
|
|
446
|
+
if (!origin) {
|
|
447
|
+
return okVoid();
|
|
448
|
+
}
|
|
449
|
+
if (!marker) {
|
|
450
|
+
return runnerFailure(
|
|
451
|
+
'MARKER_ORIGIN_MISMATCH',
|
|
452
|
+
`Missing contract marker: expected origin storage hash ${origin.storageHash}.`,
|
|
453
|
+
{ meta: { expectedOriginStorageHash: origin.storageHash } },
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
if (marker.storageHash !== origin.storageHash) {
|
|
457
|
+
return runnerFailure(
|
|
458
|
+
'MARKER_ORIGIN_MISMATCH',
|
|
459
|
+
`Existing contract marker (${marker.storageHash}) does not match plan origin (${origin.storageHash}).`,
|
|
460
|
+
{
|
|
461
|
+
meta: {
|
|
462
|
+
markerStorageHash: marker.storageHash,
|
|
463
|
+
expectedOriginStorageHash: origin.storageHash,
|
|
464
|
+
},
|
|
465
|
+
},
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
if (origin.profileHash && marker.profileHash !== origin.profileHash) {
|
|
469
|
+
return runnerFailure(
|
|
470
|
+
'MARKER_ORIGIN_MISMATCH',
|
|
471
|
+
`Existing contract marker profile hash (${marker.profileHash}) does not match plan origin profile hash (${origin.profileHash}).`,
|
|
472
|
+
{
|
|
473
|
+
meta: {
|
|
474
|
+
markerProfileHash: marker.profileHash,
|
|
475
|
+
expectedOriginProfileHash: origin.profileHash,
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
return okVoid();
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
private ensurePlanMatchesDestinationContract(
|
|
484
|
+
destination: SqlMigrationPlanContractInfo,
|
|
485
|
+
contract: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['destinationContract'],
|
|
486
|
+
): Result<void, SqlMigrationRunnerFailure> {
|
|
487
|
+
if (destination.storageHash !== contract.storage.storageHash) {
|
|
488
|
+
return runnerFailure(
|
|
489
|
+
'DESTINATION_CONTRACT_MISMATCH',
|
|
490
|
+
`Plan destination storage hash (${destination.storageHash}) does not match provided contract storage hash (${contract.storage.storageHash}).`,
|
|
491
|
+
{
|
|
492
|
+
meta: {
|
|
493
|
+
planStorageHash: destination.storageHash,
|
|
494
|
+
contractStorageHash: contract.storage.storageHash,
|
|
495
|
+
},
|
|
496
|
+
},
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
if (
|
|
500
|
+
destination.profileHash &&
|
|
501
|
+
contract.profileHash &&
|
|
502
|
+
destination.profileHash !== contract.profileHash
|
|
503
|
+
) {
|
|
504
|
+
return runnerFailure(
|
|
505
|
+
'DESTINATION_CONTRACT_MISMATCH',
|
|
506
|
+
`Plan destination profile hash (${destination.profileHash}) does not match provided contract profile hash (${contract.profileHash}).`,
|
|
507
|
+
{
|
|
508
|
+
meta: {
|
|
509
|
+
planProfileHash: destination.profileHash,
|
|
510
|
+
contractProfileHash: contract.profileHash,
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
return okVoid();
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
private async upsertMarker(
|
|
519
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
520
|
+
options: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>,
|
|
521
|
+
existingMarker: ContractMarkerRecord | null,
|
|
522
|
+
): Promise<void> {
|
|
523
|
+
// SQLite has no native array type, so we can't merge invariants in SQL
|
|
524
|
+
// the way Postgres does. Merge client-side under the runner's
|
|
525
|
+
// BEGIN EXCLUSIVE — sort + dedupe so the JSON-encoded value is stable.
|
|
526
|
+
const merged = new Set<string>(existingMarker?.invariants ?? []);
|
|
527
|
+
for (const inv of options.plan.providedInvariants) merged.add(inv);
|
|
528
|
+
const invariants = Array.from(merged).sort();
|
|
529
|
+
const writeStatements = buildWriteMarkerStatements({
|
|
530
|
+
storageHash: options.plan.destination.storageHash,
|
|
531
|
+
profileHash:
|
|
532
|
+
options.plan.destination.profileHash ??
|
|
533
|
+
options.destinationContract.profileHash ??
|
|
534
|
+
options.plan.destination.storageHash,
|
|
535
|
+
contractJson: options.destinationContract,
|
|
536
|
+
canonicalVersion: null,
|
|
537
|
+
meta: {},
|
|
538
|
+
invariants,
|
|
539
|
+
});
|
|
540
|
+
const statement = existingMarker ? writeStatements.update : writeStatements.insert;
|
|
541
|
+
await this.executeStatement(driver, statement);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
private async recordLedgerEntry(
|
|
545
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
546
|
+
options: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>,
|
|
547
|
+
existingMarker: ContractMarkerRecord | null,
|
|
548
|
+
executedOperations: readonly SqlMigrationPlanOperation<SqlitePlanTargetDetails>[],
|
|
549
|
+
): Promise<void> {
|
|
550
|
+
const ledgerStatement = buildLedgerInsertStatement({
|
|
551
|
+
originStorageHash: existingMarker?.storageHash ?? null,
|
|
552
|
+
originProfileHash: existingMarker?.profileHash ?? null,
|
|
553
|
+
destinationStorageHash: options.plan.destination.storageHash,
|
|
554
|
+
destinationProfileHash:
|
|
555
|
+
options.plan.destination.profileHash ??
|
|
556
|
+
options.destinationContract.profileHash ??
|
|
557
|
+
options.plan.destination.storageHash,
|
|
558
|
+
contractJsonBefore: existingMarker?.contractJson ?? null,
|
|
559
|
+
contractJsonAfter: options.destinationContract,
|
|
560
|
+
operations: executedOperations,
|
|
561
|
+
});
|
|
562
|
+
await this.executeStatement(driver, ledgerStatement);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
private async beginExclusiveTransaction(
|
|
566
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
567
|
+
): Promise<void> {
|
|
568
|
+
await driver.query('BEGIN EXCLUSIVE');
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
private async commitTransaction(
|
|
572
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
573
|
+
): Promise<void> {
|
|
574
|
+
await driver.query('COMMIT');
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private async rollbackTransaction(
|
|
578
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
579
|
+
): Promise<void> {
|
|
580
|
+
await driver.query('ROLLBACK');
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private async executeStatement(
|
|
584
|
+
driver: SqlMigrationRunnerExecuteOptions<SqlitePlanTargetDetails>['driver'],
|
|
585
|
+
statement: SqlStatement,
|
|
586
|
+
): Promise<void> {
|
|
587
|
+
if (statement.params.length > 0) {
|
|
588
|
+
await driver.query(statement.sql, statement.params);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
await driver.query(statement.sql);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Migration as SqlMigration } from '@prisma-next/family-sql/migration';
|
|
2
|
+
import type { SqlitePlanTargetDetails } from './planner-target-details';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Target-owned base class for SQLite migrations. Fixes the `SqlMigration`
|
|
6
|
+
* generic to `SqlitePlanTargetDetails` and the abstract `targetId` to the
|
|
7
|
+
* SQLite literal, so both user-authored migrations and renderer-generated
|
|
8
|
+
* scaffolds can extend `SqliteMigration` directly without redeclaring
|
|
9
|
+
* target-local identity.
|
|
10
|
+
*/
|
|
11
|
+
export abstract class SqliteMigration extends SqlMigration<SqlitePlanTargetDetails> {
|
|
12
|
+
readonly targetId = 'sqlite' as const;
|
|
13
|
+
}
|