From d1860316f6e680b2e452d21735980aaf6cb4312c Mon Sep 17 00:00:00 2001 From: Hermes Date: Sun, 23 Aug 2026 01:47:14 +0000 Subject: [PATCH] =?UTF-8?q?fix(sync):=20keep=20inode=20=E2=80=94=20DELETE?= =?UTF-8?q?=20then=20import=20so=20live=20connection=20sees=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/admin/sync-from-prod/route.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/app/api/admin/sync-from-prod/route.ts b/src/app/api/admin/sync-from-prod/route.ts index 0ff57e0..323b844 100644 --- a/src/app/api/admin/sync-from-prod/route.ts +++ b/src/app/api/admin/sync-from-prod/route.ts @@ -50,10 +50,9 @@ export async function POST(request: NextRequest) { try { fs.writeFileSync(tmp, dump, 'utf-8'); const tableCount = (dump.match(/CREATE TABLE/g) || []).length; - // Wipe existing DB so PKs don't collide on re-sync (atomic replace) - for (const suffix of ['', '-wal', '-shm']) { - try { fs.unlinkSync(dbPath + suffix); } catch {} - } + // Keep the same SQLite file inode so the running app's connection stays valid. + // Clear tables (foreign_keys off) then re-import dump into the same file. + 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 }); if (restore.status !== 0) { return NextResponse.json(err('restore failed: ' + (restore.stderr || restore.stdout || '').slice(0, 1500)), { status: 500 });