@fabrika/db 0.9.0-dev.48 → 0.9.0-dev.49
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/package.json +4 -4
- package/src/sqlite-migrate.ts +102 -1
- package/types/src/sqlite-migrate.d.ts +32 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.49",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"./src/testing.ts",
|
|
@@ -96,9 +96,9 @@
|
|
|
96
96
|
"test": "bun test"
|
|
97
97
|
},
|
|
98
98
|
"dependencies": {
|
|
99
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
100
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
101
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
99
|
+
"@fabrika/auth-core": "0.9.0-dev.49",
|
|
100
|
+
"@fabrika/platform": "0.9.0-dev.49",
|
|
101
|
+
"@fabrika/platform-node": "0.9.0-dev.49",
|
|
102
102
|
"kysely": "0.29.6",
|
|
103
103
|
"kysely-postgres-js": "5.0.1",
|
|
104
104
|
"zod": "^3.25.76"
|
package/src/sqlite-migrate.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// database when its Durable Object boots (ADR-0065), in a ledger in the same database. The identity and the
|
|
3
3
|
// checksum rule are the Postgres runner's (ADR-0017): `(bundle, name)`, and the SHA-256 of an applied file's text.
|
|
4
4
|
// Every new object instance runs it, so a bundle whose files match the digest recorded for it is not read further.
|
|
5
|
-
//
|
|
5
|
+
// `migrateSqlite` commits each file on its own; `planSqliteMigrations` applies every pending file of every bundle in one
|
|
6
|
+
// transaction, which the migrating boot of an application's object uses (ADR-0069 §3). Runs inside a Worker: no Bun import.
|
|
6
7
|
|
|
7
8
|
/** What the runner needs of the database; a Durable Object's `storage.sql.exec` and `storage.transactionSync` fit it. */
|
|
8
9
|
export interface SqliteMigrationTarget {
|
|
@@ -240,3 +241,103 @@ export async function migrateSqlite(
|
|
|
240
241
|
}
|
|
241
242
|
return { applied }
|
|
242
243
|
}
|
|
244
|
+
|
|
245
|
+
/** One row of the ledger, as a migration record names it: the identity and the checksum, without the time it ran. */
|
|
246
|
+
export interface SqliteLedgerEntry {
|
|
247
|
+
readonly bundle: string
|
|
248
|
+
readonly name: string
|
|
249
|
+
readonly checksum: string
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The ledger's rows in `(bundle, name)` order, and their digest. */
|
|
253
|
+
export interface SqliteLedger {
|
|
254
|
+
readonly entries: readonly SqliteLedgerEntry[]
|
|
255
|
+
readonly digest: string
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The SHA-256 of the rows' `bundle name checksum` lines in `(bundle, name)` order: neither part holds a space or a newline. */
|
|
259
|
+
export async function sqliteLedgerDigest(entries: readonly SqliteLedgerEntry[]): Promise<string> {
|
|
260
|
+
const sorted = [...entries].sort((a, b) => compareNames(a.bundle, b.bundle) || compareNames(a.name, b.name))
|
|
261
|
+
return await checksumOf(sorted.map((entry) => `${entry.bundle} ${entry.name} ${entry.checksum}\n`).join(''))
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** The ledger as it stands; empty while the table does not exist. */
|
|
265
|
+
export async function readSqliteLedger(target: SqliteMigrationTarget): Promise<SqliteLedger> {
|
|
266
|
+
const entries = [...readLedger(target)].map(([identity, checksum]): SqliteLedgerEntry => {
|
|
267
|
+
const slash = identity.indexOf('/')
|
|
268
|
+
return { bundle: identity.slice(0, slash), name: identity.slice(slash + 1), checksum }
|
|
269
|
+
}).sort((a, b) => compareNames(a.bundle, b.bundle) || compareNames(a.name, b.name))
|
|
270
|
+
return { entries, digest: await sqliteLedgerDigest(entries) }
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** What the database needs to reach the bundles: nothing, a digest only, or pending files. */
|
|
274
|
+
export interface SqliteMigrationPlan {
|
|
275
|
+
/** The ledger the plan starts from; `null` when every bundle matched its digest and the ledger was not read. */
|
|
276
|
+
readonly ledger: SqliteLedger | null
|
|
277
|
+
/** The files to apply, the bundles in the order given and each bundle's files in name order. */
|
|
278
|
+
readonly pending: readonly SqliteLedgerEntry[]
|
|
279
|
+
/** Whether `apply` writes anything: a pending file, or a bundle digest that is out of date. */
|
|
280
|
+
readonly writes: boolean
|
|
281
|
+
/**
|
|
282
|
+
* Applies every pending file with its ledger row, and records every out-of-date bundle digest, in one transaction
|
|
283
|
+
* that ends with `PRAGMA foreign_key_check`: a failing file or a row the check returns rolls the whole set back.
|
|
284
|
+
* Answers the identities it applied.
|
|
285
|
+
*/
|
|
286
|
+
apply(): readonly string[]
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Plans the bundles against the ledger without writing; refuses what `migrateSqlite` refuses, before anything runs. */
|
|
290
|
+
export async function planSqliteMigrations(target: SqliteMigrationTarget, bundles: readonly SqliteMigrationBundle[]): Promise<SqliteMigrationPlan> {
|
|
291
|
+
const planned = await plan(bundles)
|
|
292
|
+
const digests = readDigests(target, planned)
|
|
293
|
+
const stale = planned.filter((bundle) => digests.get(bundle.bundle) !== bundle.digest)
|
|
294
|
+
if (stale.length === 0) {
|
|
295
|
+
return { ledger: null, pending: [], writes: false, apply: () => [] }
|
|
296
|
+
}
|
|
297
|
+
const ledger = await readSqliteLedger(target)
|
|
298
|
+
const recorded = new Map(ledger.entries.map((entry) => [`${entry.bundle}/${entry.name}`, entry.checksum]))
|
|
299
|
+
verify(planned, recorded)
|
|
300
|
+
const pending = stale.flatMap((bundle) => bundle.migrations.filter((migration) => !recorded.has(`${migration.bundle}/${migration.name}`)))
|
|
301
|
+
return {
|
|
302
|
+
ledger,
|
|
303
|
+
pending: pending.map(({ bundle, name, checksum }) => ({ bundle, name, checksum })),
|
|
304
|
+
writes: true,
|
|
305
|
+
apply: () => applyAtomically(target, stale, pending),
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
class ForeignKeyViolation extends Error {
|
|
310
|
+
override readonly name = 'ForeignKeyViolation'
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function applyAtomically(target: SqliteMigrationTarget, stale: readonly PlannedBundle[], pending: readonly PlannedMigration[]): readonly string[] {
|
|
314
|
+
const appliedAt = new Date().toISOString()
|
|
315
|
+
try {
|
|
316
|
+
target.transactionSync(() => {
|
|
317
|
+
target.exec(LEDGER_DDL)
|
|
318
|
+
for (const migration of pending) {
|
|
319
|
+
target.exec(migration.sql)
|
|
320
|
+
target.exec(
|
|
321
|
+
`INSERT INTO ${LEDGER} (bundle, name, checksum, applied_at) VALUES (?, ?, ?, ?)`,
|
|
322
|
+
migration.bundle,
|
|
323
|
+
migration.name,
|
|
324
|
+
migration.checksum,
|
|
325
|
+
appliedAt,
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
for (const bundle of stale) recordDigest(target, bundle)
|
|
329
|
+
if (target.exec('PRAGMA foreign_key_check').toArray().length > 0) {
|
|
330
|
+
throw new ForeignKeyViolation('a row violates a foreign key')
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
} catch (error) {
|
|
334
|
+
const files = pending.map((migration) => `${migration.bundle}/${migration.name}`).join(', ')
|
|
335
|
+
throw new SqliteMigrationError(
|
|
336
|
+
error instanceof ForeignKeyViolation
|
|
337
|
+
? `SQLite migrations ${files} left a row that violates a foreign key; none of them was applied`
|
|
338
|
+
: `SQLite migrations ${files} failed; none of them was applied`,
|
|
339
|
+
{ cause: error },
|
|
340
|
+
)
|
|
341
|
+
}
|
|
342
|
+
return pending.map((migration) => `${migration.bundle}/${migration.name}`)
|
|
343
|
+
}
|
|
@@ -26,3 +26,35 @@ export declare class SqliteMigrationError extends Error {
|
|
|
26
26
|
export declare function migrateSqlite(target: SqliteMigrationTarget, bundles: readonly SqliteMigrationBundle[]): Promise<{
|
|
27
27
|
readonly applied: readonly string[];
|
|
28
28
|
}>;
|
|
29
|
+
/** One row of the ledger, as a migration record names it: the identity and the checksum, without the time it ran. */
|
|
30
|
+
export interface SqliteLedgerEntry {
|
|
31
|
+
readonly bundle: string;
|
|
32
|
+
readonly name: string;
|
|
33
|
+
readonly checksum: string;
|
|
34
|
+
}
|
|
35
|
+
/** The ledger's rows in `(bundle, name)` order, and their digest. */
|
|
36
|
+
export interface SqliteLedger {
|
|
37
|
+
readonly entries: readonly SqliteLedgerEntry[];
|
|
38
|
+
readonly digest: string;
|
|
39
|
+
}
|
|
40
|
+
/** The SHA-256 of the rows' `bundle name checksum` lines in `(bundle, name)` order: neither part holds a space or a newline. */
|
|
41
|
+
export declare function sqliteLedgerDigest(entries: readonly SqliteLedgerEntry[]): Promise<string>;
|
|
42
|
+
/** The ledger as it stands; empty while the table does not exist. */
|
|
43
|
+
export declare function readSqliteLedger(target: SqliteMigrationTarget): Promise<SqliteLedger>;
|
|
44
|
+
/** What the database needs to reach the bundles: nothing, a digest only, or pending files. */
|
|
45
|
+
export interface SqliteMigrationPlan {
|
|
46
|
+
/** The ledger the plan starts from; `null` when every bundle matched its digest and the ledger was not read. */
|
|
47
|
+
readonly ledger: SqliteLedger | null;
|
|
48
|
+
/** The files to apply, the bundles in the order given and each bundle's files in name order. */
|
|
49
|
+
readonly pending: readonly SqliteLedgerEntry[];
|
|
50
|
+
/** Whether `apply` writes anything: a pending file, or a bundle digest that is out of date. */
|
|
51
|
+
readonly writes: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Applies every pending file with its ledger row, and records every out-of-date bundle digest, in one transaction
|
|
54
|
+
* that ends with `PRAGMA foreign_key_check`: a failing file or a row the check returns rolls the whole set back.
|
|
55
|
+
* Answers the identities it applied.
|
|
56
|
+
*/
|
|
57
|
+
apply(): readonly string[];
|
|
58
|
+
}
|
|
59
|
+
/** Plans the bundles against the ledger without writing; refuses what `migrateSqlite` refuses, before anything runs. */
|
|
60
|
+
export declare function planSqliteMigrations(target: SqliteMigrationTarget, bundles: readonly SqliteMigrationBundle[]): Promise<SqliteMigrationPlan>;
|