diff --git a/server/src/app-config/db-settings.ts b/server/src/app-config/db-settings.ts new file mode 100644 index 000000000..fc225efdb --- /dev/null +++ b/server/src/app-config/db-settings.ts @@ -0,0 +1,14 @@ +/** The databases TREK can run on. MySQL and MariaDB are out of scope (server/CLAUDE.md). */ +export type EngineId = 'sqlite' | 'postgres'; + +/** + * Which database this process runs on, as `deriveDb` reads it from + * `TREK_DB_URL`. The Postgres arm carries what its pool needs, so nothing + * downstream has to check the URL again. + */ +export type DbEngineSettings = { engine: 'sqlite' } | { engine: 'postgres'; url: string; poolMax: number }; + +/** The most connections the Postgres pool opens (`TREK_DB_POOL_MAX` is refused above it). */ +export const DB_POOL_MAX_CEILING = 100; +/** The pool size when `TREK_DB_POOL_MAX` is unset. */ +export const DB_POOL_MAX_DEFAULT = 10; diff --git a/server/src/app-config/derive.ts b/server/src/app-config/derive.ts index 097e4d61b..aa8b59475 100644 --- a/server/src/app-config/derive.ts +++ b/server/src/app-config/derive.ts @@ -14,6 +14,7 @@ * call, which is what keeps the runtime env-mutation semantics the test suite * depends on. Zod validation runs once at boot (env.schema.ts), never here. */ +import { DB_POOL_MAX_CEILING, DB_POOL_MAX_DEFAULT, type DbEngineSettings } from './db-settings'; import { imageVersion } from './image-version'; import { csvList, @@ -336,16 +337,23 @@ export function deriveBackup(raw: RawEnv) { }; } +/** The database settings `readEnv().db` answers with. */ +export type DbSettings = ReturnType; + export function deriveDb(raw: RawEnv) { const durability = resolveDurability(raw.TREK_DB_JOURNAL_MODE, raw.TREK_DB_SYNCHRONOUS); const url = raw.TREK_DB_URL?.trim() || undefined; + const engine: DbEngineSettings = url + ? { + engine: 'postgres', + // The connection URL carries the password, so it is never logged (db/engine/redact-url.ts). + url, + poolMax: Math.min(positiveIntOr(raw.TREK_DB_POOL_MAX, DB_POOL_MAX_DEFAULT), DB_POOL_MAX_CEILING), + } + : { engine: 'sqlite' }; return { /** Which database this process runs on: Postgres when TREK_DB_URL names one, the SQLite file otherwise. */ - engine: (url ? 'postgres' : 'sqlite') as 'sqlite' | 'postgres', - /** The Postgres connection URL; it carries the password, so never log it (db/engine/redact-url.ts). */ - url, - /** The largest number of connections the Postgres pool opens. */ - poolMax: Math.min(positiveIntOr(raw.TREK_DB_POOL_MAX, 10), 100), + ...engine, /** Where pg_dump and pg_restore are; unset, the usual install directories are tried (never PATH). */ pgBinDir: raw.TREK_DB_PG_BIN_DIR?.trim() || undefined, trekDbFile: raw.TREK_DB_FILE, diff --git a/server/src/app-config/env.schema.ts b/server/src/app-config/env.schema.ts index 4936776c1..c7961665b 100644 --- a/server/src/app-config/env.schema.ts +++ b/server/src/app-config/env.schema.ts @@ -9,6 +9,7 @@ * listed here pass through untouched (Zod strips unknown keys, it does not * reject them). */ +import { DB_POOL_MAX_CEILING } from './db-settings'; import { isP256PrivateKey, isServiceKeyShape, @@ -167,7 +168,7 @@ export const envSchema = z.object({ // A Postgres database instead of the SQLite file. Its value is a secret (it // carries the password), so it is masked in the boot report. TREK_DB_URL: postgresUrl, - TREK_DB_POOL_MAX: integer(1, 100), + TREK_DB_POOL_MAX: integer(1, DB_POOL_MAX_CEILING), TREK_DB_PG_BIN_DIR: anyString, TREK_DB_FILE: anyString, // The two SQLite pragmas are deliberately unvalidated: a typo here must fall diff --git a/server/src/app-config/index.ts b/server/src/app-config/index.ts index f5e3c392f..bfea8b697 100644 --- a/server/src/app-config/index.ts +++ b/server/src/app-config/index.ts @@ -2,6 +2,7 @@ export { readEnv, validateEnvAtBoot } from './env'; export { getAppUrl, getMcpSafeUrl } from './app-url'; export { runningVersion } from './running-version'; export type { AppEnv, RawEnv } from './env'; +export type { DbSettings } from './derive'; export { deriveAll, deriveApp, diff --git a/server/src/db/engine/engine.ts b/server/src/db/engine/engine.ts index 08681c22e..5a62bdeaa 100644 --- a/server/src/db/engine/engine.ts +++ b/server/src/db/engine/engine.ts @@ -1,9 +1,7 @@ +import type { EngineId } from '../../app-config/db-settings'; import type { AnyOrm } from '../orm'; import type { EntityManager, Options } from '@mikro-orm/core'; -/** The databases TREK can be pointed at. MySQL/MariaDB are out of scope (server/CLAUDE.md). */ -type EngineId = 'sqlite' | 'postgres'; - /** * One database engine: what the ORM needs to reach it, how the connection * opens, closes and comes back after a restore, and how a database is brought @@ -22,21 +20,19 @@ export interface DatabaseEngine { /** What MikroORM needs to reach the database: the driver and its target, the pool, driver options. */ ormOptions(): Partial; /** - * Whether a reopen hands the ORM a different connection, so its client has - * to be closed and connected again around it. SQLite swaps the file under - * one handle; a Postgres restore replaces the contents of the same database, - * which the pool's connections keep reaching. + * Called once at boot, before the schema bootstrap. `onReopened` runs after + * each later reopen, once the ORM is pointed at the reopened database: the + * engine does what its own reopen needs first (SQLite hands the ORM a new + * handle, so the ORM's connection is closed and connected again), then calls + * it, and the caller migrates the database and runs whatever follows. */ - readonly reopenSwapsConnection: boolean; - /** - * Called once at boot, before the schema bootstrap. `onReopened` is what - * has to follow a later reopen (rebinding the ORM, migrating the restored - * database forward). - */ - open(onReopened: () => Promise): void; + open(orm: AnyOrm, onReopened: () => Promise): void; /** Closes what the engine itself holds; the ORM's own pool is closed with the Nest container. */ close(): void; - /** Comes back after the database was replaced by a restore, then runs `onReopened`. */ + /** + * Comes back after the database was replaced by a restore, then runs the + * `onReopened` that `open` registered. Throws when `open` has not run. + */ reopen(): Promise; /** * Brings the database to the current schema, then seeds it. `snapshot: diff --git a/server/src/db/engine/index.ts b/server/src/db/engine/index.ts index 43ff34659..fb80cf17e 100644 --- a/server/src/db/engine/index.ts +++ b/server/src/db/engine/index.ts @@ -1,4 +1,6 @@ import { readEnv } from '../../app-config'; +import type { DbEngineSettings } from '../../app-config/db-settings'; +import type { DbSettings } from '../../app-config/derive'; import { ALL_ENTITIES } from '../entities'; import type { DatabaseEngine } from './engine'; import { PostgresEngine } from './postgres/postgres-engine'; @@ -15,11 +17,22 @@ export type { DatabaseEngine } from './engine'; * call, like everything `readEnv()` answers, so nothing caches a choice a test * or a restart changes. */ -export function resolveEngine(db: ReturnType['db'] = readEnv().db): DatabaseEngine { - if (db.engine === 'postgres' && db.url) return new PostgresEngine({ url: db.url, poolMax: db.poolMax }); +export function resolveEngine(db: DbEngineSettings = readEnv().db): DatabaseEngine { + if (db.engine === 'postgres') return new PostgresEngine(db); return new SqliteEngine(); } +/** + * The Postgres URL and the directory of its client tools, when the settings + * name a Postgres database; null on SQLite. For the code that drives pg_dump + * and pg_restore, which needs both and must not read them from the environment + * again. + */ +export function postgresTarget(db: DbSettings): { url: string; binDir: string | undefined } | null { + if (db.engine !== 'postgres') return null; + return { url: db.url, binDir: db.pgBinDir }; +} + /** * The MikroORM config for `engine`: what every engine shares (entities, * migrations, seeders) and what the engine adds. `src/mikro-orm.config.ts` diff --git a/server/src/db/engine/postgres/postgres-engine.ts b/server/src/db/engine/postgres/postgres-engine.ts index f453f14d1..e7b48b137 100644 --- a/server/src/db/engine/postgres/postgres-engine.ts +++ b/server/src/db/engine/postgres/postgres-engine.ts @@ -1,59 +1,15 @@ +import type { DbEngineSettings } from '../../../app-config/db-settings'; import { NO_SAFETY_NET } from '../../legacy-baseline'; import { finishSchemaBootstrap, type AnyOrm } from '../../orm'; import type { DatabaseEngine } from '../engine'; import { redactUrl } from '../redact-url'; import { TrekPostgreSqlDriver } from './postgres-driver'; import { bootstrapPostgresSchema, defaultPostgresBootstrapDeps, type PostgresBootstrapDeps } from './schema-bootstrap'; +import { sslOptionFor } from './ssl'; import type { EntityManager } from '@mikro-orm/core'; -import { readFileSync } from 'node:fs'; - -/** What `TREK_DB_URL` and `TREK_DB_POOL_MAX` resolve to (`readEnv().db`). */ -export interface PostgresSettings { - url: string; - poolMax: number; -} - -/** The `ssl` option of a `pg` pool, as `sslOptionFor` builds it. */ -export type PgSslOption = false | { rejectUnauthorized: true; ca?: string }; - -/** The sslmode values that verify the server, read the way the `pg` driver reads them. */ -const VERIFYING_MODES = new Set(['require', 'verify-ca', 'verify-full']); - -/** - * The `ssl` option for the pool, from the URL's `sslmode` and `sslrootcert`. - * - * MikroORM builds the pool from the URL's host, port, user, password and - * database alone and drops the rest of the query, so an `sslmode` in - * `TREK_DB_URL` would be ignored without this. `require`, `verify-ca` and - * `verify-full` all verify the server's certificate, as the `pg` driver itself - * treats them; `disable` turns SSL off, and no `sslmode` leaves the driver's - * default (no SSL). A mode that would connect without verifying (`prefer`, - * `allow`, `no-verify`) or that is misspelt refuses, rather than quietly - * connecting with less than was asked. `sslrootcert` names the CA file a host - * that signs its own certificates needs (Supabase's, say). - */ -export function sslOptionFor(url: string): PgSslOption | undefined { - const params = new URL(url).searchParams; - const mode = params.get('sslmode'); - const rootCert = params.get('sslrootcert'); - if (mode === null) { - if (rootCert !== null) throw new Error('TREK_DB_URL names an sslrootcert without sslmode=verify-full.'); - return undefined; - } - if (mode === 'disable') return false; - if (!VERIFYING_MODES.has(mode)) { - throw new Error( - `TREK_DB_URL has sslmode "${mode}"; use disable, require, verify-ca or verify-full (all but disable verify the server).`, - ); - } - if (rootCert === null) return { rejectUnauthorized: true }; - try { - return { rejectUnauthorized: true, ca: readFileSync(rootCert, 'utf8') }; - } catch (error) { - throw new Error(`TREK_DB_URL names sslrootcert ${rootCert}, which cannot be read.`, { cause: error }); - } -} +/** What `TREK_DB_URL` and `TREK_DB_POOL_MAX` resolve to, when they name a Postgres database. */ +type PostgresSettings = Extract; /** The advisory lock key every write transaction takes: "TREK" in ASCII. */ const TREK_WRITE_LOCK = 0x5452454b; @@ -67,12 +23,6 @@ const TREK_WRITE_LOCK = 0x5452454b; */ export class PostgresEngine implements DatabaseEngine { readonly id = 'postgres'; - /** - * The pool stays: ending it waits for every connection it has lent out, and - * one lent to a job while the restore ran was never handed back to the - * ending pool, so the restore never returned. - */ - readonly reopenSwapsConnection = false; constructor( private readonly settings: PostgresSettings, @@ -81,8 +31,14 @@ export class PostgresEngine implements DatabaseEngine { private onReopened: (() => Promise) | null = null; - /** Nothing of its own to open (the ORM connects the pool); keeps what a reopen runs. */ - open(onReopened: () => Promise): void { + /** + * Nothing of its own to open: the ORM connects the pool. Keeps what a reopen + * runs. A reopen replaces the database's contents, which the pool's own + * connections keep reaching, so the ORM is not pointed at anything new and + * the pool is never closed (ending a pool waits for every connection it has + * lent out, and one lent to a job during the restore would never come back). + */ + open(_orm: AnyOrm, onReopened: () => Promise): void { this.onReopened = onReopened; } diff --git a/server/src/db/engine/postgres/ssl.ts b/server/src/db/engine/postgres/ssl.ts new file mode 100644 index 000000000..5b96a0c50 --- /dev/null +++ b/server/src/db/engine/postgres/ssl.ts @@ -0,0 +1,42 @@ +import { readFileSync } from 'node:fs'; + +/** The sslmode values that verify the server, read the way the `pg` driver reads them. */ +const VERIFYING_MODES = new Set(['require', 'verify-ca', 'verify-full']); + +/** The `ssl` option of a `pg` pool, as `sslOptionFor` builds it. */ +type PgSslOption = false | { rejectUnauthorized: true; ca?: string }; + +/** + * The `ssl` option for the pool, from the URL's `sslmode` and `sslrootcert`. + * + * MikroORM builds the pool from the URL's host, port, user, password and + * database alone and drops the rest of the query, so an `sslmode` in + * `TREK_DB_URL` would be ignored without this. `require`, `verify-ca` and + * `verify-full` all verify the server's certificate, as the `pg` driver itself + * treats them; `disable` turns SSL off, and no `sslmode` leaves the driver's + * default (no SSL). A mode that would connect without verifying (`prefer`, + * `allow`, `no-verify`) or that is misspelt refuses, rather than quietly + * connecting with less than was asked. `sslrootcert` names the CA file a host + * that signs its own certificates needs (Supabase's, say). + */ +export function sslOptionFor(url: string): PgSslOption | undefined { + const params = new URL(url).searchParams; + const mode = params.get('sslmode'); + const rootCert = params.get('sslrootcert'); + if (mode === null) { + if (rootCert !== null) throw new Error('TREK_DB_URL names an sslrootcert without sslmode=verify-full.'); + return undefined; + } + if (mode === 'disable') return false; + if (!VERIFYING_MODES.has(mode)) { + throw new Error( + `TREK_DB_URL has sslmode "${mode}"; use disable, require, verify-ca or verify-full (all but disable verify the server).`, + ); + } + if (rootCert === null) return { rejectUnauthorized: true }; + try { + return { rejectUnauthorized: true, ca: readFileSync(rootCert, 'utf8') }; + } catch (error) { + throw new Error(`TREK_DB_URL names sslrootcert ${rootCert}, which cannot be read.`, { cause: error }); + } +} diff --git a/server/src/db/engine/sqlite/sqlite-engine.ts b/server/src/db/engine/sqlite/sqlite-engine.ts index ff46499f5..a57360876 100644 --- a/server/src/db/engine/sqlite/sqlite-engine.ts +++ b/server/src/db/engine/sqlite/sqlite-engine.ts @@ -14,7 +14,6 @@ import type { EntityManager } from '@mikro-orm/core'; */ export class SqliteEngine implements DatabaseEngine { readonly id = 'sqlite'; - readonly reopenSwapsConnection = true; describe(): string { return `SQLite file ${resolveDbPath()}`; @@ -24,11 +23,23 @@ export class SqliteEngine implements DatabaseEngine { return { driver: SharedSqliteDriver, dbName: resolveDbPath() }; } - open(onReopened: () => 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. + /** + * Opens the file (normally a no-op: the ORM's first connect inside + * NestFactory.create() already did, said here so the boot does not depend on + * that), and registers the reopen. A reopen swaps the file under the same + * handle, which Kysely caches, so the ORM's connection is closed and connected + * again first: the driver then reaches the new handle through + * `createKyselyDialect()`. The bound driver's `destroy()` is a no-op + * (`orm-driver.ts`), so the close never reaches the handle `db/database.ts` owns. + */ + open(orm: AnyOrm, onReopened: () => Promise): void { getRawConnection(); - registerReinitializeHook(onReopened); + registerReinitializeHook(async () => { + const connection = orm.em.getConnection(); + await connection.close(true); + await connection.connect(); + await onReopened(); + }); } close(): void { @@ -43,8 +54,8 @@ export class SqliteEngine implements DatabaseEngine { return runSchemaBootstrap(orm, options); } + /** A no-op: SQLite has one connection and one writer, so its transactions already run one at a time. */ serializeWrites(_em: EntityManager): Promise { - // One connection, one writer: SQLite serializes the transactions already. return Promise.resolve(); } } diff --git a/server/src/index.ts b/server/src/index.ts index c32f4206f..5accbe2bb 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -14,6 +14,7 @@ import { resolveDataPaths } from './app-config/data-paths'; // keeping the require after the restore keeps that true by construction. import type * as Bootstrap from './bootstrap'; import { resolveDbPath } from './db/db-path'; +import { postgresTarget } from './db/engine'; import { flushLogFileSync, logError } from './nest/audit/audit-log.logger'; import type { DatabaseLifecycle } from './nest/database/database-lifecycle.service'; import { ReadinessService } from './nest/health/readiness.service'; @@ -150,7 +151,7 @@ async function restoreBeforeTheDatabaseOpens() { dbFile: resolveDbPath(), dataDir: paths.dataDir, reclaimResetFile: paths.reclaimResetFile, - postgres: env.db.url ? { url: env.db.url, binDir: env.db.pgBinDir } : undefined, + postgres: postgresTarget(env.db) ?? undefined, }); } diff --git a/server/src/mikro-orm.config.ts b/server/src/mikro-orm.config.ts index 28dd200e4..b6afe6da2 100644 --- a/server/src/mikro-orm.config.ts +++ b/server/src/mikro-orm.config.ts @@ -1,8 +1,14 @@ import { buildOrmConfig, resolveEngine } from './db/engine'; /** - * The one MikroORM config, for the CLI and the Nest container alike: the - * engine `TREK_DB_URL` names (the SQLite file by default) and what every - * engine shares (`db/engine/index.ts`). + * The MikroORM config for the CLI and the Nest container alike: the engine + * `TREK_DB_URL` names (the SQLite file by default) and what every engine shares + * (`db/engine/index.ts`). + * + * Built at module level, not from `DATABASE_ENGINE`, because the MikroORM CLI + * loads this file without Nest. Only the engine's options come from it, and + * those are plain values with no state, so it and the container's engine agree + * on the database. The state an engine keeps (Postgres's reopen hook) exists + * only on the container's instance. */ export default buildOrmConfig(resolveEngine()); diff --git a/server/src/nest/backup/backup.impl.ts b/server/src/nest/backup/backup.impl.ts index 7b3dbd6d9..d01bad257 100644 --- a/server/src/nest/backup/backup.impl.ts +++ b/server/src/nest/backup/backup.impl.ts @@ -4,7 +4,9 @@ import { UserSessions } from '../../db/entities/UserSessions.entity'; import type { CarriedUserSessionRow } from '../../db/repositories/UserSessions.repository'; import { dbNow } from '../../db/types'; import { logError, logWarn } from '../audit/audit-log.logger'; -import type { DatabaseBackupStrategy } from '../database/database-backup.interface'; +import { describeError } from '../common/describe-error'; +import { DomainError } from '../common/domain-error'; +import type { DatabaseBackupRefusal, DatabaseBackupStrategy } from '../database/database-backup.interface'; import { invalidatePermissionsCache } from '../permissions/permissions-cache'; import { snapshotAllPluginDataDbs } from '../plugins/host/plugin-data.service'; import { pluginsCodeRoot, pluginsDataRoot } from '../plugins/paths'; @@ -36,8 +38,6 @@ export interface BackupDeps { database: DatabaseBackupStrategy; } -const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err)); - // The scratch and key directory. The database itself may live elsewhere // (TREK_DB_FILE); the database port knows where. const { dataDir, encryptionKeyFile, reclaimResetFile } = resolveDataPaths(); @@ -467,6 +467,12 @@ async function restoreCarriedSessions(rows: readonly CarriedUserSessionRow[]): P } } +/** The restore route's refusal for a `verify` that threw one. Any other failure is not a refusal, and keeps propagating. */ +function refusalOf(err: unknown): DatabaseBackupRefusal { + if (err instanceof DomainError) return { error: err.publicMessage, status: err.getStatus() }; + throw err; +} + export async function restoreFromZip({ storage, database }: BackupDeps, zipPath: string): Promise { const extractDir = path.join(dataDir, `restore-${Date.now()}`); let reinitFailed: unknown; @@ -474,7 +480,7 @@ export async function restoreFromZip({ storage, database }: BackupDeps, zipPath: const refused = await extractBackupArchive(zipPath, extractDir); if (refused) return { success: false, ...refused }; - const unreadable = await database.verify(extractDir); + const unreadable = await database.verify(extractDir).then(() => null, refusalOf); if (unreadable) { fs.rmSync(extractDir, { recursive: true, force: true }); return { success: false, ...unreadable }; diff --git a/server/src/nest/backup/database-backup.module.ts b/server/src/nest/backup/database-backup.module.ts index 2df975a1d..76f8bc466 100644 --- a/server/src/nest/backup/database-backup.module.ts +++ b/server/src/nest/backup/database-backup.module.ts @@ -1,5 +1,6 @@ -import { resolveEngine } from '../../db/engine'; +import type { DatabaseEngine } from '../../db/engine'; import { DATABASE_BACKUP, type DatabaseBackupStrategy } from '../database/database-backup.interface'; +import { DatabaseEngineModule, DATABASE_ENGINE } from '../database/database-engine.module'; import { DatabaseLifecycleModule } from '../database/database-lifecycle.module'; import { MaintenanceModule } from '../database/maintenance.module'; import { PostgresDatabaseBackup } from './postgres-database-backup'; @@ -8,7 +9,7 @@ import { Global, Module } from '@nestjs/common'; /** * Binds the database backup port (`DATABASE_BACKUP`) to the engine in use - * (`TREK_DB_URL`, read at boot through `resolveEngine()`). + * (`TREK_DB_URL`, the one `DATABASE_ENGINE` provides). * * Global because its consumers sit in two domains: the backup domain itself * and the admin domain (the demo reset job and the baseline route). Admin @@ -22,15 +23,18 @@ import { Global, Module } from '@nestjs/common'; */ @Global() @Module({ - imports: [DatabaseLifecycleModule, MaintenanceModule], + imports: [DatabaseEngineModule, DatabaseLifecycleModule, MaintenanceModule], providers: [ SqliteDatabaseBackup, PostgresDatabaseBackup, { provide: DATABASE_BACKUP, - useFactory: (sqlite: SqliteDatabaseBackup, postgres: PostgresDatabaseBackup): DatabaseBackupStrategy => - ({ sqlite, postgres })[resolveEngine().id], - inject: [SqliteDatabaseBackup, PostgresDatabaseBackup], + useFactory: ( + engine: DatabaseEngine, + sqlite: SqliteDatabaseBackup, + postgres: PostgresDatabaseBackup, + ): DatabaseBackupStrategy => ({ sqlite, postgres })[engine.id], + inject: [DATABASE_ENGINE, SqliteDatabaseBackup, PostgresDatabaseBackup], }, ], exports: [DATABASE_BACKUP], diff --git a/server/src/nest/backup/database-swap.ts b/server/src/nest/backup/database-swap.ts new file mode 100644 index 000000000..ee06e5613 --- /dev/null +++ b/server/src/nest/backup/database-swap.ts @@ -0,0 +1,58 @@ +import { logInfo, logWarn } from '../audit/audit-log.logger'; +import { describeError } from '../common/describe-error'; +import { DatabaseConnectionLostError } from '../database/database-backup.interface'; + +/** Whether a step ran to the end, and what it threw when it did not. */ +interface Outcome { + failed: boolean; + error: unknown; +} + +/** Runs `step` and records that it threw even when what it threw is undefined. */ +async function settle(step: () => void | Promise): Promise { + try { + await step(); + return { failed: false, error: null }; + } catch (error) { + return { failed: true, error }; + } +} + +/** + * The restore step both backup ports share. `swap` puts the new database in + * place; `reopen` always runs after it, so the process is never left without a + * connection. A failed swap rethrows its error, or a DatabaseConnectionLostError + * carrying both when the reopen failed too. A swap that landed resolves with the + * reopen's error, which the caller reports as "restart required". + */ +export async function replaceThenReopen( + swap: () => void | Promise, + reopen: () => Promise, +): Promise<{ reopenError: unknown }> { + const swapped = await settle(swap); + const reopened = await settle(reopen); + if (swapped.failed) { + if (reopened.failed) throw new DatabaseConnectionLostError(swapped.error, reopened.error); + throw swapped.error; + } + return { reopenError: reopened.failed ? reopened.error : null }; +} + +/** + * Keeps a copy of the database a restore is about to replace, at `target`. Best + * effort: a database broken enough to need the restore may not copy, and that + * must not block the recovery. Resolves with where the copy went, or null. + */ +export async function keepRestoreCopy( + target: string, + snapshot: (target: string) => Promise, +): Promise { + try { + await snapshot(target); + logInfo(`Restore: the replaced database was kept as ${target}`); + return target; + } catch (err) { + logWarn(`Restore: could not keep a copy of the current database (${describeError(err)})`); + return null; + } +} diff --git a/server/src/nest/backup/postgres-database-backup.ts b/server/src/nest/backup/postgres-database-backup.ts index 1549ed07a..3a2a05709 100644 --- a/server/src/nest/backup/postgres-database-backup.ts +++ b/server/src/nest/backup/postgres-database-backup.ts @@ -1,23 +1,20 @@ -import { readEnv } from '../../app-config'; -import { resolveEngine } from '../../db/engine'; +import type { DbSettings } from '../../app-config'; +import { postgresTarget, type DatabaseEngine } from '../../db/engine'; import { pgDump, pgDumpMigrations, pgRestore } from '../../db/engine/postgres/pg-tools'; import { unknownMigrations } from '../../db/known-migrations'; import { DataPathsService } from '../app-config/data-paths.service'; -import { logInfo, logWarn } from '../audit/audit-log.logger'; +import { logWarn } from '../audit/audit-log.logger'; +import { describeError } from '../common/describe-error'; import { DomainError } from '../common/domain-error'; -import { - DatabaseConnectionLostError, - type DatabaseBackupRefusal, - type DatabaseBackupStrategy, -} from '../database/database-backup.interface'; +import type { DatabaseBackupStrategy } from '../database/database-backup.interface'; +import { DATABASE_ENGINE, DATABASE_SETTINGS } from '../database/database-engine.module'; import { DatabaseLifecycle } from '../database/database-lifecycle.service'; -import { Injectable } from '@nestjs/common'; +import { keepRestoreCopy, replaceThenReopen } from './database-swap'; +import { Inject, Injectable } from '@nestjs/common'; import fs from 'fs'; import path from 'path'; -const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err)); - /** The name a Postgres database takes inside a backup archive: a pg_dump custom-format file. */ export const PG_ARCHIVE_ENTRY = 'travel.pgdump'; @@ -67,21 +64,25 @@ export async function checkPostgresDump(extractDir: string, binDir: string | und @Injectable() export class PostgresDatabaseBackup implements DatabaseBackupStrategy { readonly archiveEntry = PG_ARCHIVE_ENTRY; + /** The URL and the tools' directory; null on SQLite, where this provider is built but never called. */ + private readonly target: ReturnType; constructor( private readonly lifecycle: DatabaseLifecycle, private readonly paths: DataPathsService, - ) {} + @Inject(DATABASE_ENGINE) private readonly engine: DatabaseEngine, + @Inject(DATABASE_SETTINGS) settings: DbSettings, + ) { + this.target = postgresTarget(settings); + } - /** The URL and the tools' directory, read when used: on SQLite this provider is built but never called. */ - private get settings(): { url: string; binDir: string | undefined } { - const { url, pgBinDir } = readEnv().db; - if (!url) throw new Error('The Postgres backup was used without TREK_DB_URL.'); - return { url, binDir: pgBinDir }; + private get connection(): { url: string; binDir: string | undefined } { + if (!this.target) throw new Error('The Postgres backup was used without TREK_DB_URL.'); + return this.target; } location(): string { - return resolveEngine().describe(); + return this.engine.describe(); } /** A Postgres database is always there to copy; a missing pg_dump fails the snapshot, loudly. */ @@ -95,51 +96,32 @@ export class PostgresDatabaseBackup implements DatabaseBackupStrategy { /** pg_dump writes beside `target` and renames into place: a failed dump leaves nothing there. */ async snapshot(target: string): Promise { - const { url, binDir } = this.settings; + const { url, binDir } = this.connection; await pgDump(url, target, binDir); } - /** Refuses (a DomainError, 400) an archive this database cannot take, before anything is replaced. */ - async verify(extractDir: string): Promise { - const refusal = await checkPostgresDump(extractDir, this.settings.binDir); + /** Throws a DomainError (400) with the refusal when the archive cannot be restored here, before anything is replaced. */ + async verify(extractDir: string): Promise { + const refusal = await checkPostgresDump(extractDir, this.connection.binDir); if (refusal) throw new DomainError(400, refusal); - return null; } /** A dump of the database a restore is about to replace, in the data directory. */ - async keepCopyBeforeRestore(): Promise { + keepCopyBeforeRestore(): Promise { const target = path.join(this.paths.dataDir, `pre-restore-${Date.now()}.pgdump`); - try { - await this.snapshot(target); - logInfo(`Restore: the replaced database was kept as ${target}`); - return target; - } catch (err) { - logWarn(`Restore: could not keep a copy of the current database (${describeError(err)})`); - return null; - } + return keepRestoreCopy(target, (copy) => this.snapshot(copy)); } - async replace(source: string): Promise<{ reopenError: unknown }> { - let swapError: unknown = null; - try { - const { url, binDir } = this.settings; - await pgRestore(url, source, binDir); - } catch (err) { - swapError = err ?? new Error('pg_restore failed'); - } - - // As on SQLite: the reopen always runs, so the ORM never keeps a stale pool, - // and both errors go up together when the restore failed as well. - let reopenError: unknown = null; - try { - await this.lifecycle.reopen(); - } catch (err) { - reopenError = err; - } - if (swapError) { - if (reopenError) throw new DatabaseConnectionLostError(swapError, reopenError); - throw swapError; - } - return { reopenError }; + replace(source: string): Promise<{ reopenError: unknown }> { + return replaceThenReopen( + async () => { + const { url, binDir } = this.connection; + // A rejection with no reason still has to fail the swap, so it is never swallowed. + await pgRestore(url, source, binDir).catch((err: unknown) => { + throw err ?? new Error('pg_restore failed'); + }); + }, + () => this.lifecycle.reopen(), + ); } } diff --git a/server/src/nest/backup/sqlite-database-backup.ts b/server/src/nest/backup/sqlite-database-backup.ts index c373ad3b7..970491cf2 100644 --- a/server/src/nest/backup/sqlite-database-backup.ts +++ b/server/src/nest/backup/sqlite-database-backup.ts @@ -1,29 +1,22 @@ import { SqliteMaintenance } from '../../db/engine/sqlite/sqlite-maintenance'; -import { logInfo, logWarn } from '../audit/audit-log.logger'; -import { - DatabaseConnectionLostError, - type DatabaseBackupRefusal, - type DatabaseBackupStrategy, -} from '../database/database-backup.interface'; +import { DomainError } from '../common/domain-error'; +import type { DatabaseBackupStrategy } from '../database/database-backup.interface'; import { DatabaseLifecycle } from '../database/database-lifecycle.service'; import { checkBackupDatabase } from './backup-archive'; +import { keepRestoreCopy, replaceThenReopen } from './database-swap'; import { Injectable } from '@nestjs/common'; import fs from 'fs'; import path from 'path'; -const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err)); - /** - * The backup port for the one engine TREK runs on: a SQLite file. + * The backup port on a SQLite file, the engine TREK runs on by default. * - * Every step is what `backup.impl.ts` and `demo/demo-reset.ts` did inline - * before the port existed. A snapshot is `VACUUM INTO`, which takes a - * consistent copy even under concurrent writers. A restore closes the - * connection, swaps the file by copy and rename (a crash mid-swap leaves the - * old or the new file, never neither), drops the old `-wal`/`-shm` sidecars, - * and reopens through `DatabaseLifecycle`, which migrates the restored file - * forward. + * A snapshot is `VACUUM INTO`, which takes a consistent copy even under + * concurrent writers. A restore closes the connection, swaps the file by copy + * and rename (a crash mid-swap leaves the old or the new file, never neither), + * drops the old `-wal`/`-shm` sidecars, and reopens through `DatabaseLifecycle`, + * which migrates the restored file forward. * * The file is always `DatabaseLifecycle.file`, the path the connection was * opened on, so `TREK_DB_FILE` moves the backup, the restore and the demo reset @@ -62,66 +55,42 @@ export class SqliteDatabaseBackup implements DatabaseBackupStrategy { } } - async verify(extractDir: string): Promise { - return checkBackupDatabase(extractDir); + /** Throws a DomainError carrying the refusal when the archive's database cannot be restored here. */ + async verify(extractDir: string): Promise { + const refusal = checkBackupDatabase(extractDir); + if (refusal) throw new DomainError(refusal.status, refusal.error); } - /** - * A copy of the database a restore is about to replace, next to it. The swap - * deletes the current file, and a restore of the wrong archive used to leave - * nothing to go back to. - */ - async keepCopyBeforeRestore(): Promise { + /** A copy of the database a restore is about to replace, next to it: the swap deletes the current file. */ + keepCopyBeforeRestore(): Promise { const target = path.join(path.dirname(this.lifecycle.file), `pre-restore-${Date.now()}.db`); - try { - await this.snapshot(target); - logInfo(`Restore: the replaced database was kept as ${target}`); - return target; - } catch (err) { - logWarn(`Restore: could not keep a copy of the current database (${describeError(err)})`); - return null; - } + return keepRestoreCopy(target, (copy) => this.snapshot(copy)); } async replace(source: string): Promise<{ reopenError: unknown }> { const dest = this.lifecycle.file; this.lifecycle.close(); - let swapFailed = false; - let swapError: unknown = null; - try { - // Copy to a temp file on the SAME filesystem, drop the old sidecars (they - // belong to the database being replaced and would corrupt the new one), - // then rename into place. The rename is atomic. - const tmp = dest + '.restore-tmp'; - fs.copyFileSync(source, tmp); - for (const ext of ['-wal', '-shm']) { - try { - fs.unlinkSync(dest + ext); - } catch { - /* no sidecar to drop */ - } - } - fs.renameSync(tmp, dest); - } catch (err) { - swapFailed = true; - swapError = err; - } - - // Reopening must always run, even when the swap threw, so the process is - // never left without a connection. After a swap that landed, a reopen - // failure is reported, not thrown: the files are in place and the caller - // has to say "restart". After a swap that failed, both errors go up - // together, because the swap's alone would hide that the connection is gone. - let reopenError: unknown = null; - try { - await this.lifecycle.reopen(); - } catch (err) { - reopenError = err; - } - if (swapFailed) { - if (reopenError) throw new DatabaseConnectionLostError(swapError, reopenError); - throw swapError; - } - return { reopenError }; + return replaceThenReopen( + () => swapFile(source, dest), + () => this.lifecycle.reopen(), + ); } } + +/** + * Puts `source` at `dest` by copying it to a temp file on the same filesystem + * and renaming it into place. The old sidecars go first: they belong to the + * database being replaced and would corrupt the new one. + */ +function swapFile(source: string, dest: string): void { + const tmp = dest + '.restore-tmp'; + fs.copyFileSync(source, tmp); + for (const ext of ['-wal', '-shm']) { + try { + fs.unlinkSync(dest + ext); + } catch { + /* no sidecar to drop */ + } + } + fs.renameSync(tmp, dest); +} diff --git a/server/src/nest/common/describe-error.ts b/server/src/nest/common/describe-error.ts new file mode 100644 index 000000000..743668591 --- /dev/null +++ b/server/src/nest/common/describe-error.ts @@ -0,0 +1,2 @@ +/** The message of an error, or the value itself as text when it is not an Error (a thrown string, say). */ +export const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err)); diff --git a/server/src/nest/database/database-backup.interface.ts b/server/src/nest/database/database-backup.interface.ts index ddf71edf7..7e9a83e5d 100644 --- a/server/src/nest/database/database-backup.interface.ts +++ b/server/src/nest/database/database-backup.interface.ts @@ -1,14 +1,15 @@ +import { describeError } from '../common/describe-error'; + /** * How a backup, a restore and the demo reset get at the core database, without * knowing which engine holds it. * * The admin backup (`nest/backup/backup.impl.ts`), its restore, and the demo * baseline save and hourly reset (`demo/demo-reset.ts`) all go through this - * port. The only implementation today is `SqliteDatabaseBackup` - * (`nest/backup/`), which does what those paths always did: `VACUUM INTO` for a - * consistent copy of the file, a file swap under a closed connection for a - * restore. An engine that is not one local file would plug in here with a - * logical dump and load instead. + * port. Each engine has an adapter in `nest/backup/`, chosen by the engine in + * use: SQLite copies the file (`VACUUM INTO`) and swaps it under a closed + * connection; Postgres dumps and restores it with pg_dump and pg_restore. The + * two adapters answer every method the same way, so the callers do not branch. * * Lives in the shared database kernel, not in the backup domain, so the admin * domain (the demo reset job, the baseline route) can inject it without @@ -43,10 +44,10 @@ export interface DatabaseBackupStrategy { /** * Checks an unpacked archive's database before anything is replaced. Resolves - * with null when it can be restored, or the refusal in the restore route's - * words (an engine may throw a DomainError with it instead). + * when it can be restored; otherwise throws a DomainError (status 400) that + * carries the refusal in the restore route's words. */ - verify(extractDir: string): Promise; + verify(extractDir: string): Promise; /** * Keeps a copy of the database a restore is about to replace. Best effort: a @@ -68,7 +69,7 @@ export interface DatabaseBackupStrategy { replace(source: string): Promise<{ reopenError: unknown }>; } -/** A refusal in the restore route's own words and status. */ +/** A refusal in the restore route's own words and status, as `verify` reports it. */ export interface DatabaseBackupRefusal { error: string; status: number; @@ -77,8 +78,6 @@ export interface DatabaseBackupRefusal { /** Injection token for the active `DatabaseBackupStrategy`. */ export const DATABASE_BACKUP = Symbol('DATABASE_BACKUP'); -const describeError = (err: unknown): string => (err instanceof Error ? err.message : String(err)); - /** * Thrown by `replace` when the swap failed and the reopen that always follows * failed too. The process is left without a database connection until a diff --git a/server/src/nest/database/database-engine.module.ts b/server/src/nest/database/database-engine.module.ts new file mode 100644 index 000000000..82fd7fb5b --- /dev/null +++ b/server/src/nest/database/database-engine.module.ts @@ -0,0 +1,30 @@ +import { readEnv, type DbSettings } from '../../app-config'; +import { resolveEngine, type DatabaseEngine } from '../../db/engine'; +import { Module } from '@nestjs/common'; + +/** `readEnv().db`, read once per container: what decides which engine runs and its settings. */ +export const DATABASE_SETTINGS = Symbol('DATABASE_SETTINGS'); + +/** + * The one `DatabaseEngine` of this process. Every injector (the lifecycle, the + * unit of work, the backup port and its adapters) gets this instance, so the + * state an engine keeps (Postgres's reopen hook) is the same wherever it is used. + */ +export const DATABASE_ENGINE = Symbol('DATABASE_ENGINE'); + +/** + * Provides the engine and its settings. Not global: each module that needs + * the engine imports it, and Nest keeps one instance per container. + */ +@Module({ + providers: [ + { provide: DATABASE_SETTINGS, useFactory: (): DbSettings => readEnv().db }, + { + provide: DATABASE_ENGINE, + useFactory: (db: DbSettings): DatabaseEngine => resolveEngine(db), + inject: [DATABASE_SETTINGS], + }, + ], + exports: [DATABASE_SETTINGS, DATABASE_ENGINE], +}) +export class DatabaseEngineModule {} diff --git a/server/src/nest/database/database-lifecycle.module.ts b/server/src/nest/database/database-lifecycle.module.ts index e56c755b2..70434f7c5 100644 --- a/server/src/nest/database/database-lifecycle.module.ts +++ b/server/src/nest/database/database-lifecycle.module.ts @@ -1,3 +1,4 @@ +import { DatabaseEngineModule } from './database-engine.module'; import { DatabaseLifecycle } from './database-lifecycle.service'; import { Module } from '@nestjs/common'; @@ -6,5 +7,5 @@ import { Module } from '@nestjs/common'; * 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] }) +@Module({ imports: [DatabaseEngineModule], providers: [DatabaseLifecycle], exports: [DatabaseLifecycle] }) export class DatabaseLifecycleModule {} diff --git a/server/src/nest/database/database-lifecycle.service.ts b/server/src/nest/database/database-lifecycle.service.ts index a9a24dfa5..ddbf663b0 100644 --- a/server/src/nest/database/database-lifecycle.service.ts +++ b/server/src/nest/database/database-lifecycle.service.ts @@ -1,7 +1,8 @@ import { resolveDbPath } from '../../db/db-path'; -import { resolveEngine } from '../../db/engine'; +import type { DatabaseEngine } from '../../db/engine'; +import { DATABASE_ENGINE } from './database-engine.module'; import { MikroORM } from '@mikro-orm/core'; -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; /** * The one owner of the core database connection's lifecycle: open, close and @@ -12,19 +13,21 @@ import { Injectable } from '@nestjs/common'; * replace that module wholesale with `vi.mock`. What moved here is every * decision about WHEN it opens and closes: `buildApp()` opens through this, * a restore and the demo reset close and reopen through this (via the backup - * port), and the shutdown closes through this. Nothing opens the database at - * import any more; the first connect does, and this provider makes it explicit. + * port), and the shutdown closes through this. The ORM's first connect is what + * actually opens the file; this provider makes that step explicit. * - * What differs by engine (a file to open or not, how a database reaches the - * schema) is the engine's (`db/engine/`), chosen from `TREK_DB_URL`: this + * What differs by engine (a file to open or not, how a reopen reaches the ORM) + * is the engine's (`db/engine/`), the one `DATABASE_ENGINE` provides: this * provider decides when, the engine decides how. */ @Injectable() export class DatabaseLifecycle { private readonly reopenListeners: Array<() => Promise> = []; - private readonly engine = resolveEngine(); - constructor(private readonly orm: MikroORM) {} + constructor( + private readonly orm: MikroORM, + @Inject(DATABASE_ENGINE) private readonly engine: DatabaseEngine, + ) {} /** * The database file this process runs on, resolved the one way every caller @@ -36,14 +39,14 @@ export class DatabaseLifecycle { } /** - * Opens the connection, binds the ORM to its later swaps and brings the schema - * to head (on SQLite: legacy baseline, migrations, the deferred index, + * Opens the connection, points the engine's reopen at the ORM and brings the + * schema to head (on SQLite: legacy baseline, migrations, the deferred index, * seeders, demo seed, in that order, see `db/orm.ts`; on Postgres: the * baseline or the migrations after it, then the same seeding). Called once * by `buildApp()`, before anything reads the database. */ async open(): Promise { - this.engine.open(() => this.rebindOrm()); + this.engine.open(this.orm, () => this.afterReopen()); await this.engine.schemaBootstrap(this.orm, { snapshot: true }); } @@ -53,9 +56,9 @@ export class DatabaseLifecycle { } /** - * 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). + * Reopens the connection after the database was replaced. The engine points + * the ORM at the new database first; then `afterReopen` migrates it forward + * and runs the listeners. */ async reopen(): Promise { await this.engine.reopen(); @@ -76,22 +79,11 @@ export class DatabaseLifecycle { } /** - * 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. An - * engine whose reopen keeps the connection (Postgres) skips that and only - * migrates. + * What follows a reopen once the engine has pointed the ORM at the reopened + * database. No pre-migrate snapshot: the file was just unpacked from an + * archive that is still there to go back to. */ - private async rebindOrm(): Promise { - if (this.engine.reopenSwapsConnection) { - 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. + private async afterReopen(): Promise { await this.engine.schemaBootstrap(this.orm, { snapshot: false }); for (const listener of this.reopenListeners) await listener(); } diff --git a/server/src/nest/database/orm.module.ts b/server/src/nest/database/orm.module.ts index f423d967b..6afb8de72 100644 --- a/server/src/nest/database/orm.module.ts +++ b/server/src/nest/database/orm.module.ts @@ -1,3 +1,4 @@ +import { DatabaseEngineModule } from './database-engine.module'; import { UnitOfWork } from './unit-of-work'; import { Global, Module } from '@nestjs/common'; @@ -10,10 +11,11 @@ import { Global, Module } from '@nestjs/common'; * (`Test.createTestingModule({ imports: [DatabaseModule, ...] })`) * with no ORM in the container: a provider there that injects the core * `EntityManager` fails every one of them at `compile()`, because Nest - * instantiates providers eagerly. Nothing is imported here — the - * `EntityManager` token comes from `MikroOrmModule.forRoot`, whose - * `MikroOrmCoreModule` is itself `@Global()`. + * instantiates providers eagerly. The `EntityManager` token comes from + * `MikroOrmModule.forRoot`, whose + * `MikroOrmCoreModule` is itself `@Global()`. It imports `DatabaseEngineModule`, + * the engine the unit of work serializes its writes through. */ @Global() -@Module({ providers: [UnitOfWork], exports: [UnitOfWork] }) +@Module({ imports: [DatabaseEngineModule], providers: [UnitOfWork], exports: [UnitOfWork] }) export class OrmModule {} diff --git a/server/src/nest/database/unit-of-work.ts b/server/src/nest/database/unit-of-work.ts index 7b4002e60..1e92efece 100644 --- a/server/src/nest/database/unit-of-work.ts +++ b/server/src/nest/database/unit-of-work.ts @@ -1,6 +1,7 @@ -import { resolveEngine } from '../../db/engine'; +import type { DatabaseEngine } from '../../db/engine'; +import { DATABASE_ENGINE } from './database-engine.module'; import { EntityManager } from '@mikro-orm/core'; -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; /** * The one way a service opens a transaction. @@ -26,9 +27,10 @@ import { Injectable } from '@nestjs/common'; */ @Injectable() export class UnitOfWork { - private readonly engine = resolveEngine(); - - constructor(private readonly em: EntityManager) {} + constructor( + private readonly em: EntityManager, + @Inject(DATABASE_ENGINE) private readonly engine: DatabaseEngine, + ) {} transactional(fn: () => Promise): Promise { return this.em.transactional(async (em) => { diff --git a/server/tests/helpers/test-addons.ts b/server/tests/helpers/test-addons.ts index ab100e84a..2d3b0d810 100644 --- a/server/tests/helpers/test-addons.ts +++ b/server/tests/helpers/test-addons.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine'; import { Addons } from '../../src/db/entities/Addons.entity'; import { AppSettings } from '../../src/db/entities/AppSettings.entity'; import { PhotoProviderFields } from '../../src/db/entities/PhotoProviderFields.entity'; @@ -42,6 +43,6 @@ export async function createTestAddonsService(db: Database.Database): Promise // which is exactly what no-misused-promises/checksConditionals rejects. const existing = perHandle.get(db); if (existing !== undefined) return existing; - const pending = sharedTestOrm(db).then((t) => new UnitOfWork(t.em)); + const pending = sharedTestOrm(db).then((t) => new UnitOfWork(t.em, new SqliteEngine())); perHandle.set(db, pending); return pending; } diff --git a/server/tests/integration/mcp.test.ts b/server/tests/integration/mcp.test.ts index 13ed28220..c4cbc1180 100644 --- a/server/tests/integration/mcp.test.ts +++ b/server/tests/integration/mcp.test.ts @@ -8,6 +8,7 @@ import { getMcpSafeUrl } from '../../src/app-config'; import { buildApp } from '../../src/bootstrap'; import { db as testDb } from '../../src/db/database'; +import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine'; import { AuditLog } from '../../src/db/entities/AuditLog.entity'; import { McpTokens } from '../../src/db/entities/McpTokens.entity'; import { OauthClients } from '../../src/db/entities/OauthClients.entity'; @@ -85,7 +86,7 @@ beforeAll(async () => { t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)), - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), nestApp.get(CimdClientResolver), ); }); diff --git a/server/tests/integration/oauth-authorize.test.ts b/server/tests/integration/oauth-authorize.test.ts index 6fc2b2a00..364181b1d 100644 --- a/server/tests/integration/oauth-authorize.test.ts +++ b/server/tests/integration/oauth-authorize.test.ts @@ -10,6 +10,7 @@ */ import { buildApp } from '../../src/bootstrap'; import { db as testDb } from '../../src/db/database'; +import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine'; import { Addons } from '../../src/db/entities/Addons.entity'; import { AuditLog } from '../../src/db/entities/AuditLog.entity'; import { OauthClients } from '../../src/db/entities/OauthClients.entity'; @@ -96,7 +97,7 @@ beforeAll(async () => { t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)), - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), nestApp.get(CimdClientResolver), ); }); diff --git a/server/tests/integration/oauth.test.ts b/server/tests/integration/oauth.test.ts index 50c8dc356..5ea749278 100644 --- a/server/tests/integration/oauth.test.ts +++ b/server/tests/integration/oauth.test.ts @@ -5,6 +5,7 @@ */ import { buildApp } from '../../src/bootstrap'; import { db as testDb } from '../../src/db/database'; +import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine'; import { Addons } from '../../src/db/entities/Addons.entity'; import { AuditLog } from '../../src/db/entities/AuditLog.entity'; import { OauthClients } from '../../src/db/entities/OauthClients.entity'; @@ -97,7 +98,7 @@ beforeAll(async () => { t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)), - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), nestApp.get(CimdClientResolver), ); }); diff --git a/server/tests/integration/plugins/boot-registry-order.test.ts b/server/tests/integration/plugins/boot-registry-order.test.ts index e35dc8207..cead7a413 100644 --- a/server/tests/integration/plugins/boot-registry-order.test.ts +++ b/server/tests/integration/plugins/boot-registry-order.test.ts @@ -17,6 +17,7 @@ * asserts the plugin still comes up clean. */ import { db as testDb } from '../../../src/db/database'; +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { Addons } from '../../../src/db/entities/Addons.entity'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { AuditLog } from '../../../src/db/entities/AuditLog.entity'; @@ -173,7 +174,7 @@ describe('plugin boot vs registry scan ordering', () => { (t as TestOrm).repo(NotificationChannelPreferences), // Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead // of `registry?`/`hostFactory?`, matching the real constructor. - new UnitOfWork((t as TestOrm).em), + new UnitOfWork((t as TestOrm).em, new SqliteEngine()), undefined, hostFactory, t?.orm, @@ -264,7 +265,7 @@ describe('plugin boot vs registry scan ordering', () => { t2.repo(PhotoProviderFields), t2.repo(AppSettings), t2.repo(Users), - new UnitOfWork(t2.em), + new UnitOfWork(t2.em, new SqliteEngine()), ); const auditLogRepo2 = t2.repo(AuditLog); const usersRepo2 = t2.repo(Users); @@ -293,7 +294,7 @@ describe('plugin boot vs registry scan ordering', () => { t2.repo(PluginCapabilityAudit), t2.repo(Settings), t2.repo(NotificationChannelPreferences), - new UnitOfWork(t2.em), + new UnitOfWork(t2.em, new SqliteEngine()), undefined, hostFactory2, t2.orm, diff --git a/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts b/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts index 94c5c5e5e..c1b562773 100644 --- a/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts +++ b/server/tests/integration/plugins/plugin-runtime.task2-conversion.test.ts @@ -13,6 +13,7 @@ * so every one of the 14 repositories this task injects reads/writes real columns, * not a hand-slimmed fixture table. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { AuditLog } from '../../../src/db/entities/AuditLog.entity'; import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity'; import { PluginActions } from '../../../src/db/entities/PluginActions.entity'; @@ -76,7 +77,7 @@ async function buildRuntime(registrar?: CronRegistrarService): Promise extract -> validate -> * move -> register pipeline (with the network download mocked). */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { PluginActions } from '../../../src/db/entities/PluginActions.entity'; import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity'; import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity'; @@ -148,7 +149,7 @@ beforeEach(() => { t.repo(PluginActions), t.repo(PluginSettingsFields), t.repo(PluginErrorLog), - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), ); }); afterEach(() => { diff --git a/server/tests/unit/app-config/derive.test.ts b/server/tests/unit/app-config/derive.test.ts index 67a77a6a2..cc6e632a8 100644 --- a/server/tests/unit/app-config/derive.test.ts +++ b/server/tests/unit/app-config/derive.test.ts @@ -373,8 +373,8 @@ describe('deriveDb: the pre-migration snapshot', () => { describe('deriveDb: the engine', () => { it('DB-ENGINE-001: SQLite unless TREK_DB_URL names a database', () => { - expect(deriveDb({})).toMatchObject({ engine: 'sqlite', url: undefined }); - expect(deriveDb({ TREK_DB_URL: ' ' })).toMatchObject({ engine: 'sqlite', url: undefined }); + expect(deriveDb({})).toMatchObject({ engine: 'sqlite' }); + expect(deriveDb({ TREK_DB_URL: ' ' })).toMatchObject({ engine: 'sqlite' }); expect(deriveDb({ TREK_DB_URL: ' postgres://u:p@db:5432/trek ' })).toMatchObject({ engine: 'postgres', url: 'postgres://u:p@db:5432/trek', @@ -382,10 +382,20 @@ describe('deriveDb: the engine', () => { }); it('DB-ENGINE-002: a pool of ten connections unless told otherwise, from 1 to 100', () => { - expect(deriveDb({}).poolMax).toBe(10); - expect(deriveDb({ TREK_DB_POOL_MAX: '25' }).poolMax).toBe(25); - expect(deriveDb({ TREK_DB_POOL_MAX: '0' }).poolMax).toBe(10); - expect(deriveDb({ TREK_DB_POOL_MAX: '500' }).poolMax).toBe(100); + const poolMaxOf = (raw: Record) => { + const db = deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek', ...raw }); + return db.engine === 'postgres' ? db.poolMax : null; + }; + expect(deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek' })).toMatchObject({ engine: 'postgres' }); + expect(poolMaxOf({})).toBe(10); + expect(poolMaxOf({ TREK_DB_POOL_MAX: '25' })).toBe(25); + expect(poolMaxOf({ TREK_DB_POOL_MAX: '0' })).toBe(10); + expect(poolMaxOf({ TREK_DB_POOL_MAX: '500' })).toBe(100); + }); + + it('DB-ENGINE-003: no pool size on SQLite, which has no pool', () => { + expect(deriveDb({ TREK_DB_POOL_MAX: '25' })).toMatchObject({ engine: 'sqlite' }); + expect(deriveDb({})).not.toHaveProperty('poolMax'); }); }); diff --git a/server/tests/unit/db/connection.test.ts b/server/tests/unit/db/connection.test.ts index b179f2397..ec5f3bf84 100644 --- a/server/tests/unit/db/connection.test.ts +++ b/server/tests/unit/db/connection.test.ts @@ -1,4 +1,5 @@ import { openDatabase } from '../../../src/db/connection'; +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { Places } from '../../../src/db/entities/Places.entity'; import { Trips } from '../../../src/db/entities/Trips.entity'; import { UnitOfWork } from '../../../src/nest/database/unit-of-work'; @@ -147,7 +148,19 @@ async function deleteTripAndWatchTempFiles(db: Database.Database): Promise<{ const none = undefined as never; try { - const trips = new TripsService(none, none, none, none, none, none, none, none, new UnitOfWork(t.em), t.em, none); + const trips = new TripsService( + none, + none, + none, + none, + none, + none, + none, + none, + new UnitOfWork(t.em, new SqliteEngine()), + t.em, + none, + ); await trips.remove(trip.id, user.id, 'user'); return { tempFiles: [...seen], diff --git a/server/tests/unit/db/engine/engine.test.ts b/server/tests/unit/db/engine/engine.test.ts index 52aa4f568..0b002b733 100644 --- a/server/tests/unit/db/engine/engine.test.ts +++ b/server/tests/unit/db/engine/engine.test.ts @@ -1,8 +1,9 @@ import { deriveDb } from '../../../../src/app-config/derive'; import { buildOrmConfig, resolveEngine } from '../../../../src/db/engine'; import { TrekPostgreSqlDriver } from '../../../../src/db/engine/postgres/postgres-driver'; -import { sslOptionFor } from '../../../../src/db/engine/postgres/postgres-engine'; +import { sslOptionFor } from '../../../../src/db/engine/postgres/ssl'; import { redactUrl } from '../../../../src/db/engine/redact-url'; +import type { AnyOrm } from '../../../../src/db/orm'; import { SharedSqliteDriver } from '../../../../src/db/orm-driver'; import type { EntityManager } from '@mikro-orm/core'; @@ -112,7 +113,8 @@ describe('buildOrmConfig', () => { const engine = resolveEngine(deriveDb({ TREK_DB_URL: PG_URL })); await expect(engine.reopen()).rejects.toThrow(/reopened before it was opened/); const onReopened = vi.fn(async () => undefined); - expect(() => engine.open(onReopened)).not.toThrow(); + const orm = {} as unknown as AnyOrm; + expect(() => engine.open(orm, onReopened)).not.toThrow(); expect(() => engine.close()).not.toThrow(); await engine.reopen(); expect(onReopened).toHaveBeenCalledTimes(1); diff --git a/server/tests/unit/db/pre-migrate-snapshot.test.ts b/server/tests/unit/db/pre-migrate-snapshot.test.ts index 500e36c92..a7fbf4fb2 100644 --- a/server/tests/unit/db/pre-migrate-snapshot.test.ts +++ b/server/tests/unit/db/pre-migrate-snapshot.test.ts @@ -44,7 +44,7 @@ const migrator = () => orm.config.getExtension('@mikro-orm/migrator') as Migrato function envWith(db: Partial, isTest = false): () => AppEnv { return () => { const env = readEnv(); - return { ...env, app: { ...env.app, isTest }, db: { ...env.db, ...db } }; + return { ...env, app: { ...env.app, isTest }, db: { ...env.db, ...db } as AppEnv['db'] }; }; } diff --git a/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts b/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts index 54b03ef32..bef91c610 100644 --- a/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts +++ b/server/tests/unit/db/repositories/AssignmentParticipants.repository.test.ts @@ -6,6 +6,7 @@ * username)` — unlike `AssignmentsService.getParticipants` (AS15) — see the * repository's own docstring. */ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { AssignmentParticipants } from '../../../../src/db/entities/AssignmentParticipants.entity'; import { Users } from '../../../../src/db/entities/Users.entity'; import type { AssignmentParticipantsRepository } from '../../../../src/db/repositories/AssignmentParticipants.repository'; @@ -27,7 +28,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); participants = t.repo(AssignmentParticipants); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/DayAssignments.repository.test.ts b/server/tests/unit/db/repositories/DayAssignments.repository.test.ts index d443b39a6..a3d68ab29 100644 --- a/server/tests/unit/db/repositories/DayAssignments.repository.test.ts +++ b/server/tests/unit/db/repositories/DayAssignments.repository.test.ts @@ -3,6 +3,7 @@ * assignment-with-place projection contract (built here, Task 3 consumes it * unchanged for AS1/AS3). */ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { DayAccommodations } from '../../../../src/db/entities/DayAccommodations.entity'; import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity'; import { Places } from '../../../../src/db/entities/Places.entity'; @@ -35,7 +36,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); assignments = t.repo(DayAssignments); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/InviteTokens.repository.test.ts b/server/tests/unit/db/repositories/InviteTokens.repository.test.ts index d8c28307b..2e4b92cbf 100644 --- a/server/tests/unit/db/repositories/InviteTokens.repository.test.ts +++ b/server/tests/unit/db/repositories/InviteTokens.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { InviteTokens } from '../../../../src/db/entities/InviteTokens.entity'; import type { InviteTokensRepository } from '../../../../src/db/repositories/InviteTokens.repository'; import { withRequestContext } from '../../../../src/nest/database/request-context'; @@ -227,7 +228,7 @@ describe('InviteTokensRepository', () => { it('INVREPO-017: incrementUsedCount inside uow.transactional is not discarded by a stale entity read earlier under a different projection', async () => { const { user: admin } = createUser(testDb, { username: 'invrepo-017-admin' }); const invite = createInviteToken(testDb, { token: 'invrepo-017', max_uses: 5, created_by: admin.id }); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await withRequestContext(t.orm, async () => { // Projection A — wide, full row, carries used_count. diff --git a/server/tests/unit/db/repositories/McpTokens.repository.test.ts b/server/tests/unit/db/repositories/McpTokens.repository.test.ts index df1d3b2a8..69e6c746e 100644 --- a/server/tests/unit/db/repositories/McpTokens.repository.test.ts +++ b/server/tests/unit/db/repositories/McpTokens.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { McpTokens } from '../../../../src/db/entities/McpTokens.entity'; import type { McpTokensRepository } from '../../../../src/db/repositories/McpTokens.repository'; import { UnitOfWork } from '../../../../src/nest/database/unit-of-work'; @@ -449,7 +450,7 @@ describe('McpTokensRepository', () => { // read, nativeUpdate, flush, still correct — is still worth pinning. const { user } = createUser(testDb); const created = createMcpToken(testDb, user.id, { name: 'stays-original' }); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await uow.transactional(async () => { const basic = await tokens.findBasic(created.id); @@ -516,7 +517,7 @@ describe('McpTokensRepository', () => { it('MCPTOKREPO-023: projection A then projection B then a nativeUpdate on an A-only column, inside uow.transactional — the nativeUpdate survives the closing flush', async () => { const { user } = createUser(testDb); const created = createMcpToken(testDb, user.id, { name: 'stays-original', scope_mode: 'all' }); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await uow.transactional(async () => { // Projection A: selects `scope_mode` among its columns. diff --git a/server/tests/unit/db/repositories/OauthClients.repository.test.ts b/server/tests/unit/db/repositories/OauthClients.repository.test.ts index 98accbac1..82e495b26 100644 --- a/server/tests/unit/db/repositories/OauthClients.repository.test.ts +++ b/server/tests/unit/db/repositories/OauthClients.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { OauthClients } from '../../../../src/db/entities/OauthClients.entity'; import { OauthTokens } from '../../../../src/db/entities/OauthTokens.entity'; import type { OauthClientsRepository } from '../../../../src/db/repositories/OauthClients.repository'; @@ -20,7 +21,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); clients = t.repo(OauthClients); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(async () => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/OauthConsents.repository.test.ts b/server/tests/unit/db/repositories/OauthConsents.repository.test.ts index 1d54c197b..792574db2 100644 --- a/server/tests/unit/db/repositories/OauthConsents.repository.test.ts +++ b/server/tests/unit/db/repositories/OauthConsents.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { OauthClients } from '../../../../src/db/entities/OauthClients.entity'; import { OauthConsents } from '../../../../src/db/entities/OauthConsents.entity'; import type { OauthConsentsRepository } from '../../../../src/db/repositories/OauthConsents.repository'; @@ -19,7 +20,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); consents = t.repo(OauthConsents); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/OauthTokens.repository.test.ts b/server/tests/unit/db/repositories/OauthTokens.repository.test.ts index b2eb73c88..545705e23 100644 --- a/server/tests/unit/db/repositories/OauthTokens.repository.test.ts +++ b/server/tests/unit/db/repositories/OauthTokens.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { OauthClients } from '../../../../src/db/entities/OauthClients.entity'; import { OauthConsents } from '../../../../src/db/entities/OauthConsents.entity'; import { OauthTokens } from '../../../../src/db/entities/OauthTokens.entity'; @@ -21,7 +22,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); tokens = t.repo(OauthTokens); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(async () => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts b/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts index 97fb4609a..6e65e9154 100644 --- a/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts +++ b/server/tests/unit/db/repositories/PasswordResetTokens.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { PasswordResetTokens } from '../../../../src/db/entities/PasswordResetTokens.entity'; import type { PasswordResetTokensRepository } from '../../../../src/db/repositories/PasswordResetTokens.repository'; import { dbNow } from '../../../../src/db/types'; @@ -209,7 +210,7 @@ describe('PasswordResetTokensRepository', () => { it('PWDRESETREPO-011: findByTokenHash then markConsumed inside uow.transactional survives the closing flush (cannot fail by construction — see PWDRESETREPO-012 for the mutation-proved guard)', async () => { const { user } = createUser(testDb); const id = await seedToken(user.id, hash('survives')); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await uow.transactional(async () => { const found = await tokens.findByTokenHash(hash('survives')); @@ -223,7 +224,7 @@ describe('PasswordResetTokensRepository', () => { it('PWDRESETREPO-012: findByTokenHash is the FIRST, wider read (it carries consumed_at); a narrower managed read follows; markConsumed still lands', async () => { const { user } = createUser(testDb); const id = await seedToken(user.id, hash('dshape')); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await uow.transactional(async () => { await tokens.findByTokenHash(hash('dshape')); // projection A — WIDE (carries consumed_at) @@ -241,7 +242,7 @@ describe('PasswordResetTokensRepository', () => { it('PWDRESETREPO-013: same D-shape guard for consumeAllLiveForUser', async () => { const { user } = createUser(testDb); const id = await seedToken(user.id, hash('dshape-consume-all')); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await uow.transactional(async () => { await tokens.findByTokenHash(hash('dshape-consume-all')); // projection A — WIDE (carries consumed_at) diff --git a/server/tests/unit/db/repositories/Tags.repository.test.ts b/server/tests/unit/db/repositories/Tags.repository.test.ts index 854bde53b..c58e0cf30 100644 --- a/server/tests/unit/db/repositories/Tags.repository.test.ts +++ b/server/tests/unit/db/repositories/Tags.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { Tags } from '../../../../src/db/entities/Tags.entity'; import type { TagsRepository } from '../../../../src/db/repositories/Tags.repository'; import { withRequestContext } from '../../../../src/nest/database/request-context'; @@ -19,7 +20,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); tags = t.repo(Tags); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/Tours.repository.test.ts b/server/tests/unit/db/repositories/Tours.repository.test.ts index 9be5e48b1..bf98634e6 100644 --- a/server/tests/unit/db/repositories/Tours.repository.test.ts +++ b/server/tests/unit/db/repositories/Tours.repository.test.ts @@ -4,6 +4,7 @@ * days hold (MDT-02), the trip copy read and write * (TO6/TO8), and the create/edit writes (TO7/TO9). */ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { DayAssignments } from '../../../../src/db/entities/DayAssignments.entity'; import { Places } from '../../../../src/db/entities/Places.entity'; import { TourPackingLinks } from '../../../../src/db/entities/TourPackingLinks.entity'; @@ -332,7 +333,9 @@ describe('ToursRepository stages on days (MDT-02)', () => { createTour(testDb, other.id); const otherStage = createDayAssignment(testDb, days[2].id, other.id, { tour_stage_key: 'stage-two' }); - const gone = await new UnitOfWork(t.em).transactional(() => repo.deleteTourStagesNotIn(place.id, ['stage-one'])); + const gone = await new UnitOfWork(t.em, new SqliteEngine()).transactional(() => + repo.deleteTourStagesNotIn(place.id, ['stage-one']), + ); expect(gone).toEqual([{ id: second.id, day_id: days[1].id }]); const left = (await findRows(t, DayAssignments, {}, { id: 'asc' })).map((row) => row.id); expect(left).toEqual([first.id, whole.id, otherStage.id]); diff --git a/server/tests/unit/db/repositories/Trips.repository.test.ts b/server/tests/unit/db/repositories/Trips.repository.test.ts index 887dbed7a..61d315037 100644 --- a/server/tests/unit/db/repositories/Trips.repository.test.ts +++ b/server/tests/unit/db/repositories/Trips.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { Trips } from '../../../../src/db/entities/Trips.entity'; import { Users } from '../../../../src/db/entities/Users.entity'; import type { TripsRepository } from '../../../../src/db/repositories/Trips.repository'; @@ -20,7 +21,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); trips = t.repo(Trips); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts b/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts index bbcd63c0a..09c3ab770 100644 --- a/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts +++ b/server/tests/unit/db/repositories/WebauthnCredentials.repository.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { WebauthnCredentials } from '../../../../src/db/entities/WebauthnCredentials.entity'; import type { WebauthnCredentialsRepository } from '../../../../src/db/repositories/WebauthnCredentials.repository'; import { withRequestContext } from '../../../../src/nest/database/request-context'; @@ -317,7 +318,7 @@ describe('WebauthnCredentialsRepository', () => { it('WEBAUTHN-CRED-REPO-017: a nativeUpdate inside uow.transactional is not discarded by a stale entity read earlier under a different projection', async () => { const { user } = createUser(testDb); const cred = await insertCredential(user.id, { counter: 1 }); - const uow = new UnitOfWork(t.em); + const uow = new UnitOfWork(t.em, new SqliteEngine()); await withRequestContext(t.orm, async () => { // Projection A — wide, full row, carries `counter` (PK9's shape: findByCredentialId). diff --git a/server/tests/unit/db/repositories/identity-map-writeback.test.ts b/server/tests/unit/db/repositories/identity-map-writeback.test.ts index 7faa636ce..b70c9e0f1 100644 --- a/server/tests/unit/db/repositories/identity-map-writeback.test.ts +++ b/server/tests/unit/db/repositories/identity-map-writeback.test.ts @@ -30,6 +30,7 @@ * no `disableIdentityMap`) makes IMWB-001 fail with the stale write-back this * test is built to catch. */ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { Users } from '../../../../src/db/entities/Users.entity'; import type { UsersRepository } from '../../../../src/db/repositories/Users.repository'; import { withRequestContext } from '../../../../src/nest/database/request-context'; @@ -50,7 +51,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); users = t.repo(Users); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/db/repositories/trek-repository.test.ts b/server/tests/unit/db/repositories/trek-repository.test.ts index 1a480b0d0..beb1f96b5 100644 --- a/server/tests/unit/db/repositories/trek-repository.test.ts +++ b/server/tests/unit/db/repositories/trek-repository.test.ts @@ -36,6 +36,7 @@ * backwards until this task; `OAUTHTOKREPO-029` (`OauthTokens.repository * .test.ts`) already had it right and is the shape to copy. */ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { Users } from '../../../../src/db/entities/Users.entity'; import { WebauthnChallenges } from '../../../../src/db/entities/WebauthnChallenges.entity'; import type { UsersRepository } from '../../../../src/db/repositories/Users.repository'; @@ -66,7 +67,7 @@ beforeAll(async () => { t = await createTestOrm(testDb, { allowGlobalContext: false }); users = t.repo(Users); challenges = t.repo(WebauthnChallenges); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); // `t.clear()` is deliberately NOT called here (unlike most repository test // files): `EntityManager.clear()` itself calls the VALIDATING `getContext()` diff --git a/server/tests/unit/db/transaction-yield.test.ts b/server/tests/unit/db/transaction-yield.test.ts index 069102540..818700bfc 100644 --- a/server/tests/unit/db/transaction-yield.test.ts +++ b/server/tests/unit/db/transaction-yield.test.ts @@ -35,6 +35,7 @@ * docstring: NESTED propagation) rather than the independent, connection-mutex- * queued transaction two unrelated concurrent requests actually are. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { Trips } from '../../../src/db/entities/Trips.entity'; import type { TripsRepository } from '../../../src/db/repositories/Trips.repository'; import { withRequestContext } from '../../../src/nest/database/request-context'; @@ -57,7 +58,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); trips = t.repo(Trips); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/nest-rpc/plugin-guards.test.ts b/server/tests/unit/nest-rpc/plugin-guards.test.ts index d0805de55..c17da3e23 100644 --- a/server/tests/unit/nest-rpc/plugin-guards.test.ts +++ b/server/tests/unit/nest-rpc/plugin-guards.test.ts @@ -4,6 +4,7 @@ * the legacy router, because rpc-host.test.ts asserts them and shipped plugins read * them, so these tests pin the exact strings rather than just the refusal. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { Users } from '../../../src/db/entities/Users.entity'; import type { TripsRepository } from '../../../src/db/repositories/Trips.repository'; @@ -205,7 +206,7 @@ describe('PluginGuards — canEditAs/canCreateAs against a real UsersRepository beforeEach(async () => { testDb = createSnapshotTestDb(); orm = await createTestOrm(testDb); - const permissions = new PermissionsService(orm.repo(AppSettings), new UnitOfWork(orm.em)); + const permissions = new PermissionsService(orm.repo(AppSettings), new UnitOfWork(orm.em, new SqliteEngine())); const db = { findAccessible: vi.fn(async (tripId: number) => (tripId === 1 ? { id: 1, user_id: ownerId } : undefined)), } as unknown as TripsRepository; diff --git a/server/tests/unit/nest/addons.service.test.ts b/server/tests/unit/nest/addons.service.test.ts index fb3695b0c..27481e3f3 100644 --- a/server/tests/unit/nest/addons.service.test.ts +++ b/server/tests/unit/nest/addons.service.test.ts @@ -21,6 +21,7 @@ * filter itself (the disabled row is excluded) alongside the boolean coercion * of the enabled row that remains. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { Addons } from '../../../src/db/entities/Addons.entity'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { PhotoProviderFields } from '../../../src/db/entities/PhotoProviderFields.entity'; @@ -195,7 +196,7 @@ beforeEach(() => { photoProviderFieldsRepo, appSettingsRepo, usersRepo, - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), ); }); diff --git a/server/tests/unit/nest/backup/database-swap.test.ts b/server/tests/unit/nest/backup/database-swap.test.ts new file mode 100644 index 000000000..8b88c342d --- /dev/null +++ b/server/tests/unit/nest/backup/database-swap.test.ts @@ -0,0 +1,85 @@ +/** + * The restore step the two backup ports share (`database-swap.ts`): the swap, + * then a reopen that always runs, and how the two failures surface. + */ +import { keepRestoreCopy, replaceThenReopen } from '../../../../src/nest/backup/database-swap'; +import { DatabaseConnectionLostError } from '../../../../src/nest/database/database-backup.interface'; + +import { describe, expect, it, vi } from 'vitest'; + +const ok = async () => undefined; +const fails = (error: unknown) => async () => { + throw error; +}; + +describe('replaceThenReopen', () => { + it('SWAP-001: a swap that lands and a reopen that works resolves with no reopen error', async () => { + const swap = vi.fn(ok); + const reopen = vi.fn(ok); + + await expect(replaceThenReopen(swap, reopen)).resolves.toEqual({ reopenError: null }); + expect(swap).toHaveBeenCalledTimes(1); + expect(reopen).toHaveBeenCalledTimes(1); + }); + + it('SWAP-002: a swap that lands and a reopen that fails resolves with the reopen error, so the caller says restart', async () => { + const lost = new Error('reconnect failed'); + + await expect(replaceThenReopen(ok, fails(lost))).resolves.toEqual({ reopenError: lost }); + }); + + it('SWAP-003: a swap that fails still reopens, and its own error is rethrown', async () => { + const reopen = vi.fn(ok); + const swapError = new Error('rename failed'); + + await expect(replaceThenReopen(fails(swapError), reopen)).rejects.toBe(swapError); + expect(reopen).toHaveBeenCalledTimes(1); + }); + + it('SWAP-004: a swap and a reopen that both fail go up together, as DatabaseConnectionLostError', async () => { + const swapError = new Error('rename failed'); + const reopenError = new Error('reconnect failed'); + + const thrown = await replaceThenReopen(fails(swapError), fails(reopenError)).catch((err: unknown) => err); + + expect(thrown).toBeInstanceOf(DatabaseConnectionLostError); + expect((thrown as DatabaseConnectionLostError).swapError).toBe(swapError); + expect((thrown as DatabaseConnectionLostError).cause).toBe(reopenError); + }); + + it('SWAP-005: a step that throws undefined still counts as failed', async () => { + // A reopen that throws undefined is a failure: with a swap that also failed, the two go up as the lost connection. + await expect(replaceThenReopen(fails(new Error('rename failed')), fails(undefined))).rejects.toBeInstanceOf( + DatabaseConnectionLostError, + ); + // A swap that throws undefined rethrows undefined, the same value the step threw. + await expect(replaceThenReopen(fails(undefined), ok)).rejects.toBeUndefined(); + }); + + it('SWAP-006: a synchronous swap that throws is caught like an async one', async () => { + const swapError = new Error('copy failed'); + const reopen = vi.fn(ok); + + await expect( + replaceThenReopen(() => { + throw swapError; + }, reopen), + ).rejects.toBe(swapError); + expect(reopen).toHaveBeenCalledTimes(1); + }); +}); + +describe('keepRestoreCopy', () => { + it('SWAP-007: resolves with the target once the copy is written', async () => { + await expect(keepRestoreCopy('/data/pre-restore-1.db', ok)).resolves.toBe('/data/pre-restore-1.db'); + }); + + it('SWAP-008: a copy that cannot be written resolves with null, so the restore goes on', async () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); + try { + await expect(keepRestoreCopy('/data/pre-restore-2.db', fails(new Error('disk full')))).resolves.toBeNull(); + } finally { + warn.mockRestore(); + } + }); +}); diff --git a/server/tests/unit/nest/database/database-engine.module.test.ts b/server/tests/unit/nest/database/database-engine.module.test.ts new file mode 100644 index 000000000..5164588ff --- /dev/null +++ b/server/tests/unit/nest/database/database-engine.module.test.ts @@ -0,0 +1,29 @@ +/** + * DatabaseEngineModule: one engine per container, shared by every injector. + */ +import { + DatabaseEngineModule, + DATABASE_ENGINE, + DATABASE_SETTINGS, +} from '../../../../src/nest/database/database-engine.module'; +import { Test } from '@nestjs/testing'; + +import { describe, expect, it } from 'vitest'; + +describe('DatabaseEngineModule', () => { + it('ENGINE-DI-001: provides one engine instance, so state an engine keeps is the same wherever it is injected', async () => { + const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile(); + + const engine = moduleRef.get(DATABASE_ENGINE); + expect(moduleRef.get(DATABASE_ENGINE)).toBe(engine); + expect(engine.id).toBe('sqlite'); + }); + + it('ENGINE-DI-002: the settings are read from the environment once and the engine is resolved from them', async () => { + const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile(); + + const settings = moduleRef.get(DATABASE_SETTINGS); + expect(settings.engine).toBe('sqlite'); + expect(moduleRef.get(DATABASE_SETTINGS)).toBe(settings); + }); +}); diff --git a/server/tests/unit/nest/database/database-lifecycle.service.test.ts b/server/tests/unit/nest/database/database-lifecycle.service.test.ts index 77fafa990..e6ec90e50 100644 --- a/server/tests/unit/nest/database/database-lifecycle.service.test.ts +++ b/server/tests/unit/nest/database/database-lifecycle.service.test.ts @@ -8,6 +8,7 @@ * The real open, migrate and reopen run in tests/integration/database-lifecycle.test.ts. */ import { PostgresEngine } from '../../../../src/db/engine/postgres/postgres-engine'; +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { DatabaseLifecycle } from '../../../../src/nest/database/database-lifecycle.service'; import type { MikroORM } from '@mikro-orm/core'; @@ -16,6 +17,9 @@ import os from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +/** The settings a Postgres process would read from TREK_DB_URL, given directly rather than through the environment. */ +const POSTGRES = { engine: 'postgres' as const, url: 'postgres://trek:pw@db/trek', poolMax: 10 }; + const { dbMock, ormMock, calls } = vi.hoisted(() => { const calls: string[] = []; return { @@ -67,7 +71,7 @@ describe('DatabaseLifecycle', () => { it('DBLIFE-001: open() opens the connection, binds the ORM to later swaps, then brings the schema to head', async () => { const { orm } = ormStub(); - await new DatabaseLifecycle(orm).open(); + await new DatabaseLifecycle(orm, new SqliteEngine()).open(); expect(calls).toEqual(['getRawConnection', 'registerReinitializeHook', 'runSchemaBootstrap:{"snapshot":true}']); expect(ormMock.runSchemaBootstrap).toHaveBeenCalledWith(orm, { snapshot: true }); @@ -75,7 +79,7 @@ describe('DatabaseLifecycle', () => { it('DBLIFE-002: the hook open() registers rebuilds the ORM client and migrates the new file without a pre-migrate copy', async () => { const { orm, connection } = ormStub(); - await new DatabaseLifecycle(orm).open(); + await new DatabaseLifecycle(orm, new SqliteEngine()).open(); const hook = dbMock.registerReinitializeHook.mock.calls[0][0]; calls.length = 0; @@ -87,7 +91,7 @@ describe('DatabaseLifecycle', () => { it('DBLIFE-007: listeners registered with onReopened run after the schema bootstrap of a reopen, in order', async () => { const { orm } = ormStub(); - const lifecycle = new DatabaseLifecycle(orm); + const lifecycle = new DatabaseLifecycle(orm, new SqliteEngine()); lifecycle.onReopened(async () => { calls.push('listener:first'); }); @@ -111,7 +115,7 @@ describe('DatabaseLifecycle', () => { it('DBLIFE-008: a listener that throws fails the reopen, so a listener must contain its own failures', async () => { const { orm } = ormStub(); - const lifecycle = new DatabaseLifecycle(orm); + const lifecycle = new DatabaseLifecycle(orm, new SqliteEngine()); lifecycle.onReopened(async () => { throw new Error('listener broke'); }); @@ -122,7 +126,6 @@ describe('DatabaseLifecycle', () => { }); it('DBLIFE-009: on Postgres a reopen keeps the pool and only migrates, so it never waits on a connection lent to a job', async () => { - vi.stubEnv('TREK_DB_URL', 'postgres://trek:pw@db/trek'); const bootstrap = vi .spyOn(PostgresEngine.prototype, 'schemaBootstrap') .mockImplementation(async (_orm, options) => { @@ -130,7 +133,7 @@ describe('DatabaseLifecycle', () => { }); try { const { orm, connection } = ormStub(); - const lifecycle = new DatabaseLifecycle(orm); + const lifecycle = new DatabaseLifecycle(orm, new PostgresEngine(POSTGRES)); lifecycle.onReopened(async () => { calls.push('listener'); }); @@ -144,12 +147,11 @@ describe('DatabaseLifecycle', () => { expect(calls).toEqual(['schemaBootstrap:{"snapshot":false}', 'listener']); } finally { bootstrap.mockRestore(); - vi.unstubAllEnvs(); } }); it('DBLIFE-003: close() and reopen() go through the connection module', async () => { - const lifecycle = new DatabaseLifecycle(ormStub().orm); + const lifecycle = new DatabaseLifecycle(ormStub().orm, new SqliteEngine()); lifecycle.close(); await lifecycle.reopen(); @@ -160,7 +162,9 @@ describe('DatabaseLifecycle', () => { it('DBLIFE-004: a reopen that fails is not swallowed', async () => { dbMock.reinitialize.mockRejectedValueOnce(new Error('database is locked')); - await expect(new DatabaseLifecycle(ormStub().orm).reopen()).rejects.toThrow('database is locked'); + await expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).reopen()).rejects.toThrow( + 'database is locked', + ); }); describe('file', () => { @@ -173,7 +177,7 @@ describe('DatabaseLifecycle', () => { }); it('DBLIFE-005: is the in-memory database under test', () => { - expect(new DatabaseLifecycle(ormStub().orm).file).toBe(':memory:'); + expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).file).toBe(':memory:'); }); it('DBLIFE-006: is TREK_DB_FILE when it is set, the one path the connection, the backup and the restore share', () => { @@ -182,7 +186,7 @@ describe('DatabaseLifecycle', () => { vi.stubEnv('NODE_ENV', 'production'); vi.stubEnv('TREK_DB_FILE', file); - expect(new DatabaseLifecycle(ormStub().orm).file).toBe(file); + expect(new DatabaseLifecycle(ormStub().orm, new SqliteEngine()).file).toBe(file); }); }); }); diff --git a/server/tests/unit/nest/database/unit-of-work.d5.test.ts b/server/tests/unit/nest/database/unit-of-work.d5.test.ts index a26acc0cf..f4805fd87 100644 --- a/server/tests/unit/nest/database/unit-of-work.d5.test.ts +++ b/server/tests/unit/nest/database/unit-of-work.d5.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { Users } from '../../../../src/db/entities/Users.entity'; import { UnitOfWork } from '../../../../src/nest/database/unit-of-work'; import { createSnapshotTestDb } from '../../../helpers/db-mock'; @@ -29,7 +30,7 @@ let uow: UnitOfWork; beforeAll(async () => { t = await createTestOrm(testDb); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); }); beforeEach(() => { resetTestDb(testDb); diff --git a/server/tests/unit/nest/database/unit-of-work.test.ts b/server/tests/unit/nest/database/unit-of-work.test.ts index 50775b6a7..4f6fcd286 100644 --- a/server/tests/unit/nest/database/unit-of-work.test.ts +++ b/server/tests/unit/nest/database/unit-of-work.test.ts @@ -1,3 +1,4 @@ +import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine'; import { Days } from '../../../../src/db/entities/Days.entity'; import type { DaysRepository } from '../../../../src/db/repositories/Days.repository'; import { UnitOfWork } from '../../../../src/nest/database/unit-of-work'; @@ -25,7 +26,7 @@ let days: DaysRepository; */ beforeAll(async () => { t = await createTestOrm(testDb); - uow = new UnitOfWork(t.em); + uow = new UnitOfWork(t.em, new SqliteEngine()); days = t.repo(Days); }); beforeEach(() => { diff --git a/server/tests/unit/nest/oauth.service.test.ts b/server/tests/unit/nest/oauth.service.test.ts index c9388143a..c7fb0b4f8 100644 --- a/server/tests/unit/nest/oauth.service.test.ts +++ b/server/tests/unit/nest/oauth.service.test.ts @@ -11,6 +11,7 @@ import { ADDON_IDS } from '../../../src/addons'; import { getMcpSafeUrl } from '../../../src/app-config'; import { db as testDb } from '../../../src/db/database'; +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { AuditLog } from '../../../src/db/entities/AuditLog.entity'; import { OauthClients } from '../../../src/db/entities/OauthClients.entity'; import { OauthConsents } from '../../../src/db/entities/OauthConsents.entity'; @@ -132,7 +133,7 @@ beforeAll(async () => { consentsRepo, addonsStub, new AuditService(auditLogRepo, usersRepo), - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), cimdStub, ); }); @@ -1446,7 +1447,7 @@ describe('module-scoped OAuth state', () => { consentsRepo, addonsStub, new AuditService(auditLogRepo, usersRepo), - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), cimdStub, ); expect((await secondInstance.consumeAuthCode(code))?.userId).toBe(42); diff --git a/server/tests/unit/nest/postgres-database-backup.test.ts b/server/tests/unit/nest/postgres-database-backup.test.ts index 207463803..19a304469 100644 --- a/server/tests/unit/nest/postgres-database-backup.test.ts +++ b/server/tests/unit/nest/postgres-database-backup.test.ts @@ -4,6 +4,8 @@ * tests/helpers/fake-pg-tools.ts) and a stand-in DatabaseLifecycle. The tools' * own arguments are pinned in tests/unit/db/engine/postgres/pg-tools.test.ts. */ +import { deriveDb } from '../../../src/app-config/derive'; +import { resolveEngine } from '../../../src/db/engine'; import { knownMigrationNames } from '../../../src/db/known-migrations'; import type { DataPathsService } from '../../../src/nest/app-config/data-paths.service'; import { PostgresDatabaseBackup } from '../../../src/nest/backup/postgres-database-backup'; @@ -31,6 +33,17 @@ let extract: string; let reopen: ReturnType; let backup: PostgresDatabaseBackup; +/** The adapter over the settings a process reads from these variables, with stand-ins for the lifecycle and the data directory. */ +function postgresBackup(env: Record): PostgresDatabaseBackup { + const db = deriveDb(env); + return new PostgresDatabaseBackup( + { reopen } as unknown as DatabaseLifecycle, + { dataDir: root } as unknown as DataPathsService, + resolveEngine(db), + db, + ); +} + beforeEach(() => { root = mkdtempSync(path.join(tmpdir(), 'trek-pg-backup-')); bin = path.join(root, 'bin'); @@ -38,13 +51,8 @@ beforeEach(() => { mkdirSync(bin); mkdirSync(extract); installFakeTool(bin, 'psql', { snippets: [answersSchema()] }); - vi.stubEnv('TREK_DB_URL', 'postgresql://trek:pw@db/trek'); - vi.stubEnv('TREK_DB_PG_BIN_DIR', bin); reopen = vi.fn(async () => undefined); - backup = new PostgresDatabaseBackup( - { reopen } as unknown as DatabaseLifecycle, - { dataDir: root } as unknown as DataPathsService, - ); + backup = postgresBackup({ TREK_DB_URL: 'postgresql://trek:pw@db/trek', TREK_DB_PG_BIN_DIR: bin }); }); afterEach(() => { vi.unstubAllEnvs(); @@ -78,7 +86,7 @@ describe('PostgresDatabaseBackup', () => { it('PGBACKUP-002: verify accepts a TREK dump this build knows', async () => { writeFileSync(path.join(extract, 'travel.pgdump'), 'dump'); installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(KNOWN.slice(0, 3))] }); - await expect(backup.verify(extract)).resolves.toBeNull(); + await expect(backup.verify(extract)).resolves.toBeUndefined(); }); it('PGBACKUP-002a: verify refuses an archive without a TREK dump in it', async () => { @@ -159,7 +167,6 @@ describe('PostgresDatabaseBackup', () => { it('PGBACKUP-006: names the database without its password, and refuses to run without TREK_DB_URL', async () => { expect(backup.location()).toBe('Postgres postgresql://trek:***@db/trek'); - vi.stubEnv('TREK_DB_URL', ''); - await expect(backup.snapshot(path.join(root, 'c.pgdump'))).rejects.toThrow(/without TREK_DB_URL/); + await expect(postgresBackup({}).snapshot(path.join(root, 'c.pgdump'))).rejects.toThrow(/without TREK_DB_URL/); }); }); diff --git a/server/tests/unit/nest/sqlite-database-backup.test.ts b/server/tests/unit/nest/sqlite-database-backup.test.ts index 4773211ee..5699cae5e 100644 --- a/server/tests/unit/nest/sqlite-database-backup.test.ts +++ b/server/tests/unit/nest/sqlite-database-backup.test.ts @@ -9,6 +9,7 @@ */ import { SqliteMaintenance } from '../../../src/db/engine/sqlite/sqlite-maintenance'; import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-database-backup'; +import { DomainError } from '../../../src/nest/common/domain-error'; import { DatabaseConnectionLostError } from '../../../src/nest/database/database-backup.interface'; import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service'; import { withRequestContext } from '../../../src/nest/database/request-context'; @@ -146,11 +147,16 @@ describe('SqliteDatabaseBackup', () => { expect(fs.existsSync(target)).toBe(false); }); - it('SQLBK-007: verify refuses an unpacked archive without a database', async () => { - await expect(portWith(maintenanceStub()).verify(dir)).resolves.toEqual({ - error: 'Invalid backup: travel.db not found', - status: 400, - }); + it('SQLBK-007: verify refuses an unpacked archive without a database, as a DomainError the restore route answers', async () => { + const refusal = await portWith(maintenanceStub()) + .verify(dir) + .then( + () => null, + (err: unknown) => err, + ); + expect(refusal).toBeInstanceOf(DomainError); + expect((refusal as DomainError).publicMessage).toBe('Invalid backup: travel.db not found'); + expect((refusal as DomainError).getStatus()).toBe(400); }); it('SQLBK-008: keeps a copy of the database a restore replaces next to it, and says where', async () => { diff --git a/server/tests/unit/plugins/discovery.test.ts b/server/tests/unit/plugins/discovery.test.ts index d8b41e1a6..5d925d3b2 100644 --- a/server/tests/unit/plugins/discovery.test.ts +++ b/server/tests/unit/plugins/discovery.test.ts @@ -8,6 +8,7 @@ * bundle), via a real MikroORM over the full migrated schema (`createSnapshotTestDb` * + `createTestOrm`) rather than a hand-rolled `:memory:` table set. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { PluginActions } from '../../../src/db/entities/PluginActions.entity'; import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity'; import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity'; @@ -47,7 +48,7 @@ beforeAll(async () => { actions: t.repo(PluginActions), settingsFields: t.repo(PluginSettingsFields), errorLog: t.repo(PluginErrorLog), - uow: new UnitOfWork(t.em), + uow: new UnitOfWork(t.em, new SqliteEngine()), }; }); beforeEach(() => { diff --git a/server/tests/unit/plugins/plugin-oauth.service.test.ts b/server/tests/unit/plugins/plugin-oauth.service.test.ts index d1fc9942f..3e6be8d32 100644 --- a/server/tests/unit/plugins/plugin-oauth.service.test.ts +++ b/server/tests/unit/plugins/plugin-oauth.service.test.ts @@ -4,6 +4,7 @@ * https/SSRF guard on the endpoints, the code + refresh token exchanges (mocked fetch), * tokens encrypted at rest, and a stored refresh token that the plugin never sees. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { PluginOauthState } from '../../../src/db/entities/PluginOauthState.entity'; import { PluginOauthTokens } from '../../../src/db/entities/PluginOauthTokens.entity'; import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity'; @@ -90,7 +91,7 @@ async function makeOauthService(): Promise { orm.repo(PluginOauthTokens), orm.repo(PluginOauthState), orm.repo(PluginSettingsFields), - new UnitOfWork(orm.em), + new UnitOfWork(orm.em, new SqliteEngine()), ); } diff --git a/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts b/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts index 80f8d3f7c..b7dbf28df 100644 --- a/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts +++ b/server/tests/unit/plugins/plugin-runtime.boot-no-orm.test.ts @@ -8,6 +8,7 @@ * defensible: dropping the `logError` call, or falling through to `activate` * anyway, would ship green with no test failing. This file pins it directly. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { AuditLog } from '../../../src/db/entities/AuditLog.entity'; import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity'; import { PluginActions } from '../../../src/db/entities/PluginActions.entity'; @@ -99,7 +100,7 @@ async function buildRuntime(withOrm: boolean): Promise { t.repo(NotificationChannelPreferences), // Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead of // `registry?`/`hostFactory?`, matching the real constructor's own order. - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), undefined, undefined, withOrm ? t.orm : undefined, diff --git a/server/tests/unit/plugins/request-context.test.ts b/server/tests/unit/plugins/request-context.test.ts index 508a35bb3..c90d697a6 100644 --- a/server/tests/unit/plugins/request-context.test.ts +++ b/server/tests/unit/plugins/request-context.test.ts @@ -29,6 +29,7 @@ * shared helpers use to let ordinary unit tests call a repository with no * request wrapper at all: that default would hide this exact bug. */ +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; import { Trips } from '../../../src/db/entities/Trips.entity'; import { Users } from '../../../src/db/entities/Users.entity'; @@ -70,7 +71,7 @@ let userId: number; beforeAll(async () => { t = await createTestOrm(testDb, { allowGlobalContext: false }); - permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em)); + permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em, new SqliteEngine())); // Plan 3j Task 1: PluginGuards' own role lookup (canCreateAs, PG4) now goes // through UsersRepository.getRole — a REAL repository bound to `t.em` // (allowGlobalContext: false, same as `permissions`' AppSettingsRepository diff --git a/server/tests/unit/plugins/settings-isolation.test.ts b/server/tests/unit/plugins/settings-isolation.test.ts index db65528b7..456679f49 100644 --- a/server/tests/unit/plugins/settings-isolation.test.ts +++ b/server/tests/unit/plugins/settings-isolation.test.ts @@ -10,6 +10,7 @@ * which for a notification channel meant being dispatched to everyone with no credentials. */ import { db as testDb } from '../../../src/db/database'; +import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine'; import { AuditLog } from '../../../src/db/entities/AuditLog.entity'; import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity'; import { PluginActions } from '../../../src/db/entities/PluginActions.entity'; @@ -204,7 +205,7 @@ describe('a plugin channel label is bounded by the host', () => { t.repo(Settings), t.repo(NotificationChannelPreferences), // Plan 4 Task 4: `uow` is no longer `@Optional()`. - new UnitOfWork(t.em), + new UnitOfWork(t.em, new SqliteEngine()), ); // Stand the plugin up as a granted, active notificationChannel provider. (rt as unknown as { supervisor: { running: Map } }).supervisor.running.set('loud', {