fix(backup): back up and restore through an injected database port

Backup, restore and the demo reset each handled the SQLite file
themselves: backup.impl closed and reinitialized the connection, built
MaintenanceRepository with new, and assumed the database was
data/travel.db. That tied the whole chain to one local file and hid two
bugs.

They now go through DATABASE_BACKUP, a port declared in the shared
database kernel (snapshot, verify, keep a copy before a restore, replace).
SqliteDatabaseBackup implements it with what the code did before:
VACUUM INTO for a consistent copy, and a close, copy, sidecar drop,
rename and reopen through DatabaseLifecycle for a restore. BackupService
injects it and hands it to backup.impl. The demo reset and the baseline
save use the same port; the first baseline is saved by DemoResetJob once
the app is up, because the boot-time seed runs before anything can be
injected. Another engine can plug in a logical dump behind the port.

Fixes on the way:

- The backup and the restore ignored TREK_DB_FILE. The backup looked for
  data/travel.db and, with the database elsewhere, silently left it out
  of the archive; the restore wrote data/travel.db and never replaced
  the live database. Both now use DatabaseLifecycle.file, the path the
  connection runs on, and the pre-restore copy sits next to it.
- A failed VACUUM INTO fell back to archiving the live WAL-mode file
  without a word, the torn copy the snapshot exists to prevent. The
  backup now fails: nothing is archived or committed, the existing
  backups are untouched, a partial snapshot is removed, and the log says
  why. A missing database file and a failed checkpoint are logged too.
This commit is contained in:
Maurice
2026-10-08 15:25:52 +02:00
parent 8ede0dab9e
commit 0aa5b36661
26 changed files with 1029 additions and 473 deletions
@@ -0,0 +1,111 @@
import fs from 'fs';
import path from 'path';
import { Injectable } from '@nestjs/common';
import { MaintenanceRepository } from '../../db/repositories/MaintenanceRepository';
import { DatabaseLifecycle } from '../database/database-lifecycle.service';
import type { DatabaseBackupRefusal, DatabaseBackupStrategy } from '../database/database-backup.interface';
import { logInfo, logWarn } from '../audit/audit-log.logger';
import { checkBackupDatabase } from './backup-archive';
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.
*
* 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.
*
* 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
* along with the database.
*/
@Injectable()
export class SqliteDatabaseBackup implements DatabaseBackupStrategy {
readonly archiveEntry = 'travel.db';
constructor(
private readonly lifecycle: DatabaseLifecycle,
private readonly maintenance: MaintenanceRepository,
) {}
location(): string {
return this.lifecycle.file;
}
canSnapshot(): boolean {
const file = this.lifecycle.file;
return file !== ':memory:' && fs.existsSync(file);
}
async checkpoint(): Promise<void> {
await this.maintenance.walCheckpoint();
}
async snapshot(target: string): Promise<void> {
try {
await this.maintenance.vacuumInto(target);
} catch (err) {
// A VACUUM INTO that failed part-way (a full disk) can leave a truncated
// file behind. Nothing may mistake it for a copy.
fs.rmSync(target, { force: true });
throw err;
}
}
verify(extractDir: string): DatabaseBackupRefusal | null {
return checkBackupDatabase(extractDir);
}
/**
* 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<string | null> {
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;
}
}
async replace(source: string): Promise<{ reopenError: unknown }> {
const dest = this.lifecycle.file;
this.lifecycle.close();
let reopenError: 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);
} finally {
// Reopening must always run, even when the swap threw, so the process is
// never left without a connection. A reopen failure is reported, not
// thrown: the files already landed and the caller has to say "restart".
try {
await this.lifecycle.reopen();
} catch (err) {
reopenError = err;
}
}
return { reopenError };
}
}