fix(backup): terapkan restore DB saat startup (tunda swap) - menimpa DB di bawah koneksi hidup merusak (WAL)
This commit is contained in:
1 parent
2aadb66f77
commit
ba0845da47
2 files changed
+28
-11
No files matched your search
@@ -10,6 +10,26 @@ if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true });
|
||||
|
||||
const dbPath = path.join(dbDir, 'billing.db');
|
||||
|
||||
// Terapkan restore DB yang ditunda SEBELUM koneksi dibuka.
|
||||
// (menimpa billing.db di bawah koneksi hidup bisa merusak DB karena WAL bercampur)
|
||||
const _pendingRestore = path.join(dbDir, '.restore-pending');
|
||||
if (fs.existsSync(_pendingRestore)) {
|
||||
try {
|
||||
const _bkName = fs.readFileSync(_pendingRestore, 'utf8').trim();
|
||||
const _bkPath = path.join(__dirname, '..', 'backups', _bkName);
|
||||
if (_bkName && fs.existsSync(_bkPath)) {
|
||||
fs.copyFileSync(_bkPath, dbPath);
|
||||
for (const _ext of ['-wal', '-shm']) { try { fs.unlinkSync(dbPath + _ext); } catch (e) {} }
|
||||
console.log('[DB] Restore tertunda diterapkan dari ' + _bkName);
|
||||
} else {
|
||||
console.error('[DB] Restore tertunda dilewati, backup tidak ada: ' + _bkName);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[DB] Gagal menerapkan restore tertunda:', e.message);
|
||||
}
|
||||
try { fs.unlinkSync(_pendingRestore); } catch (e) {}
|
||||
}
|
||||
|
||||
let db;
|
||||
try {
|
||||
db = new Database(dbPath);
|
||||
|
||||
Reference in new issue
Block a user