fix(sync): keep inode — DELETE then import so live connection sees data

This commit is contained in:
Hermes
2026-08-23 01:47:14 +00:00
parent 69107b2e60
commit d1860316f6
+3 -4
View File
@@ -50,10 +50,9 @@ export async function POST(request: NextRequest) {
try { try {
fs.writeFileSync(tmp, dump, 'utf-8'); fs.writeFileSync(tmp, dump, 'utf-8');
const tableCount = (dump.match(/CREATE TABLE/g) || []).length; const tableCount = (dump.match(/CREATE TABLE/g) || []).length;
// Wipe existing DB so PKs don't collide on re-sync (atomic replace) // Keep the same SQLite file inode so the running app's connection stays valid.
for (const suffix of ['', '-wal', '-shm']) { // Clear tables (foreign_keys off) then re-import dump into the same file.
try { fs.unlinkSync(dbPath + suffix); } catch {} spawnSync('sqlite3', [dbPath, "PRAGMA foreign_keys=OFF; DELETE FROM time_entries; DELETE FROM employees; DELETE FROM companies; DELETE FROM audit_log; DELETE FROM __drizzle_migrations; DELETE FROM sqlite_sequence;"], { encoding: 'utf-8' });
}
const restore = spawnSync('sh', ['-c', `sqlite3 "${dbPath}" < "${tmp}"`], { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 }); const restore = spawnSync('sh', ['-c', `sqlite3 "${dbPath}" < "${tmp}"`], { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });
if (restore.status !== 0) { if (restore.status !== 0) {
return NextResponse.json(err('restore failed: ' + (restore.stderr || restore.stdout || '').slice(0, 1500)), { status: 500 }); return NextResponse.json(err('restore failed: ' + (restore.stderr || restore.stdout || '').slice(0, 1500)), { status: 500 });