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
+1 -1
View File
@@ -4,7 +4,7 @@
* index.ts imports this immediately after `import 'dotenv/config'` — as a
* side-effect import (not a function call) so validation runs BEFORE the rest
* of the import graph executes its module-load side effects (config.ts key
* resolution, db/database.ts initDb, ...). A plain call in index.ts's body
* resolution, ...). A plain call in index.ts's body
* would run after all of those, because imports hoist.
*/
import { validateEnvAtBoot } from './env';
+15 -7
View File
@@ -1,4 +1,3 @@
import { attachOrm, runSchemaBootstrap } from './db/orm';
import { applyGlobalMiddleware, routingCspOrigins } from './middleware/globalMiddleware';
import { readEnv } from './app-config';
import { nestLogLevels } from './app-config/nest-log-levels';
@@ -16,6 +15,7 @@ import { SettingsService } from './nest/settings/settings.service';
import { StorageService } from './nest/storage/storage.service';
import { MikroORM } from '@mikro-orm/core';
import { withRequestContext } from './nest/database/request-context';
import { DatabaseLifecycle } from './nest/database/database-lifecycle.service';
import type { INestApplication } from '@nestjs/common';
import type { ConfigType } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
@@ -60,6 +60,13 @@ import nodeHttp from 'node:http';
*/
let boundHttpServer: nodeHttp.Server | null = null;
/**
* The provider that owns the database connection, for index.ts to close it on
* shutdown. Re-exported so index.ts reaches it through the one module it loads
* after a first-boot restore.
*/
export { DatabaseLifecycle };
/**
* The http server buildApp created and bound the ws gateway to.
*
@@ -82,13 +89,14 @@ export async function buildApp(): Promise<INestApplication> {
rawBody: true,
logger: nestLogLevels(readEnv().app.logLevel),
});
// Schema first, before ANY consumer reads it. `database.ts` only opens the
// connection now; migrating and seeding is MikroORM's job and it is async, so
// this is the earliest point it can happen. It has to stay above the
// SettingsService resolution below, which is the boot's first DB read.
// Schema first, before ANY consumer reads it. The connection lifecycle is the
// DatabaseLifecycle provider's: it opens the connection (the ORM's first
// connect usually already has), binds the ORM to later swaps, and runs the
// legacy baseline, the migrations and the seeders. That is async, so this is
// the earliest point it can happen. It has to stay above the SettingsService
// resolution below, which is the boot's first DB read.
const orm = app.get(MikroORM);
attachOrm(orm);
await runSchemaBootstrap(orm);
await app.get(DatabaseLifecycle).open();
const instance = app.getHttpAdapter().getInstance();
// The http server is created HERE, not by the caller after buildApp returns,
// and that ordering is the whole point: Nest binds gateways during app.init(),
+53 -40
View File
@@ -6,49 +6,57 @@ import { readSchemaSnapshot } from './schema-snapshot';
import type Database from 'better-sqlite3';
// In test mode each vitest worker gets an isolated in-memory DB so that
// parallel forks can't race on the same file or share migration state.
const isTest = readEnv().app.isTest;
const dbPath = resolveDbPath();
let _db: Database.Database | null = null;
/**
* Set by `closeDb()`, cleared by `openDb()`. A handle that was never opened is
* opened on first use; one that was closed on purpose (a restore swapping the
* file, the shutdown) stays closed until somebody reopens it, so nothing can
* quietly reconnect to a file that is being replaced.
*/
let closedOnPurpose = false;
/**
* Opens the connection and applies the pragmas. It deliberately does NOT build
* the schema: migrations and seeding are MikroORM's job now and run from
* `db/orm.ts` during `buildApp()`, because the migrator is async and this
* function is called at module load.
* the schema: migrations and seeding are MikroORM's job and run from
* `db/orm.ts` when `DatabaseLifecycle.open()` (`nest/database/`) is called by
* `buildApp()`, because the migrator is async.
*
* The one exception is test mode, where there is no `buildApp()` to hook into
* for the handful of suites that use this singleton directly. Those open a copy
* of a schema snapshot the vitest global setup migrated once for the whole run.
* Nothing calls this at import any more. The first reader opens it: in
* production that is the ORM's first connect inside `NestFactory.create()`,
* which is still after `index.ts` ran a first-boot restore. Opening an open
* connection is a no-op.
*
* In test mode there is no `buildApp()` for the handful of suites that use this
* singleton directly, so those open a copy of a schema snapshot the vitest
* global setup migrated once for the whole run.
*/
function initDb(): void {
if (_db) {
try {
_db.exec('PRAGMA wal_checkpoint(TRUNCATE)');
} catch (e) {}
try {
_db.close();
} catch (e) {}
_db = null;
}
const snapshot = isTest ? readSchemaSnapshot() : null;
_db = openDatabase(snapshot ?? dbPath);
function openDb(): void {
if (_db) return;
// Resolved here rather than at import, so loading the module touches neither
// the environment nor the data directory.
const dbPath = resolveDbPath();
const snapshot = readEnv().app.isTest ? readSchemaSnapshot() : null;
const handle = openDatabase(snapshot ?? dbPath);
// Ahead of the journal switch now: changing journal_mode needs an exclusive
// lock, which a sibling process (reset-admin, the rotation script) may hold.
_db.exec('PRAGMA busy_timeout = 5000');
const durability = applyDurabilityPragmas(_db);
_db.exec('PRAGMA foreign_keys = ON');
// Reported so an operator can see whether their setting took — the test DB is
handle.exec('PRAGMA busy_timeout = 5000');
const durability = applyDurabilityPragmas(handle);
handle.exec('PRAGMA foreign_keys = ON');
_db = handle;
closedOnPurpose = false;
// Reported so an operator can see whether their setting took. The test DB is
// :memory: and has no journal file, so there is nothing to report there.
if (dbPath !== ':memory:') {
console.log(`[DB] journal_mode=${durability.journalMode}, synchronous=${durability.synchronous}`);
}
}
initDb();
/** The open handle, opening it on first use. Throws while it is closed on purpose. */
function current(): Database.Database {
if (!_db && !closedOnPurpose) openDb();
if (!_db) throw new Error('Database connection is not available (restore in progress?)');
return _db;
}
/**
* The live better-sqlite3 handle, for the MikroORM dialect in `orm-driver.ts`.
@@ -58,8 +66,7 @@ initDb();
* The driver calls this on every connect so it always binds the current handle.
*/
function getRawConnection(): Database.Database {
if (!_db) throw new Error('Database connection is not available (restore in progress?)');
return _db;
return current();
}
/**
@@ -80,12 +87,12 @@ function getRawConnection(): Database.Database {
*/
const db = new Proxy({} as Database.Database, {
get(_, prop: string | symbol) {
if (!_db) throw new Error('Database connection is not available (restore in progress?)');
const val = (_db as unknown as Record<string | symbol, unknown>)[prop];
return typeof val === 'function' ? val.bind(_db) : val;
const handle = current();
const val = (handle as unknown as Record<string | symbol, unknown>)[prop];
return typeof val === 'function' ? val.bind(handle) : val;
},
set(_, prop: string | symbol, val: unknown) {
(_db as unknown as Record<string | symbol, unknown>)[prop] = val;
(current() as unknown as Record<string | symbol, unknown>)[prop] = val;
return true;
},
});
@@ -119,8 +126,9 @@ async function runDemoSeed(): Promise<void> {
}
/**
* Lets `db/orm.ts` rebuild the ORM's connection when this module swaps handles,
* without this module importing the ORM (which imports this module back).
* Lets `DatabaseLifecycle` (`nest/database/`) rebuild the ORM's connection when
* this module swaps handles, without this module importing the ORM (which
* imports this module back).
*/
let onReinitialize: (() => Promise<void>) | null = null;
function registerReinitializeHook(hook: () => Promise<void>): void {
@@ -136,6 +144,7 @@ function closeDb(): void {
_db.close();
} catch (e) {}
_db = null;
closedOnPurpose = true;
console.log('[DB] Database connection closed');
}
}
@@ -144,13 +153,17 @@ function closeDb(): void {
* Reopens the connection after a restore has swapped the file underneath us.
*
* Async now, because bringing a restored backup up to date means running the
* migrations, and the hook also has to rebuild the ORM's Kysely client — it
* migrations, and the hook also has to rebuild the ORM's Kysely client: it
* cached the handle this function is about to replace.
*
* Production code reaches this through `DatabaseLifecycle.reopen()`; the export
* stays for the suites that drive the swap directly and for the mock factories
* that stub it.
*/
async function reinitialize(): Promise<void> {
console.log('[DB] Reinitializing database connection after restore...');
if (_db) closeDb();
initDb();
openDb();
if (onReinitialize) await onReinitialize();
console.log('[DB] Database reinitialized successfully');
}
@@ -167,4 +180,4 @@ async function reinitialize(): Promise<void> {
// exported — see its own docstring above for why (~190 test files' static
// typecheck, not a production `src/` need).
export { db, closeDb, reinitialize, getRawConnection, registerReinitializeHook, runDemoSeed };
export { db, openDb, closeDb, reinitialize, getRawConnection, registerReinitializeHook, runDemoSeed };
+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
+4 -4
View File
@@ -6,10 +6,10 @@ import path from 'path';
/**
* A migrated-but-empty database, serialised once per test run.
*
* Migrations are async and `database.ts::initDb()` is not — it runs at module
* load, long before anything can await. Most suites do not care (they mock the
* module or build their own database), but a handful use the singleton directly
* and need a schema to be there the moment they import it.
* Migrations are async and `database.ts::openDb()` is not: it runs on the
* connection's first read, which cannot await. Most suites do not care (they
* mock the module or build their own database), but a handful use the
* singleton directly and need a schema to be there the moment they touch it.
*
* So the vitest global setup migrates one database for the whole run and writes
* it here; every worker opens a copy. `better-sqlite3` deserialises a 1.5 MiB
+16 -6
View File
@@ -2,16 +2,19 @@ import 'reflect-metadata';
import 'dotenv/config';
// Fail-fast env validation — must stay directly after dotenv so a malformed
// variable aborts before any other module runs its import-time side effects
// (config.ts key resolution, db/database.ts initDb, ...).
// (config.ts key resolution, ...).
import './app-config/boot-validate';
import path from 'node:path';
import fs from 'node:fs';
import http from 'node:http';
import type { INestApplication } from '@nestjs/common';
// bootstrap is required inside bootstrap() below, not imported here: importing
// it opens the database, and a first-start restore (#1089) has to put the
// backup's database in place before that happens.
// bootstrap is required inside bootstrap() below, not imported here. Importing
// it no longer opens the database (the ORM's first connect inside buildApp()
// does, through DatabaseLifecycle), but a first-start restore (#1089) has to
// put the backup's database in place before anything near it loads, and
// keeping the require after the restore keeps that true by construction.
import type * as Bootstrap from './bootstrap';
import type { DatabaseLifecycle } from './nest/database/database-lifecycle.service';
// data/tmp is the driver-agnostic global scratch dir (restore-upload spool,
// mirror stream staging) and stays boot-created here. Driver-owned roots — the
@@ -84,6 +87,7 @@ const onListen = () => {
let server: http.Server;
let nestApp: INestApplication;
let database: DatabaseLifecycle | undefined;
// Strangler toggle: prefixes served by Nest (env-overridable, instant rollback).
async function bootstrap(): Promise<void> {
@@ -92,8 +96,9 @@ async function bootstrap(): Promise<void> {
// (/mcp, /.well-known, OAuth SDK, SPA catch-all). buildApp() owns the composition
// order; it is shared with the integration-test harness so they can't drift.
const restore = await restoreBeforeTheDatabaseOpens();
const { buildApp, getHttpServer } = require('./bootstrap') as typeof Bootstrap;
const { buildApp, getHttpServer, DatabaseLifecycle: Lifecycle } = require('./bootstrap') as typeof Bootstrap;
nestApp = await buildApp();
database = nestApp.get(Lifecycle);
if (restore.restored) await finishFirstBootRestore(nestApp, restore);
// The server buildApp created and bound /ws to. Creating a second one here
// would serve the REST API fine and leave the gateway attached to a socket
@@ -185,7 +190,12 @@ function shutdown(signal: string): void {
closeNestApp: async () => { await nestApp?.close(); },
getWsClients: () => getServer()?.clients ?? null,
closeMcpSessions,
closeDb: () => { require('./db/database').closeDb(); },
// Through the lifecycle provider once the app is up; before that (a signal
// that beat bootstrap()) the module function is all there is.
closeDb: () => {
if (database) database.close();
else (require('./db/database') as typeof import('./db/database')).closeDb();
},
logInfo: sLogInfo,
logError: sLogError,
exit: (code: number) => process.exit(code),
+3
View File
@@ -31,6 +31,7 @@ import { TrekExceptionFilter } from './common/trek-exception.filter';
import { ZodValidationPipe } from './common/zod-validation.pipe';
import { ConfigModule } from './config/config.module';
import { OrmModule } from './database/orm.module';
import { DatabaseLifecycleModule } from './database/database-lifecycle.module';
import { DayNotesModule } from './day-notes/day-notes.module';
import { DaysModule } from './days/days.module';
import { DocSyncModule } from './doc-sync/doc-sync.module';
@@ -170,6 +171,8 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
// facade) is gone — Plan 4 Task 4 deleted it once every domain moved onto
// repositories/`UnitOfWork`. `OrmModule` is the module that survives.
OrmModule,
// The connection's open/close/reopen, resolved by buildApp() and index.ts.
DatabaseLifecycleModule,
// MfaPolicyGuard (Plan 3b Task 1) is registered directly below as this
// module's own APP_GUARD, never per-controller — so it is the one place
// that needs `forFeature` for its three repositories; `Users` is not
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { DatabaseLifecycle } from './database-lifecycle.service';
/**
* Provides `DatabaseLifecycle`. Not global: the backup port and the app root
* import it by name, so a partial test harness that never touches the
* connection lifecycle does not have to supply the `MikroORM` it injects.
*/
@Module({ providers: [DatabaseLifecycle], exports: [DatabaseLifecycle] })
export class DatabaseLifecycleModule {}
@@ -0,0 +1,76 @@
import { Injectable } from '@nestjs/common';
import { MikroORM } from '@mikro-orm/core';
import { closeDb, getRawConnection, registerReinitializeHook, reinitialize } from '../../db/database';
import { resolveDbPath } from '../../db/db-path';
import { runSchemaBootstrap } from '../../db/orm';
/**
* The one owner of the core database connection's lifecycle: open, close and
* reopen, plus the schema bootstrap that has to follow an open.
*
* The handle itself still lives in `db/database.ts`, because the ORM's bound
* driver (`db/orm-driver.ts`) reads it on every connect and the test suites
* replace that module wholesale with `vi.mock`. What moved here is the
* decision about WHEN it opens and closes: `buildApp()` opens through this and
* the shutdown closes through this. Nothing opens the database at import any
* more; the first connect does, and this provider makes it explicit. The
* restore and the demo reset still call the module's `closeDb()`/`reinitialize()`
* themselves until they move onto `close()`/`reopen()`.
*/
@Injectable()
export class DatabaseLifecycle {
constructor(private readonly orm: MikroORM) {}
/**
* The database file this process runs on, resolved the one way every caller
* shares (`db/db-path.ts`): `TREK_DB_FILE` when set, `data/travel.db`
* otherwise, `:memory:` under test.
*/
get file(): string {
return resolveDbPath();
}
/**
* Opens the connection, binds the ORM to its later swaps and brings the schema
* to head (legacy baseline, migrations, the deferred index, seeders, demo
* seed, in that order: see `db/orm.ts`). Called once by `buildApp()`, before
* anything reads the database.
*/
async open(): Promise<void> {
// Normally a no-op: the ORM's first connect inside NestFactory.create()
// already opened it. Said here so the boot does not depend on that.
getRawConnection();
registerReinitializeHook(() => this.rebindOrm());
await runSchemaBootstrap(this.orm);
}
/** Checkpoints and closes the connection. A closed connection stays closed until `reopen()`. */
close(): void {
closeDb();
}
/**
* Reopens the connection after its file was replaced, rebuilds the ORM's
* cached client around the new handle and migrates the new file forward (a
* restored backup may predate this release).
*/
async reopen(): Promise<void> {
await reinitialize();
}
/**
* Kysely caches whatever handle it was given, so without this the ORM would
* keep talking to the closed one. Closing and reconnecting sends the driver
* back through `createKyselyDialect()`, which picks up the new handle.
*/
private async rebindOrm(): Promise<void> {
const connection = this.orm.em.getConnection();
// The handle is owned and closed by `db/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();
// No pre-migrate snapshot: the file was just unpacked from an archive that
// is still there to go back to.
await runSchemaBootstrap(this.orm, { snapshot: false });
}
}