refactor(db): open the database through a DatabaseLifecycle provider

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.
This commit is contained in:
Maurice
2026-10-08 15:21:05 +02:00
parent 5aa9ac8529
commit 0a4c6fbace
18 changed files with 456 additions and 96 deletions
+4 -24
View File
@@ -1,5 +1,5 @@
import mikroOrmConfig from '../mikro-orm.config';
import { registerReinitializeHook, runDemoSeed } from './database';
import { runDemoSeed } from './database';
import { ensureEmailCaseIndex } from './email-case-index';
import { migrateToHead, NO_SAFETY_NET } from './legacy-baseline';
import { preMigrateSnapshot } from './pre-migrate-snapshot';
@@ -25,7 +25,7 @@ import { MikroORM as SqliteMikroORM } from '@mikro-orm/sqlite';
* class's own `readonly` form while a direct `init()` yields the mutable one;
* nothing here reads that parameter.
*/
type AnyOrm = MikroORM<
export type AnyOrm = MikroORM<
IDatabaseDriver,
EntityManager<IDatabaseDriver>,
readonly (string | EntityClass<AnyEntity> | EntitySchema)[]
@@ -57,8 +57,8 @@ export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { sna
//
// Plan 3c Task 0b (R9): wrapping the call HERE, the one place
// `runSchemaBootstrap` already has `orm` in scope, covers BOTH of
// `runDemoSeed`'s real call sites at once (`bootstrap.ts`'s initial boot
// and `attachOrm`'s restore hook below, both of which only ever reach
// `runDemoSeed`'s real call sites at once (`DatabaseLifecycle.open()` at
// boot and its restore hook in `reopen()`, both of which only ever reach
// `runDemoSeed` through this function) with no second wrapper to keep in
// sync.
//
@@ -72,26 +72,6 @@ export async function runSchemaBootstrap(orm: AnyOrm, { snapshot = true }: { sna
await withRequestContext(orm, () => runDemoSeed());
}
/**
* Binds the ORM to this process's connection lifecycle.
*
* A restore swaps the SQLite file and reopens the raw handle; Kysely caches
* whatever handle it was given, so without this the ORM would keep talking to a
* closed connection. Closing and reconnecting sends the driver back through
* `createKyselyDialect()`, which picks up the new handle — and the restored file
* may be an older backup, so it gets migrated forward too.
*/
export function attachOrm(orm: AnyOrm): void {
registerReinitializeHook(async () => {
const connection = orm.em.getConnection();
// The handle is owned and closed by `database.ts`; the bound driver's
// `destroy()` is a no-op (see `orm-driver.ts`), so this close never reaches it.
await connection.close(true);
await connection.connect();
await runSchemaBootstrap(orm, { snapshot: false });
});
}
/**
* An ORM instance outside Nest, for the vitest global setup that builds the
* schema snapshot. Production uses the container-owned one from