refactor(db): type the database settings and inject one engine

The settings for the database were one flat record whose url and pool size
were meaningful only for Postgres, so every reader had to re-check the URL.
They are now a discriminated union per engine (DbEngineSettings): the
Postgres arm carries its URL and a pool size already capped at the ceiling,
and the SQLite arm carries nothing else. The env schema uses the same ceiling
constant, so the boot check and the derivation cannot drift apart.

The engine was resolved from the environment by every caller that needed it
(the lifecycle, the unit of work, the backup adapters), each building its own
instance. The engine is now provided once per container by
DatabaseEngineModule and injected everywhere, so the state an engine keeps
(Postgres's reopen hook) is the same wherever it is used. resolveEngine stays
for the MikroORM CLI config and the DI factory, and nothing else calls it.

The engine's reopen no longer carries a `reopenSwapsConnection` flag. The
engine itself decides what a reopen needs: SQLite closes and reconnects the
ORM's connection before the schema bootstrap (its handle is swapped under
Kysely's cache), and Postgres only migrates, because a restore replaces the
contents of the database the pool already reaches. The Postgres pool is never
closed on a reopen, since ending it waits for connections lent out during the
restore.

The backup ports answer every method the same way. verify() now throws a
DomainError for a refused archive on both engines, and the restore path maps
it back to the same { success: false, error, status } result, so the HTTP
response of a refused restore (status and body) is unchanged. The restore
step that swaps the database and then always reopens it is shared in
nest/backup/database-swap.ts, with the error-description helper in
nest/common, instead of two copies that could drift.
This commit is contained in:
Isaias Tavares
2026-11-05 00:56:39 -03:00
parent 1c3dc93716
commit 98533f3dbe
66 changed files with 639 additions and 351 deletions
@@ -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<typeof postgresTarget>;
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<void> {
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<DatabaseBackupRefusal | null> {
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<void> {
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<string | null> {
keepCopyBeforeRestore(): Promise<string | null> {
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(),
);
}
}