fix(backup): report a lost connection when the swap and its reopen both fail

SqliteDatabaseBackup.replace reopens the connection in a finally block, so
it runs even when the swap throws. A reopen that failed there was stored
and then dropped: the swap's exception propagated past the return that
would have carried it. The demo reset caught that swap error, logged
"Failed to restore baseline" and returned normally, leaving the instance
without a database connection and nothing in the log to say so. Before
the port, the same branch awaited reinitialize() and its rejection reached
the tick's log line.

replace now throws a DatabaseConnectionLostError when both steps fail. It
carries the swap error and has the reopen failure as its cause, and its
message tells the operator to restart. The demo reset lets that error
through to DemoResetJob's tick, which logs it, and still logs and skips a
failed swap whose reopen succeeded. A restore that hits it answers the
same 500 as any other thrown restore error, with both failures in the
server log.
This commit is contained in:
Maurice
2026-10-08 15:40:00 +02:00
parent fd28ba7fd3
commit 175f5d96da
5 changed files with 100 additions and 14 deletions
+5 -2
View File
@@ -3,7 +3,7 @@ import path from 'path';
import { RequestContext, type EntityManager } from '@mikro-orm/core';
import { readEnv } from '../app-config';
import { DemoRepository, type DemoAdminCredentialsRow, type DemoInstanceKeyRow } from '../db/repositories/DemoRepository';
import type { DatabaseBackupStrategy } from '../nest/database/database-backup.interface';
import { DatabaseConnectionLostError, type DatabaseBackupStrategy } from '../nest/database/database-backup.interface';
const dataDir = path.join(__dirname, '../../data');
const baselinePath = path.join(dataDir, 'travel-baseline.db');
@@ -84,11 +84,14 @@ async function resetDemoUser(database: DatabaseBackupStrategy): Promise<void> {
}
// Close, swap the baseline in, reopen. The reopen runs even when the swap
// fails, so the instance is never left without a connection.
// fails, so the instance is never left without a connection. A failed swap
// alone is logged and the reset skipped; a failed swap whose reopen failed as
// well goes up to the tick's log line, because the connection is gone.
let reopenError: unknown;
try {
({ reopenError } = await database.replace(baselinePath));
} catch (e: unknown) {
if (e instanceof DatabaseConnectionLostError) throw e;
console.error('[Demo Reset] Failed to restore baseline:', e instanceof Error ? e.message : e);
return;
}