v2.1
Ship configurator updates with catalog DB included for server deploy. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
+35
-2
@@ -14,10 +14,43 @@ if (!fs.existsSync(dbPath)) {
|
||||
const SQL = await initSqlJs({
|
||||
locateFile: (file) => path.join(__dirname, '..', 'node_modules', 'sql.js', 'dist', file)
|
||||
});
|
||||
|
||||
function countMaterials(database) {
|
||||
try {
|
||||
const result = database.exec('SELECT COUNT(*) AS c FROM formex_materials');
|
||||
return Number(result[0]?.values?.[0]?.[0] ?? 0);
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function atomicWrite(filePath, database) {
|
||||
const tmpPath = `${filePath}.tmp`;
|
||||
fs.writeFileSync(tmpPath, Buffer.from(database.export()));
|
||||
try {
|
||||
fs.renameSync(tmpPath, filePath);
|
||||
} catch {
|
||||
fs.copyFileSync(tmpPath, filePath);
|
||||
try { fs.unlinkSync(tmpPath); } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
const buffer = fs.readFileSync(dbPath);
|
||||
const db = new SQL.Database(buffer);
|
||||
const before = countMaterials(db);
|
||||
|
||||
const seedSql = fs.readFileSync(path.join(__dirname, 'seeds.sql'), 'utf8');
|
||||
db.exec(seedSql);
|
||||
fs.writeFileSync(dbPath, Buffer.from(db.export()));
|
||||
|
||||
const after = countMaterials(db);
|
||||
if (before > 0 && after < before) {
|
||||
console.error(
|
||||
`REFUSING to write seeds: materials would shrink ${before} → ${after}. Aborting.`
|
||||
);
|
||||
db.close();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
atomicWrite(dbPath, db);
|
||||
db.close();
|
||||
console.log('Seeds applied.');
|
||||
console.log(`Seeds applied. Materials preserved: ${after} (was ${before}).`);
|
||||
|
||||
Reference in New Issue
Block a user