fix(sync): filter dump to INSERTs so re-sync is idempotent
This commit is contained in:
@@ -48,11 +48,19 @@ export async function POST(request: NextRequest) {
|
|||||||
|
|
||||||
const tmp = path.join(os.tmpdir(), `pulse-restore-${Date.now()}.sql`);
|
const tmp = path.join(os.tmpdir(), `pulse-restore-${Date.now()}.sql`);
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync(tmp, dump, 'utf-8');
|
|
||||||
const tableCount = (dump.match(/CREATE TABLE/g) || []).length;
|
const tableCount = (dump.match(/CREATE TABLE/g) || []).length;
|
||||||
// Keep the same SQLite file inode so the running app's connection stays valid.
|
// Keep same SQLite inode so live connection stays valid.
|
||||||
// Clear tables (foreign_keys off) then re-import dump into the same file.
|
// 1) Clear existing rows
|
||||||
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' });
|
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' });
|
||||||
|
// 2) Filter dump to INSERTs only (DDL already exists via migrations) — otherwise CREATE TABLE fails on re-sync
|
||||||
|
const filtered = dump
|
||||||
|
.split('\n')
|
||||||
|
.filter((line) => {
|
||||||
|
const t = line.trimStart();
|
||||||
|
return t.startsWith('INSERT ') || t.startsWith('PRAGMA ') || t.startsWith('BEGIN ') || t.startsWith('COMMIT');
|
||||||
|
})
|
||||||
|
.join('\n');
|
||||||
|
fs.writeFileSync(tmp, filtered, '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 });
|
||||||
|
|||||||
Reference in New Issue
Block a user