Public: anyone can read this; the crew can change it.

A write from the terminal and a write from the web do not collide: BEGIN IMMEDIATE and a busy_timeout #26

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/tempo:scout/24-a-write-from-the-terminal-and-a-write-fr → main 1 commit fork

  • Merged. scout/24-a-write-from-the-terminal-and-a-write-fr was merged into main as 63cd31b.

Review

Sign in to review this change.
2 files changed +60 −11
src/db.test.ts +50 −2
@@ -1,9 +1,10 @@
11 import { expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
2 import { existsSync, mkdtempSync, rmSync } from "node:fs";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
55
6 process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-"));
6 const dataDir = mkdtempSync(join(tmpdir(), "app-"));
7 process.env.FORGE_APP_DATA = dataDir;
78 const db = await import("./db.ts");
89
910 function setup() {
@@ -84,3 +85,50 @@ test("ties are ordered consistently by position then id", () => {
8485 test("undo returns false with nothing to undo", () => {
8586 expect(db.undo("no-such-board")).toBe(false);
8687 });
88
89 // Simulates the ssh terminal and the web server as two separate processes
90 // writing the same file at once: a second handle takes the write lock and
91 // holds it while the module's own moveCard (a read-then-write) is asked to
92 // write too. Without a busy_timeout and an upfront (immediate) lock, that
93 // throws "database is locked" at once; with them, it waits and both writes land.
94 test("a write from another process holding the write lock does not fail moveCard, it waits", async () => {
95 const { boardId, columnId } = setup();
96 const held = db.createCard(boardId, columnId, "held")!;
97 const mover = db.createCard(boardId, columnId, "mover")!;
98
99 const dbPath = db.db.filename;
100 const markerPath = join(dataDir, "locked.marker");
101 rmSync(markerPath, { force: true });
102
103 const script = `
104 import { Database } from "bun:sqlite";
105 import { writeFileSync } from "node:fs";
106 const other = new Database(process.env.DB_PATH);
107 other.exec("PRAGMA journal_mode = WAL");
108 other.exec("PRAGMA busy_timeout = 5000");
109 other.exec("BEGIN IMMEDIATE");
110 other.query("UPDATE card SET title = ?2 WHERE id = ?1").run(process.env.CARD_ID, "touched-by-other-process");
111 writeFileSync(process.env.MARKER_PATH, "locked");
112 Bun.sleepSync(300);
113 other.exec("COMMIT");
114 `;
115 const child = Bun.spawn({
116 cmd: [process.execPath, "-e", script],
117 env: { ...process.env, DB_PATH: dbPath, CARD_ID: held.id, MARKER_PATH: markerPath },
118 stderr: "pipe",
119 });
120
121 const deadline = Date.now() + 8000;
122 while (!existsSync(markerPath) && Date.now() < deadline) await new Promise((r) => setTimeout(r, 10));
123 if (!existsSync(markerPath)) console.error(await new Response(child.stderr).text());
124 expect(existsSync(markerPath)).toBe(true);
125
126 const moved = db.moveCard(boardId, mover.id, columnId, 0);
127
128 const exitCode = await child.exited;
129 expect(exitCode).toBe(0);
130 expect(moved).toBe(true);
131 expect(db.getCard(boardId, held.id)!.title).toBe("touched-by-other-process");
132 const cards = db.listCards(boardId).filter((c) => c.column_id === columnId);
133 expect(cards[0].id).toBe(mover.id);
134 }, 15000);
src/db.ts +10 −9
@@ -16,6 +16,7 @@ const dataDir = process.env.FORGE_APP_DATA ?? "./data";
1616 mkdirSync(dataDir, { recursive: true });
1717 export const db = new Database(join(dataDir, "app.db"));
1818 db.exec("PRAGMA journal_mode = WAL");
19 db.exec("PRAGMA busy_timeout = 5000");
1920 db.exec("PRAGMA foreign_keys = ON");
2021 db.exec(`
2122 CREATE TABLE IF NOT EXISTS board (
@@ -177,7 +178,7 @@ export function createColumn(boardId: string, name: string, explicitPosition?: n
177178 );
178179 logAction(boardId, { deleteColumnIds: [id] });
179180 return { id, board_id: boardId, name, position };
180 })();
181 }).immediate();
181182 }
182183
183184 export function getColumn(boardId: string, columnId: string): Column | null {
@@ -193,7 +194,7 @@ export function renameColumn(boardId: string, columnId: string, name: string): b
193194 db.query("UPDATE board_column SET name = ?2 WHERE id = ?1").run(columnId, name);
194195 logAction(boardId, { restoreColumns: [before] });
195196 return true;
196 })();
197 }).immediate();
197198 }
198199
199200 export function moveColumn(boardId: string, columnId: string, direction: "left" | "right"): boolean {
@@ -208,7 +209,7 @@ export function moveColumn(boardId: string, columnId: string, direction: "left"
208209 db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[j].id, columns[i].position);
209210 logAction(boardId, { restoreColumns: before });
210211 return true;
211 })();
212 }).immediate();
212213 }
213214
214215 export function deleteColumn(boardId: string, columnId: string): boolean {
@@ -219,7 +220,7 @@ export function deleteColumn(boardId: string, columnId: string): boolean {
219220 db.query("DELETE FROM board_column WHERE id = ?1").run(columnId);
220221 logAction(boardId, { restoreColumns: [before], restoreCards: cards });
221222 return true;
222 })();
223 }).immediate();
223224 }
224225
225226 export function createCard(boardId: string, columnId: string, title: string): Card | null {
@@ -237,7 +238,7 @@ export function createCard(boardId: string, columnId: string, title: string): Ca
237238 );
238239 logAction(boardId, { deleteCardIds: [id] });
239240 return getCard(boardId, id);
240 })();
241 }).immediate();
241242 }
242243
243244 export function getCard(boardId: string, cardId: string): Card | null {
@@ -256,7 +257,7 @@ export function editCard(boardId: string, cardId: string, title: string): boolea
256257 db.query("UPDATE card SET title = ?2, updated_at = datetime('now') WHERE id = ?1").run(cardId, title);
257258 logAction(boardId, { restoreCards: [before] });
258259 return true;
259 })();
260 }).immediate();
260261 }
261262
262263 // Moves a card into a column at a 0-based index among that column's
@@ -302,7 +303,7 @@ export function moveCard(boardId: string, cardId: string, columnId: string, inde
302303 );
303304 logAction(boardId, { restoreCards });
304305 return true;
305 })();
306 }).immediate();
306307 }
307308
308309 export function deleteCard(boardId: string, cardId: string): boolean {
@@ -312,7 +313,7 @@ export function deleteCard(boardId: string, cardId: string): boolean {
312313 db.query("DELETE FROM card WHERE id = ?1").run(cardId);
313314 logAction(boardId, { restoreCards: [before] });
314315 return true;
315 })();
316 }).immediate();
316317 }
317318
318319 export function undo(boardId: string): boolean {
@@ -334,5 +335,5 @@ export function undo(boardId: string): boolean {
334335 for (const id of payload.deleteCardIds ?? []) db.query("DELETE FROM card WHERE id = ?1").run(id);
335336 db.query("DELETE FROM action WHERE id = ?1").run(row.id);
336337 return true;
337 })();
338 }).immediate();
338339 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs