v2.1
Ship configurator updates with catalog DB included for server deploy. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Restore configurateur.db from the newest non-empty backup.
|
||||
* Safe to run while the server is up: the guarded saver refuses empty overwrites,
|
||||
* then restart the server (or let --watch reload) to pick up the restored file.
|
||||
*/
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import initSqlJs from 'sql.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const dataDir = path.join(__dirname, '..', 'data');
|
||||
const dbPath = process.env.DATABASE_PATH || path.join(dataDir, 'configurateur.db');
|
||||
const backupDir = path.join(dataDir, 'backups');
|
||||
|
||||
const SQL = await initSqlJs({
|
||||
locateFile: (f) => path.join(__dirname, '..', 'node_modules', 'sql.js', 'dist', f)
|
||||
});
|
||||
|
||||
function countMaterials(filePath) {
|
||||
if (!fs.existsSync(filePath)) return 0;
|
||||
let db = null;
|
||||
try {
|
||||
db = new SQL.Database(fs.readFileSync(filePath));
|
||||
const r = db.exec('SELECT COUNT(*) FROM formex_materials');
|
||||
return Number(r[0]?.values?.[0]?.[0] ?? 0);
|
||||
} catch {
|
||||
return 0;
|
||||
} finally {
|
||||
try { db?.close(); } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
const backups = fs.existsSync(backupDir)
|
||||
? fs.readdirSync(backupDir)
|
||||
.filter((f) => /^configurateur-.*\.db$/.test(f))
|
||||
.map((f) => {
|
||||
const full = path.join(backupDir, f);
|
||||
return { file: f, full, count: countMaterials(full), mtime: fs.statSync(full).mtimeMs };
|
||||
})
|
||||
.filter((b) => b.count > 0)
|
||||
.sort((a, b) => b.count - a.count || b.mtime - a.mtime)
|
||||
: [];
|
||||
|
||||
if (backups.length === 0) {
|
||||
console.error('No non-empty backup found under server/data/backups/');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const best = backups[0];
|
||||
const currentCount = countMaterials(dbPath);
|
||||
console.log(`Current DB: ${currentCount} materials`);
|
||||
console.log(`Restoring from ${best.file} (${best.count} materials)`);
|
||||
|
||||
if (fs.existsSync(dbPath) && currentCount > 0) {
|
||||
const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
||||
const safety = path.join(backupDir, `configurateur-pre-restore-${stamp}.db`);
|
||||
fs.copyFileSync(dbPath, safety);
|
||||
console.log(`Safety copy of current DB → ${safety}`);
|
||||
}
|
||||
|
||||
fs.copyFileSync(best.full, dbPath);
|
||||
console.log(`Restored. Disk now has ${countMaterials(dbPath)} materials.`);
|
||||
console.log('Restart the API server so it reloads this file into memory.');
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Prove the anti-wipe guard: a stale empty in-memory DB must not overwrite
|
||||
* a richer on-disk materials catalog.
|
||||
*/
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import initSqlJs from 'sql.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const dbPath = path.join(__dirname, '..', 'data', 'configurateur.db');
|
||||
|
||||
const SQL = await initSqlJs({
|
||||
locateFile: (f) => path.join(__dirname, '..', 'node_modules', 'sql.js', 'dist', f)
|
||||
});
|
||||
|
||||
function count(file) {
|
||||
const db = new SQL.Database(fs.readFileSync(file));
|
||||
const n = Number(db.exec('SELECT COUNT(*) FROM formex_materials')[0].values[0][0]);
|
||||
db.close();
|
||||
return n;
|
||||
}
|
||||
|
||||
const before = count(dbPath);
|
||||
console.log('disk before:', before);
|
||||
if (before < 1) {
|
||||
console.error('FAIL: expected materials on disk before guard test');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Simulate a stale empty process writing the whole file (old wipe mode).
|
||||
const empty = new SQL.Database();
|
||||
empty.exec(`
|
||||
CREATE TABLE formex_materials (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
formex_code TEXT UNIQUE NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
collection TEXT NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
// Mimic the NEW guarded save from db/index.js
|
||||
function guardedSave(database) {
|
||||
const memCount = Number(database.exec('SELECT COUNT(*) FROM formex_materials')[0]?.values?.[0]?.[0] ?? 0);
|
||||
const diskCount = count(dbPath);
|
||||
if (diskCount > 0 && memCount < diskCount) {
|
||||
console.log(`GUARD OK: refused overwrite (mem=${memCount}, disk=${diskCount})`);
|
||||
return false;
|
||||
}
|
||||
fs.writeFileSync(dbPath, Buffer.from(database.export()));
|
||||
return true;
|
||||
}
|
||||
|
||||
const wrote = guardedSave(empty);
|
||||
empty.close();
|
||||
const after = count(dbPath);
|
||||
console.log('disk after:', after);
|
||||
if (wrote || after !== before) {
|
||||
console.error('FAIL: guard did not protect materials');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('PASS: scrapes on disk are protected from empty overwrites');
|
||||
Reference in New Issue
Block a user