mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
fix(demo): keep a demo baseline per engine and check it before every reset
The baseline was travel-baseline.db on both engines and went straight into the swap, so a SQLite baseline left in the data directory made every hourly reset of a Postgres demo fail inside pg_restore, and never got replaced. It is now named after the engine's archive entry (travel-baseline.db on SQLite, travel-baseline.pgdump on Postgres), and the backup port's new checkCopy refuses one the engine cannot restore before the swap; the reset then saves a fresh baseline and logs the reason instead of failing hourly.
This commit is contained in:
@@ -16,7 +16,16 @@ import { RequestContext, type EntityManager } from '@mikro-orm/core';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const baselinePath = path.join(resolveDataPaths().dataDir, 'travel-baseline.db');
|
||||
/**
|
||||
* The baseline the hourly reset returns to, named for the engine that wrote
|
||||
* it after the database's own archive entry: `travel-baseline.db` on SQLite,
|
||||
* `travel-baseline.pgdump` on Postgres. An install that changes engine keeps
|
||||
* its data directory, so a baseline the other engine left there is never
|
||||
* taken for this one's.
|
||||
*/
|
||||
function baselinePathFor(database: DatabaseBackupStrategy): string {
|
||||
return path.join(resolveDataPaths().dataDir, `travel-baseline${path.extname(database.archiveEntry)}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* The active fork's `EntityManager`, resolved via `RequestContext.getEntityManager()`
|
||||
@@ -49,6 +58,7 @@ export function requireEntityManager(): EntityManager {
|
||||
* closes the connection, swaps the baseline in atomically and reopens it.
|
||||
*/
|
||||
async function resetDemoUser(database: DatabaseBackupStrategy): Promise<void> {
|
||||
const baselinePath = baselinePathFor(database);
|
||||
if (!fs.existsSync(baselinePath)) {
|
||||
console.log('[Demo Reset] No baseline found, skipping. Admin must save baseline first.');
|
||||
return;
|
||||
@@ -59,6 +69,18 @@ async function resetDemoUser(database: DatabaseBackupStrategy): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
// A baseline this engine cannot restore (damaged, or written by an older
|
||||
// build under this name) would fail inside the swap every hour. It is
|
||||
// replaced by the current database once, with the reason in the log.
|
||||
const refusal = await database.checkCopy(baselinePath);
|
||||
if (refusal) {
|
||||
console.warn(
|
||||
`[Demo Reset] The baseline cannot be restored here (${refusal}); saving the current database as the baseline.`,
|
||||
);
|
||||
await saveBaseline(database);
|
||||
return;
|
||||
}
|
||||
|
||||
// Plan 3i Task 3 / R3 (SAFE spike verdict, task-0-report.md §7): the
|
||||
// pre-close reads (DMR1/DMR2) run through THIS fork — the one
|
||||
// `CronRegistrarService.register()`'s `wrappedTick` already opened for the
|
||||
@@ -183,6 +205,7 @@ async function saveBaseline(database: DatabaseBackupStrategy): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
const baselinePath = baselinePathFor(database);
|
||||
const pending = baselinePath + '.tmp';
|
||||
fs.rmSync(pending, { force: true });
|
||||
await database.snapshot(pending);
|
||||
@@ -190,8 +213,8 @@ async function saveBaseline(database: DatabaseBackupStrategy): Promise<void> {
|
||||
console.log('[Demo] Baseline saved');
|
||||
}
|
||||
|
||||
function hasBaseline(): boolean {
|
||||
return fs.existsSync(baselinePath);
|
||||
function hasBaseline(database: DatabaseBackupStrategy): boolean {
|
||||
return fs.existsSync(baselinePathFor(database));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -64,7 +64,7 @@ export class DemoResetJob implements OnApplicationBootstrap {
|
||||
* not read as the reopen failing.
|
||||
*/
|
||||
private async saveFirstBaselineIfSeeded(): Promise<void> {
|
||||
if (!takeExampleTripsSeeded() || hasBaseline()) return;
|
||||
if (!takeExampleTripsSeeded() || hasBaseline(this.database)) return;
|
||||
try {
|
||||
await withRequestContext({ em: this.em }, () => saveBaseline(this.database));
|
||||
} catch (err: unknown) {
|
||||
|
||||
@@ -32,6 +32,11 @@ export async function checkPostgresDump(extractDir: string, binDir: string | und
|
||||
? 'This backup holds a SQLite database, which cannot be restored into a Postgres database.'
|
||||
: `Invalid backup: ${PG_ARCHIVE_ENTRY} not found`;
|
||||
}
|
||||
return checkPostgresDumpFile(dump, binDir);
|
||||
}
|
||||
|
||||
/** Why the dump at `dump` cannot be restored into a Postgres database, or null: the checks of {@link checkPostgresDump}. */
|
||||
async function checkPostgresDumpFile(dump: string, binDir: string | undefined): Promise<string | null> {
|
||||
let executed: string[];
|
||||
try {
|
||||
executed = await pgDumpMigrations(dump, binDir);
|
||||
@@ -107,6 +112,11 @@ export class PostgresDatabaseBackup implements DatabaseBackupStrategy {
|
||||
if (refusal) throw new DomainError(400, refusal);
|
||||
}
|
||||
|
||||
/** A kept copy must be a dump this build can restore: a SQLite file a switch of engine left behind is refused. */
|
||||
checkCopy(file: string): Promise<string | null> {
|
||||
return checkPostgresDumpFile(file, this.connection.binDir);
|
||||
}
|
||||
|
||||
/** A dump of the database a restore is about to replace, in the data directory. */
|
||||
keepCopyBeforeRestore(): Promise<string | null> {
|
||||
const target = path.join(this.paths.dataDir, `pre-restore-${Date.now()}.pgdump`);
|
||||
|
||||
@@ -9,6 +9,9 @@ import { Injectable } from '@nestjs/common';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
/** The first 16 bytes of every SQLite database file. */
|
||||
const SQLITE_HEADER = Buffer.from('SQLite format 3\0', 'latin1');
|
||||
|
||||
/**
|
||||
* The backup port on a SQLite file, the engine TREK runs on by default.
|
||||
*
|
||||
@@ -61,6 +64,22 @@ export class SqliteDatabaseBackup implements DatabaseBackupStrategy {
|
||||
if (refusal) throw new DomainError(refusal.status, refusal.error);
|
||||
}
|
||||
|
||||
/** A SQLite file starts with its format header; anything else (a Postgres dump, an empty file) is refused. */
|
||||
async checkCopy(file: string): Promise<string | null> {
|
||||
const header = Buffer.alloc(SQLITE_HEADER.length);
|
||||
try {
|
||||
const fd = fs.openSync(file, 'r');
|
||||
try {
|
||||
fs.readSync(fd, header, 0, header.length, 0);
|
||||
} finally {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
return `${file} cannot be read (${err instanceof Error ? err.message : String(err)})`;
|
||||
}
|
||||
return header.equals(SQLITE_HEADER) ? null : `${file} is not a SQLite database`;
|
||||
}
|
||||
|
||||
/** A copy of the database a restore is about to replace, next to it: the swap deletes the current file. */
|
||||
keepCopyBeforeRestore(): Promise<string | null> {
|
||||
const target = path.join(path.dirname(this.lifecycle.file), `pre-restore-${Date.now()}.db`);
|
||||
|
||||
@@ -49,6 +49,15 @@ export interface DatabaseBackupStrategy {
|
||||
*/
|
||||
verify(extractDir: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Why `file`, a copy kept on disk (the demo baseline), cannot be put in place
|
||||
* by `replace` on this engine, or null when it can: SQLite wants a SQLite
|
||||
* file, Postgres a dump with TREK's migration history from this or an older
|
||||
* TREK. Checked before every swap, so a file another engine left behind is
|
||||
* refused with a reason instead of failing inside the swap.
|
||||
*/
|
||||
checkCopy(file: string): Promise<string | null>;
|
||||
|
||||
/**
|
||||
* Keeps a copy of the database a restore is about to replace. Best effort: a
|
||||
* database broken enough to need the restore may not copy, and that must not
|
||||
|
||||
Reference in New Issue
Block a user