mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
0a4c6fbace
db/database.ts opened the connection as a side effect of being imported, so the moment anything reached it the database was open. index.ts had to defer loading the app until a first-boot restore was done, and nothing in the container decided when the connection opened or closed. The module now opens the connection on first use and opens nothing at import. A connection closed on purpose (a restore, the shutdown) stays closed until it is reopened, as before. The new DatabaseLifecycle provider owns the decisions: buildApp() opens through it, which also binds the ORM to later swaps (the hook attachOrm used to register) and runs the schema bootstrap in the same order as before, and the shutdown closes through it. Its file property is the one resolved database path. The module keeps every export, so the test suites that replace it with vi.mock keep working; buildDbMock gains the new openDb.
42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
// Migrates one database and serialises it for the test run to reuse.
|
|
//
|
|
// Run as a child process under tsx by tests/global-setup.ts — not imported by
|
|
// it. vitest's globalSetup executes in the main process outside vitest's
|
|
// transform pipeline, where Node's native resolver rejects the extensionless
|
|
// imports these CommonJS-target modules use ('../migration-utils'). tsx resolves
|
|
// them, and it is already how `gen:plugin-facts` runs TypeScript directly.
|
|
import { schemaSnapshotPath, writeSchemaSnapshot } from '../src/db/schema-snapshot';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
async function main(): Promise<void> {
|
|
// Delete first, and open the database only afterwards: in test mode `openDb()`
|
|
// opens a copy of this snapshot when one exists, so a leftover from the last
|
|
// run would have this process migrating an already-migrated database and
|
|
// seeding it twice. The modules are loaded dynamically below so nothing of
|
|
// them runs before the delete.
|
|
fs.rmSync(schemaSnapshotPath(), { force: true });
|
|
|
|
const { getRawConnection } = await import('../src/db/database');
|
|
const { createStandaloneOrm, runSchemaBootstrap } = await import('../src/db/orm');
|
|
|
|
// The shared driver binds to the connection database.ts opens on the ORM's
|
|
// first connect, so this migrates that in-memory database and it can be
|
|
// serialised straight after.
|
|
const orm = await createStandaloneOrm();
|
|
try {
|
|
await runSchemaBootstrap(orm);
|
|
writeSchemaSnapshot(getRawConnection().serialize());
|
|
console.log(`[test-db] schema snapshot written to ${schemaSnapshotPath()}`);
|
|
} finally {
|
|
// Closing destroys Kysely's client, which closes the handle — after the
|
|
// snapshot has been taken.
|
|
await orm.close(true);
|
|
}
|
|
}
|
|
|
|
main().catch((err: unknown) => {
|
|
console.error('[test-db] failed to build the schema snapshot:', err);
|
|
process.exit(1);
|
|
});
|