mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +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:
@@ -156,7 +156,9 @@ describe('demo addon policy across the hourly reset', () => {
|
||||
vi.spyOn(fs, 'existsSync').mockImplementation(((p: fs.PathLike) => String(p) === BASELINE) as typeof fs.existsSync);
|
||||
// The stand-in swap brings back a baseline whose shelf an admin had changed.
|
||||
const database = {
|
||||
archiveEntry: 'travel.db',
|
||||
canSnapshot: () => true,
|
||||
checkCopy: vi.fn(async () => null),
|
||||
checkpoint: vi.fn(async () => undefined),
|
||||
replace: vi.fn(async () => {
|
||||
await scramble();
|
||||
|
||||
@@ -67,6 +67,8 @@ describe('demo-reset DB path', () => {
|
||||
lifecycle as unknown as DatabaseLifecycle,
|
||||
maintenance as unknown as SqliteMaintenance,
|
||||
);
|
||||
// The baseline here exists only in the existsSync stand-in; DEMORESET-008 covers a refused one.
|
||||
vi.spyOn(database, 'checkCopy').mockResolvedValue(null);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -155,4 +157,18 @@ describe('demo-reset DB path', () => {
|
||||
expect(console.error).not.toHaveBeenCalledWith('[Demo Reset] Failed to restore baseline:', expect.anything());
|
||||
expect(renameSync).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('DEMORESET-008: a baseline this engine cannot restore is not swapped in, but replaced by a fresh one', async () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
vi.spyOn(database, 'checkCopy').mockResolvedValue(`${BASELINE} is not a SQLite database`);
|
||||
|
||||
await withCtx(() => resetDemoUser(database));
|
||||
|
||||
expect(database.checkCopy).toHaveBeenCalledWith(BASELINE);
|
||||
expect(lifecycle.close).not.toHaveBeenCalled();
|
||||
expect(copyFileSync).not.toHaveBeenCalled();
|
||||
expect(maintenance.vacuumInto).toHaveBeenCalledWith(BASELINE + '.tmp');
|
||||
expect(renameSync).toHaveBeenCalledWith(BASELINE + '.tmp', BASELINE);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/is not a SQLite database\); saving the current database/));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -47,7 +47,9 @@ describe('demo reset and the signed-in sessions', () => {
|
||||
await deleteRows(t, UserSessions);
|
||||
};
|
||||
database = {
|
||||
archiveEntry: 'travel.db',
|
||||
canSnapshot: () => true,
|
||||
checkCopy: vi.fn(async () => null),
|
||||
checkpoint: vi.fn(async () => undefined),
|
||||
replace: vi.fn(async () => {
|
||||
await swap();
|
||||
|
||||
@@ -169,4 +169,18 @@ describe('PostgresDatabaseBackup', () => {
|
||||
expect(backup.location()).toBe('Postgres postgresql://trek:***@db/trek');
|
||||
await expect(postgresBackup({}).snapshot(path.join(root, 'c.pgdump'))).rejects.toThrow(/without TREK_DB_URL/);
|
||||
});
|
||||
|
||||
it('PGBACKUP-007: a kept copy (the demo baseline) passes only as a dump with TREK history this build knows', async () => {
|
||||
const copy = path.join(root, 'travel-baseline.pgdump');
|
||||
writeFileSync(copy, 'dump');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(KNOWN.slice(0, 2))] });
|
||||
await expect(backup.checkCopy(copy)).resolves.toBeNull();
|
||||
|
||||
const notADump = failsWith('pg_restore: error: input file does not appear to be a valid archive');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [notADump] });
|
||||
await expect(backup.checkCopy(copy)).resolves.toBe('Uploaded file is not a valid Postgres dump');
|
||||
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(['Migration20990101000000_from_the_future'])] });
|
||||
await expect(backup.checkCopy(copy)).resolves.toMatch(/made by a newer TREK version/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* SqliteDatabaseBackup.checkCopy: a kept copy (the demo baseline) is put back
|
||||
* only when it is a SQLite file; a Postgres dump an engine switch left under
|
||||
* the same name, an empty file or a missing one is refused with a reason.
|
||||
*/
|
||||
import { SqliteDatabaseBackup } from '../../../src/nest/backup/sqlite-database-backup';
|
||||
import type { DatabaseLifecycle } from '../../../src/nest/database/database-lifecycle.service';
|
||||
|
||||
import Database from 'better-sqlite3';
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
let dir: string;
|
||||
const backup = new SqliteDatabaseBackup({ file: ':memory:' } as unknown as DatabaseLifecycle, {} as never);
|
||||
|
||||
beforeEach(() => {
|
||||
dir = mkdtempSync(path.join(tmpdir(), 'trek-check-copy-'));
|
||||
});
|
||||
afterEach(() => {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe('SqliteDatabaseBackup.checkCopy', () => {
|
||||
it('SQLCOPY-001: a SQLite file passes', async () => {
|
||||
const file = path.join(dir, 'travel-baseline.db');
|
||||
const db = new Database(file);
|
||||
db.exec('CREATE TABLE t (x INTEGER)');
|
||||
db.close();
|
||||
await expect(backup.checkCopy(file)).resolves.toBeNull();
|
||||
});
|
||||
|
||||
it('SQLCOPY-002: anything else is refused, naming the file', async () => {
|
||||
const dump = path.join(dir, 'dump.db');
|
||||
writeFileSync(dump, 'PGDMP\u0001\u000e\u0000 not sqlite at all');
|
||||
await expect(backup.checkCopy(dump)).resolves.toBe(`${dump} is not a SQLite database`);
|
||||
const empty = path.join(dir, 'empty.db');
|
||||
writeFileSync(empty, '');
|
||||
await expect(backup.checkCopy(empty)).resolves.toBe(`${empty} is not a SQLite database`);
|
||||
await expect(backup.checkCopy(path.join(dir, 'missing.db'))).resolves.toMatch(/missing\.db cannot be read/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user