v2.1
Ship configurator updates with catalog DB included for server deploy. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
+138
-8
@@ -10,6 +10,7 @@ const dbPath = process.env.DATABASE_PATH || path.join(dataDir, 'configurateur.db
|
||||
const schemaPath = path.join(__dirname, 'schema.sql');
|
||||
|
||||
let db = null;
|
||||
let sqlJs = null;
|
||||
|
||||
/** Création idempotente — bases anciennes sans ces tables (avant migration 014). */
|
||||
const HARDWARE_CATALOG_DDL = `
|
||||
@@ -52,23 +53,144 @@ function toSqlitePlaceholders(sql) {
|
||||
return sql.replace(/\$(\d+)/g, '?');
|
||||
}
|
||||
|
||||
function save() {
|
||||
if (db && dbPath) {
|
||||
const data = db.export();
|
||||
fs.writeFileSync(dbPath, Buffer.from(data));
|
||||
const BACKUP_DIR = path.join(dataDir, 'backups');
|
||||
const MAX_BACKUP_DAYS = 14;
|
||||
|
||||
/**
|
||||
* Count formex_materials in an open sql.js Database instance.
|
||||
*/
|
||||
function countMaterialsIn(database) {
|
||||
if (!database) return 0;
|
||||
try {
|
||||
const result = database.exec('SELECT COUNT(*) AS c FROM formex_materials');
|
||||
return Number(result[0]?.values?.[0]?.[0] ?? 0);
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek material count on a DB file without touching the live in-memory DB.
|
||||
*/
|
||||
function countMaterialsOnDisk(filePath = dbPath) {
|
||||
if (!filePath || !fs.existsSync(filePath) || !sqlJs) return 0;
|
||||
let probe = null;
|
||||
try {
|
||||
const buffer = fs.readFileSync(filePath);
|
||||
if (!buffer.length) return 0;
|
||||
probe = new sqlJs.Database(buffer);
|
||||
return countMaterialsIn(probe);
|
||||
} catch {
|
||||
return 0;
|
||||
} finally {
|
||||
try { probe?.close(); } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Snapshot the on-disk DB before this process touches it, so scraped data
|
||||
* (Häfele, Leuba Hiag, Formex) survives accidental wipes. One backup per
|
||||
* day (first start of the day), kept for MAX_BACKUP_DAYS days.
|
||||
* Never replaces a richer day's backup with an emptier DB.
|
||||
*/
|
||||
function backupExistingDb() {
|
||||
try {
|
||||
if (!fs.existsSync(dbPath)) return;
|
||||
if (!fs.existsSync(BACKUP_DIR)) fs.mkdirSync(BACKUP_DIR, { recursive: true });
|
||||
|
||||
const diskCount = countMaterialsOnDisk(dbPath);
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
const backupPath = path.join(BACKUP_DIR, `configurateur-${today}.db`);
|
||||
|
||||
if (!fs.existsSync(backupPath)) {
|
||||
if (diskCount === 0) {
|
||||
console.warn('[db] skip daily backup — on-disk DB has 0 materials (protects older non-empty backups)');
|
||||
} else {
|
||||
fs.copyFileSync(dbPath, backupPath);
|
||||
console.log(`[db] daily backup created: ${backupPath} (${diskCount} materials)`);
|
||||
}
|
||||
} else {
|
||||
const backupCount = countMaterialsOnDisk(backupPath);
|
||||
if (diskCount > backupCount) {
|
||||
fs.copyFileSync(dbPath, backupPath);
|
||||
console.log(`[db] daily backup upgraded: ${backupPath} (${backupCount} → ${diskCount} materials)`);
|
||||
}
|
||||
}
|
||||
|
||||
const backups = fs.readdirSync(BACKUP_DIR)
|
||||
.filter((f) => /^configurateur-\d{4}-\d{2}-\d{2}\.db$/.test(f))
|
||||
.sort();
|
||||
|
||||
while (backups.length > MAX_BACKUP_DAYS) {
|
||||
// Prefer deleting empty backups so a sole non-empty snapshot is kept.
|
||||
let removeIdx = backups.findIndex((f) => countMaterialsOnDisk(path.join(BACKUP_DIR, f)) === 0);
|
||||
if (removeIdx < 0) removeIdx = 0;
|
||||
const removed = backups.splice(removeIdx, 1)[0];
|
||||
fs.unlinkSync(path.join(BACKUP_DIR, removed));
|
||||
console.warn(`[db] rotated backup removed: ${removed}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[db] backup failed:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically persist the in-memory DB.
|
||||
*
|
||||
* Blocks only *catastrophic* overwrites — the failure mode that kept wiping
|
||||
* scrapes: a stale/empty in-memory process saving over a full on-disk catalog.
|
||||
* Normal scrapes (count grows) and single-row admin deletes still save.
|
||||
*/
|
||||
function save({ allowShrink = false } = {}) {
|
||||
if (!db || !dbPath) return false;
|
||||
|
||||
const memCount = countMaterialsIn(db);
|
||||
if (!allowShrink && fs.existsSync(dbPath)) {
|
||||
const diskCount = countMaterialsOnDisk(dbPath);
|
||||
const emptyClobber = memCount === 0 && diskCount > 0;
|
||||
const massShrink = diskCount >= 20 && memCount < Math.floor(diskCount * 0.1);
|
||||
if (emptyClobber || massShrink) {
|
||||
console.error(
|
||||
`[db] REFUSING save: in-memory has ${memCount} materials but disk has ${diskCount}. ` +
|
||||
'Scraped data protected. Restart the server to reload from disk.'
|
||||
);
|
||||
try {
|
||||
if (!fs.existsSync(BACKUP_DIR)) fs.mkdirSync(BACKUP_DIR, { recursive: true });
|
||||
const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
||||
fs.writeFileSync(
|
||||
path.join(BACKUP_DIR, `configurateur-refused-${stamp}.db`),
|
||||
Buffer.from(db.export())
|
||||
);
|
||||
} catch { /* best-effort forensic copy */ }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const tmpPath = `${dbPath}.tmp`;
|
||||
const data = Buffer.from(db.export());
|
||||
fs.writeFileSync(tmpPath, data);
|
||||
try {
|
||||
fs.renameSync(tmpPath, dbPath);
|
||||
} catch {
|
||||
// Windows may not allow rename over an existing file while another handle is open.
|
||||
fs.copyFileSync(tmpPath, dbPath);
|
||||
try { fs.unlinkSync(tmpPath); } catch { /* ignore */ }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async function getDb() {
|
||||
if (db) return db;
|
||||
const SQL = await initSqlJs({
|
||||
sqlJs = await initSqlJs({
|
||||
locateFile: (file) => path.join(__dirname, '..', 'node_modules', 'sql.js', 'dist', file)
|
||||
});
|
||||
let buffer;
|
||||
if (fs.existsSync(dbPath)) {
|
||||
// sqlJs must be ready before peek-based backup logic.
|
||||
backupExistingDb();
|
||||
buffer = fs.readFileSync(dbPath);
|
||||
}
|
||||
db = new SQL.Database(buffer || null);
|
||||
db = new sqlJs.Database(buffer || null);
|
||||
try {
|
||||
// Ensure full schema exists for legacy/partial DB files.
|
||||
if (fs.existsSync(schemaPath)) {
|
||||
@@ -76,11 +198,13 @@ async function getDb() {
|
||||
db.exec(schemaSql);
|
||||
}
|
||||
db.exec(HARDWARE_CATALOG_DDL);
|
||||
const data = db.export();
|
||||
fs.writeFileSync(dbPath, Buffer.from(data));
|
||||
// Persist schema ensures through the guarded saver (never clobber richer disk).
|
||||
save();
|
||||
} catch (e) {
|
||||
console.error('[db] ensureHardwareCatalogTables:', e.message);
|
||||
}
|
||||
const loaded = countMaterialsIn(db);
|
||||
console.log(`[db] loaded ${loaded} materials from ${dbPath}`);
|
||||
return db;
|
||||
}
|
||||
|
||||
@@ -105,6 +229,12 @@ export const run = async (sql, params = []) => {
|
||||
return {};
|
||||
};
|
||||
|
||||
/** Force-persist even if material count shrinks (admin bulk delete, etc.). */
|
||||
export const saveAllowShrink = async () => {
|
||||
await getDb();
|
||||
return save({ allowShrink: true });
|
||||
};
|
||||
|
||||
const pool = {
|
||||
query: (sql, params) => query(sql, params)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user