@fabrika/db 0.9.0-dev.18 → 0.9.0-dev.19
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/testing-sqlite.ts +78 -16
- package/src/testing.ts +1 -1
- package/types/src/testing-sqlite.d.ts +15 -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.19",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"test": "bun test"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
96
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
97
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
95
|
+
"@fabrika/auth-core": "0.9.0-dev.19",
|
|
96
|
+
"@fabrika/platform": "0.9.0-dev.19",
|
|
97
|
+
"@fabrika/platform-node": "0.9.0-dev.19",
|
|
98
98
|
"kysely": "0.29.6",
|
|
99
99
|
"kysely-postgres-js": "5.0.1",
|
|
100
100
|
"zod": "^3.25.76"
|
package/src/testing-sqlite.ts
CHANGED
|
@@ -67,6 +67,59 @@ export interface TestDatabaseOptions {
|
|
|
67
67
|
readonly migrations: string | URL
|
|
68
68
|
/** The test file. `@fabrika/db/testing` names it when it skips; this kit never skips. */
|
|
69
69
|
readonly label: string
|
|
70
|
+
/** A database migrated and seeded once (`testDatabaseTemplate`), which this one starts as a copy of. */
|
|
71
|
+
readonly template?: TestDatabaseTemplate
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A migrated database with its base seed, as an image every `testDatabase({ template })` copies. */
|
|
75
|
+
export interface TestDatabaseTemplate {
|
|
76
|
+
/** The migrations it was migrated with: a database of other migrations does not start from it. */
|
|
77
|
+
readonly migrations: string
|
|
78
|
+
readonly image: Uint8Array
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Each migrations' image this process migrated, by the migrations: a test file copies it instead of migrating again. */
|
|
82
|
+
const migratedImages = new Map<string, Uint8Array>()
|
|
83
|
+
|
|
84
|
+
/** The identity of a set of migrations: every file's name and text. */
|
|
85
|
+
function migrationsKey(migrations: SqliteMigrationBundle['migrations']): string {
|
|
86
|
+
return JSON.stringify(migrations.map((migration) => [migration.name, migration.sql]))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A fresh in-memory database with the framework's and the application's migrations applied, or a copy of `image`. */
|
|
90
|
+
async function migratedDatabase(migrations: SqliteMigrationBundle['migrations'], image: Uint8Array | undefined): Promise<BunDatabase> {
|
|
91
|
+
if (image !== undefined) {
|
|
92
|
+
return BunDatabase.deserialize(image)
|
|
93
|
+
}
|
|
94
|
+
const sqlite = new BunDatabase(':memory:')
|
|
95
|
+
const storage = bunSqliteStorage(sqlite)
|
|
96
|
+
try {
|
|
97
|
+
await migrateSqlite({ exec: storage.sql.exec, transactionSync: (fn) => storage.transactionSync(fn) }, [
|
|
98
|
+
{ bundle: FRAMEWORK_MIGRATION_BUNDLE, migrations: SQLITE_FRAMEWORK_MIGRATIONS },
|
|
99
|
+
{ bundle: APPLICATION_MIGRATION_BUNDLE, migrations },
|
|
100
|
+
])
|
|
101
|
+
} catch (error) {
|
|
102
|
+
sqlite.close()
|
|
103
|
+
throw error
|
|
104
|
+
}
|
|
105
|
+
return sqlite
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Migrates a database once, runs `seed` in it, and keeps it as the image each `testDatabase({ template })` starts as a
|
|
110
|
+
* copy of: in milliseconds, without migrating or seeding again.
|
|
111
|
+
*/
|
|
112
|
+
export async function testDatabaseTemplate(
|
|
113
|
+
options: Omit<TestDatabaseOptions, 'template'> & { readonly seed?: (database: TestDatabase) => Promise<void> },
|
|
114
|
+
): Promise<TestDatabaseTemplate> {
|
|
115
|
+
refuseInProduction(MODULE)
|
|
116
|
+
const { database, sqlite, migrations } = await openTestDatabase({ migrations: options.migrations, label: options.label })
|
|
117
|
+
try {
|
|
118
|
+
await options.seed?.(database)
|
|
119
|
+
return Object.freeze({ migrations, image: sqlite.serialize() })
|
|
120
|
+
} finally {
|
|
121
|
+
await database.close()
|
|
122
|
+
}
|
|
70
123
|
}
|
|
71
124
|
|
|
72
125
|
type Exec = (query: string, ...bindings: SqliteValue[]) => Record<string, SqliteValue>[]
|
|
@@ -494,18 +547,23 @@ function applicationMigrations(directory: string): SqliteMigrationBundle['migrat
|
|
|
494
547
|
*/
|
|
495
548
|
export async function testDatabase(options: TestDatabaseOptions): Promise<TestDatabase> {
|
|
496
549
|
refuseInProduction(MODULE)
|
|
550
|
+
return (await openTestDatabase(options)).database
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** The test database of `options`, with its bun:sqlite database and the key of its migrations. */
|
|
554
|
+
async function openTestDatabase(
|
|
555
|
+
options: TestDatabaseOptions,
|
|
556
|
+
): Promise<{ readonly database: TestDatabase; readonly sqlite: BunDatabase; readonly migrations: string }> {
|
|
497
557
|
const migrations = applicationMigrations(migrationsDirectory(options.migrations))
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
await migrateSqlite({ exec: storage.sql.exec, transactionSync: (fn) => storage.transactionSync(fn) }, [
|
|
502
|
-
{ bundle: FRAMEWORK_MIGRATION_BUNDLE, migrations: SQLITE_FRAMEWORK_MIGRATIONS },
|
|
503
|
-
{ bundle: APPLICATION_MIGRATION_BUNDLE, migrations },
|
|
504
|
-
])
|
|
505
|
-
} catch (error) {
|
|
506
|
-
sqlite.close()
|
|
507
|
-
throw error
|
|
558
|
+
const key = migrationsKey(migrations)
|
|
559
|
+
if (options.template !== undefined && options.template.migrations !== key) {
|
|
560
|
+
throw new Error(`${options.label}: the template was migrated with other migrations than ${String(options.migrations)}; build it again`)
|
|
508
561
|
}
|
|
562
|
+
const sqlite = await migratedDatabase(migrations, options.template?.image ?? migratedImages.get(key))
|
|
563
|
+
if (options.template === undefined && !migratedImages.has(key)) {
|
|
564
|
+
migratedImages.set(key, sqlite.serialize())
|
|
565
|
+
}
|
|
566
|
+
const storage = bunSqliteStorage(sqlite)
|
|
509
567
|
const exec: Exec = (query, ...bindings) => storage.sql.exec(query, ...bindings).toArray()
|
|
510
568
|
const columns = readColumns(exec)
|
|
511
569
|
const withoutRowid = new Set(
|
|
@@ -604,13 +662,17 @@ export async function testDatabase(options: TestDatabaseOptions): Promise<TestDa
|
|
|
604
662
|
}
|
|
605
663
|
const database = openSqlite(storage)
|
|
606
664
|
return {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
665
|
+
database: {
|
|
666
|
+
...testSeeding(backend),
|
|
667
|
+
database,
|
|
668
|
+
schema: 'main',
|
|
669
|
+
async close() {
|
|
670
|
+
await database.close()
|
|
671
|
+
sqlite.close()
|
|
672
|
+
},
|
|
613
673
|
},
|
|
674
|
+
sqlite,
|
|
675
|
+
migrations: key,
|
|
614
676
|
}
|
|
615
677
|
}
|
|
616
678
|
|
package/src/testing.ts
CHANGED
|
@@ -266,7 +266,7 @@ const TEST_SCHEMA = /^app_test_[0-9a-f]{16}$/
|
|
|
266
266
|
const TEST_ROLE = /^(?:app|run)_(test_[0-9a-f]{16})$/
|
|
267
267
|
/**
|
|
268
268
|
* Why an abandoned family is left alone: `DROP ROLE` of a role that still owns something in another database of the
|
|
269
|
-
* cluster, and a family another provisioner created
|
|
269
|
+
* cluster, and a family another provisioner created, which only it administers.
|
|
270
270
|
*/
|
|
271
271
|
const LEFT_TO_ITS_OWNER: ReadonlySet<unknown> = new Set(['2BP01', '42501'])
|
|
272
272
|
|
|
@@ -7,7 +7,22 @@ export interface TestDatabaseOptions {
|
|
|
7
7
|
readonly migrations: string | URL;
|
|
8
8
|
/** The test file. `@fabrika/db/testing` names it when it skips; this kit never skips. */
|
|
9
9
|
readonly label: string;
|
|
10
|
+
/** A database migrated and seeded once (`testDatabaseTemplate`), which this one starts as a copy of. */
|
|
11
|
+
readonly template?: TestDatabaseTemplate;
|
|
10
12
|
}
|
|
13
|
+
/** A migrated database with its base seed, as an image every `testDatabase({ template })` copies. */
|
|
14
|
+
export interface TestDatabaseTemplate {
|
|
15
|
+
/** The migrations it was migrated with: a database of other migrations does not start from it. */
|
|
16
|
+
readonly migrations: string;
|
|
17
|
+
readonly image: Uint8Array;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Migrates a database once, runs `seed` in it, and keeps it as the image each `testDatabase({ template })` starts as a
|
|
21
|
+
* copy of: in milliseconds, without migrating or seeding again.
|
|
22
|
+
*/
|
|
23
|
+
export declare function testDatabaseTemplate(options: Omit<TestDatabaseOptions, 'template'> & {
|
|
24
|
+
readonly seed?: (database: TestDatabase) => Promise<void>;
|
|
25
|
+
}): Promise<TestDatabaseTemplate>;
|
|
11
26
|
/**
|
|
12
27
|
* A fresh in-memory database with the framework's and the application's migrations applied by the runner a Durable
|
|
13
28
|
* Object boots with (`@fabrika/db/sqlite-migrate`), opened with its engine (`@fabrika/db/sqlite`). Never `null`: it
|