mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 22:41:40 +09:00
backupMemoryDb() fell back to a plain file copy after ANY native online-backup exception, not just the encrypted-RFE1 case it exists for. That meant an ordinary SQLITE_BUSY, disk error, or any other native-backup failure on a perfectly normal (unencrypted) SQLite source was silently reported as backedUp: true via a raw copy, with no check that the source was actually encrypted and no check for an active WAL (a plain copy of a live WAL-mode DB can be a torn/inconsistent snapshot). Fix: positively verify the source is RFE1-encrypted (existingFileIsEncrypted, 4-byte magic sniff) before ever taking the raw-copy fallback; refuse it for any other native-backup failure. Even for a confirmed-encrypted source, refuse the fallback if a non-empty `-wal` sidecar is present. Add a `method: 'native-online-backup' | 'raw-copy-fallback'` field to the backup receipt so downstream migration tooling can require a real native snapshot instead of trusting a blanket backedUp: true; thread it through backupProjectMemoryDbs (only set when every successful store agrees) and the nightly backup worker's metrics receipt. Added a BackupOptions.Database test seam for fault-injection tests (a fake native Database whose backup() throws) without needing a real SQLite file. Tests (__tests__/memory-backup-fail-closed.test.ts), all written and confirmed failing against the old code first: - repro: SQLITE_BUSY on an ordinary source must not report backedUp via raw copy - confirmed-encrypted RFE1 source, native backup unavailable/failing, no active WAL: raw-copy fallback succeeds - confirmed-encrypted RFE1 source WITH an active WAL: refuses the fallback - successful native online-backup is marked method: native-online-backup Co-Authored-By: RuFlo <ruv@ruv.net>
78 lines
3.1 KiB
TypeScript
78 lines
3.1 KiB
TypeScript
/**
|
|
* Vector-memory DB backup service (nightly backup worker + `memory backup`).
|
|
*
|
|
* Proves the WAL-safe snapshot is consistent, non-destructive, rotated, and
|
|
* degrades cleanly when there's nothing to back up.
|
|
*/
|
|
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { mkdtempSync, existsSync, readdirSync, statSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
|
|
import { backupMemoryDb } from '../src/services/memory-backup.js';
|
|
|
|
let Database: any;
|
|
let haveNative = false;
|
|
try { Database = (await import('better-sqlite3')).default; haveNative = true; } catch { haveNative = false; }
|
|
|
|
function seedDb(dbPath: string, rows = 50): void {
|
|
const db = new Database(dbPath);
|
|
db.pragma('journal_mode = WAL'); // the mode a naive copy would corrupt
|
|
db.exec('CREATE TABLE memory_entries (id INTEGER PRIMARY KEY, content TEXT)');
|
|
const ins = db.prepare('INSERT INTO memory_entries (content) VALUES (?)');
|
|
const tx = db.transaction(() => { for (let i = 0; i < rows; i++) ins.run('row ' + i); });
|
|
tx();
|
|
db.close();
|
|
}
|
|
|
|
describe.skipIf(!haveNative)('backupMemoryDb', () => {
|
|
let workdir: string;
|
|
beforeEach(() => { workdir = mkdtempSync(join(tmpdir(), 'mem-backup-')); });
|
|
|
|
it('takes a consistent WAL-safe snapshot without mutating the source', async () => {
|
|
const dbPath = join(workdir, 'memory.db');
|
|
seedDb(dbPath, 50);
|
|
const srcSize = statSync(dbPath).size;
|
|
|
|
const r = await backupMemoryDb({ dbPath, timestamp: 1_700_000_000_000 });
|
|
expect(r.backedUp).toBe(true);
|
|
expect(r.method).toBe('native-online-backup');
|
|
expect(existsSync(r.path!)).toBe(true);
|
|
expect(r.sizeBytes!).toBeGreaterThan(0);
|
|
|
|
// Source is unchanged (size stable) and the snapshot has all the rows.
|
|
expect(statSync(dbPath).size).toBe(srcSize);
|
|
const snap = new Database(r.path!, { readonly: true });
|
|
const count = (snap.prepare('SELECT COUNT(*) AS c FROM memory_entries').get() as { c: number }).c;
|
|
snap.close();
|
|
expect(count).toBe(50);
|
|
});
|
|
|
|
it('defaults the destination to <db dir>/backups', async () => {
|
|
const dbPath = join(workdir, 'memory.db');
|
|
seedDb(dbPath);
|
|
const r = await backupMemoryDb({ dbPath, timestamp: 1_700_000_000_000 });
|
|
expect(r.path!.replace(/\\/g, '/')).toContain('/backups/memory-');
|
|
});
|
|
|
|
it('rotates — keeps only the newest N snapshots', async () => {
|
|
const dbPath = join(workdir, 'memory.db');
|
|
seedDb(dbPath);
|
|
const destDir = join(workdir, 'backups');
|
|
// 5 snapshots at increasing timestamps, keep=3.
|
|
for (let i = 0; i < 5; i++) {
|
|
await backupMemoryDb({ dbPath, destDir, keep: 3, timestamp: 1_700_000_000_000 + i * 86_400_000 });
|
|
}
|
|
const snaps = readdirSync(destDir).filter(f => /^memory-.*\.db$/.test(f));
|
|
expect(snaps.length).toBe(3);
|
|
// The three newest (largest timestamps) survive.
|
|
expect(snaps.sort().slice(-1)[0]).toContain('memory-');
|
|
});
|
|
|
|
it('is a safe no-op when there is no DB to back up', async () => {
|
|
const r = await backupMemoryDb({ dbPath: join(workdir, 'nope.db') });
|
|
expect(r.backedUp).toBe(false);
|
|
expect(r.skipped).toBe('no-db');
|
|
});
|
|
});
|