mirror of
https://github.com/screentinker/screentinker.git
synced 2026-08-16 23:33:10 -06:00
feat: add automatic SQLite database recovery on malformed error
This commit is contained in:
parent
6229f77ae0
commit
13ffb16952
|
|
@ -18,10 +18,34 @@ if (config.bunnyDbUrl && config.bunnyDbAuthToken) {
|
|||
dbOptions.syncUrl = config.bunnyDbUrl;
|
||||
dbOptions.authToken = config.bunnyDbAuthToken;
|
||||
}
|
||||
const db = new Database(config.dbPath, dbOptions);
|
||||
let db;
|
||||
try {
|
||||
db = new Database(config.dbPath, dbOptions);
|
||||
if (dbOptions.syncUrl) {
|
||||
db.sync(); // Initial sync
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.message && e.message.includes('malformed')) {
|
||||
console.error('[boot] Database is malformed, attempting auto-recovery...');
|
||||
try {
|
||||
const { execSync } = require('child_process');
|
||||
const recoveredPath = config.dbPath + '.recovered';
|
||||
execSync(`sqlite3 "${config.dbPath}" ".recover" | sqlite3 "${recoveredPath}"`);
|
||||
fs.renameSync(config.dbPath, config.dbPath + '.corrupted.' + Date.now());
|
||||
fs.renameSync(recoveredPath, config.dbPath);
|
||||
console.error('[boot] Auto-recovery successful. Reopening...');
|
||||
db = new Database(config.dbPath, dbOptions);
|
||||
if (dbOptions.syncUrl) {
|
||||
db.sync();
|
||||
}
|
||||
} catch (recoverErr) {
|
||||
console.error('[boot] Auto-recovery failed:', recoverErr);
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// Enable foreign keys (and WAL mode if supported)
|
||||
try {
|
||||
|
|
|
|||
Loading…
Reference in a new issue