0f06d3e76e90

turn a mirror's foreign keys on after its upgrade, which drops referenced tables (v3.2.12)

time
2026-11-18 00:49 UTC · 1 week ago
machine
Junes-MacBook-Pro.local
agent
claude-opus-5-5
session
verify review findings
parent
70defb038d74
3 files package.json+1 −1src/mirror/store.ts+2 −1tests/mirrorupgrade.test.ts+51 −0
package.json+1 −1
@@ -1,6 +1,6 @@11 {22 	"name": "kepr",3-	"version": "3.2.11",3+	"version": "3.2.12",44 	"private": true,55 	"type": "module",66 	"scripts": {

view file

src/mirror/store.ts+2 −1
@@ -190,10 +190,11 @@190190 	mkdirSync(dir, { recursive: true });191191 	const db = new Database(path, { strict: true });192192 	db.run("PRAGMA journal_mode = WAL");193-	db.run("PRAGMA foreign_keys = ON");194193 	db.run("PRAGMA busy_timeout = 5000");195194 	// A current mirror needs no lock; only creating or rebuilding one takes the write lock.196195 	if (schemaVersion(db) !== SCHEMA_VERSION) upgrade(db);196+	// Only now: a rebuild drops tables other rows still reference, which foreign keys would refuse.197+	db.run("PRAGMA foreign_keys = ON");197198 	// Mirrors counted under an older rule are recounted now, not at the next save.198199 	if (mirrorState(db, "head") && mirrorState(db, "stats_rule") !== STATS_RULE) refreshStats(db);199200 	open.set(path, db);

view file

tests/mirrorupgrade.test.ts+51 −0
@@ -0,0 +1,51 @@1+import { Database } from "bun:sqlite";2+import { expect, test } from "bun:test";3+import { mkdtempSync } from "node:fs";4+import { tmpdir } from "node:os";5+import { join } from "node:path";6+import { closeMirrors, openMirror } from "../src/mirror/store.ts";7+8+// The version 4 mirror Kepr 3.2.3 wrote, as far as its foreign keys go: a save names its parent,9+// and a file row names its save and its object. Live mirrors hold rows like these.10+const V4 = `11+CREATE TABLE objects (hash TEXT PRIMARY KEY, size INTEGER NOT NULL, data BLOB NOT NULL);12+CREATE TABLE saves (13+	id TEXT PRIMARY KEY, parent TEXT REFERENCES saves(id), message TEXT NOT NULL,14+	created_at INTEGER NOT NULL, machine TEXT NOT NULL, is_checkpoint INTEGER NOT NULL DEFAULT 0,15+	size_bytes INTEGER NOT NULL DEFAULT 0, agent TEXT, session TEXT, restores TEXT, replayed_from TEXT16+);17+CREATE TABLE save_files (18+	save_id TEXT NOT NULL REFERENCES saves(id), path TEXT NOT NULL,19+	hash TEXT NOT NULL REFERENCES objects(hash), executable INTEGER NOT NULL DEFAULT 0,20+	PRIMARY KEY (save_id, path)21+);22+CREATE TABLE hidden_files (23+	save_id TEXT NOT NULL REFERENCES saves(id), path TEXT NOT NULL,24+	hash TEXT NOT NULL REFERENCES objects(hash), executable INTEGER NOT NULL DEFAULT 0,25+	PRIMARY KEY (save_id, path)26+);27+CREATE TABLE state (key TEXT PRIMARY KEY, value TEXT NOT NULL);28+INSERT INTO objects VALUES ('${"a".repeat(64)}', 2, x'789c');29+INSERT INTO saves (id, message, created_at, machine) VALUES ('aaaaaaaaaaaa', 'first', 1, 'm');30+INSERT INTO saves (id, parent, message, created_at, machine)31+	VALUES ('bbbbbbbbbbbb', 'aaaaaaaaaaaa', 'second', 2, 'm');32+INSERT INTO save_files VALUES ('bbbbbbbbbbbb', 'a.txt', '${"a".repeat(64)}', 0);33+PRAGMA user_version = 4;34+`;35+36+test("a version 4 mirror holding saves and files rebuilds as the current version", () => {37+	const dir = mkdtempSync(join(tmpdir(), "kepr-mirror-upgrade-"));38+	const old = new Database(join(dir, "app.db"));39+	old.run("PRAGMA foreign_keys = ON");40+	old.exec(V4);41+	old.close();42+43+	const mirror = openMirror(dir, "app");44+	const version = mirror.query<{ user_version: number }, []>("PRAGMA user_version").get();45+	expect(version?.user_version).toBeGreaterThan(4);46+	expect(mirror.query("SELECT 1 FROM saves").all()).toEqual([]);47+	expect(48+		mirror.query<{ foreign_keys: number }, []>("PRAGMA foreign_keys").get()?.foreign_keys,49+	).toBe(1);50+	closeMirrors();51+});

view file