mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +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:
@@ -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`);
|
||||
|
||||
Reference in New Issue
Block a user