fix: clear instance history on delete and import
All checks were successful
CI / test (pull_request) Successful in 14s
CI / build-dev (pull_request) Has been skipped

deleteInstance now removes history rows for that vmid before removing
the instance. importInstances clears all history before replacing
instances. Prevents stale history appearing when a vmid is reused.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-28 15:37:45 -04:00
parent 5f79eec3dd
commit d17f364fc5
2 changed files with 22 additions and 1 deletions

View File

@@ -151,11 +151,13 @@ export function updateInstance(vmid, data) {
} }
export function deleteInstance(vmid) { export function deleteInstance(vmid) {
return db.prepare('DELETE FROM instances WHERE vmid = ?').run(vmid); db.prepare('DELETE FROM instance_history WHERE vmid = ?').run(vmid);
db.prepare('DELETE FROM instances WHERE vmid = ?').run(vmid);
} }
export function importInstances(rows) { export function importInstances(rows) {
db.exec('BEGIN'); db.exec('BEGIN');
db.exec('DELETE FROM instance_history');
db.exec('DELETE FROM instances'); db.exec('DELETE FROM instances');
const insert = db.prepare(` const insert = db.prepare(`
INSERT INTO instances INSERT INTO instances

View File

@@ -164,6 +164,19 @@ describe('deleteInstance', () => {
expect(getInstance(1)).toBeNull(); expect(getInstance(1)).toBeNull();
expect(getInstance(2)).not.toBeNull(); expect(getInstance(2)).not.toBeNull();
}); });
it('clears history for the deleted instance', () => {
createInstance({ ...base, name: 'a', vmid: 1 });
deleteInstance(1);
expect(getInstanceHistory(1)).toHaveLength(0);
});
it('does not clear history for other instances', () => {
createInstance({ ...base, name: 'a', vmid: 1 });
createInstance({ ...base, name: 'b', vmid: 2 });
deleteInstance(1);
expect(getInstanceHistory(2).length).toBeGreaterThan(0);
});
}); });
// ── importInstances ─────────────────────────────────────────────────────────── // ── importInstances ───────────────────────────────────────────────────────────
@@ -183,6 +196,12 @@ describe('importInstances', () => {
importInstances([]); importInstances([]);
expect(getInstances()).toEqual([]); expect(getInstances()).toEqual([]);
}); });
it('clears history for all replaced instances', () => {
createInstance({ ...base, name: 'old', vmid: 1 });
importInstances([{ ...base, name: 'new', vmid: 2 }]);
expect(getInstanceHistory(1)).toHaveLength(0);
});
}); });
// ── instance history ───────────────────────────────────────────────────────── // ── instance history ─────────────────────────────────────────────────────────