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
Check · Review · Land · Live done — Landed and live in production.
scout/tempo:scout/24-a-write-from-the-terminal-and-a-write-fr → main 1 commit fork
2 files
changed +60
−11
src/db.test.ts +50 −2
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | import { expect, test } from "bun:test"; |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 2 | import { existsSync, mkdtempSync, rmSync } from "node:fs"; | |
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | 5 | |
| 6 | process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-")); | |
| 6 | const dataDir = mkdtempSync(join(tmpdir(), "app-")); | |
| 7 | process.env.FORGE_APP_DATA = dataDir; | |
| 7 | 8 | const db = await import("./db.ts"); |
| 8 | 9 | |
| 9 | 10 | function setup() { |
| @@ -84,3 +85,50 @@ test("ties are ordered consistently by position then id", () => { | ||
| 84 | 85 | test("undo returns false with nothing to undo", () => { |
| 85 | 86 | expect(db.undo("no-such-board")).toBe(false); |
| 86 | 87 | }); |
| 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"; | ||
| 16 | 16 | mkdirSync(dataDir, { recursive: true }); |
| 17 | 17 | export const db = new Database(join(dataDir, "app.db")); |
| 18 | 18 | db.exec("PRAGMA journal_mode = WAL"); |
| 19 | db.exec("PRAGMA busy_timeout = 5000"); | |
| 19 | 20 | db.exec("PRAGMA foreign_keys = ON"); |
| 20 | 21 | db.exec(` |
| 21 | 22 | CREATE TABLE IF NOT EXISTS board ( |
| @@ -177,7 +178,7 @@ export function createColumn(boardId: string, name: string, explicitPosition?: n | ||
| 177 | 178 | ); |
| 178 | 179 | logAction(boardId, { deleteColumnIds: [id] }); |
| 179 | 180 | return { id, board_id: boardId, name, position }; |
| 180 | })(); | |
| 181 | }).immediate(); | |
| 181 | 182 | } |
| 182 | 183 | |
| 183 | 184 | export function getColumn(boardId: string, columnId: string): Column | null { |
| @@ -193,7 +194,7 @@ export function renameColumn(boardId: string, columnId: string, name: string): b | ||
| 193 | 194 | db.query("UPDATE board_column SET name = ?2 WHERE id = ?1").run(columnId, name); |
| 194 | 195 | logAction(boardId, { restoreColumns: [before] }); |
| 195 | 196 | return true; |
| 196 | })(); | |
| 197 | }).immediate(); | |
| 197 | 198 | } |
| 198 | 199 | |
| 199 | 200 | export function moveColumn(boardId: string, columnId: string, direction: "left" | "right"): boolean { |
| @@ -208,7 +209,7 @@ export function moveColumn(boardId: string, columnId: string, direction: "left" | ||
| 208 | 209 | db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[j].id, columns[i].position); |
| 209 | 210 | logAction(boardId, { restoreColumns: before }); |
| 210 | 211 | return true; |
| 211 | })(); | |
| 212 | }).immediate(); | |
| 212 | 213 | } |
| 213 | 214 | |
| 214 | 215 | export function deleteColumn(boardId: string, columnId: string): boolean { |
| @@ -219,7 +220,7 @@ export function deleteColumn(boardId: string, columnId: string): boolean { | ||
| 219 | 220 | db.query("DELETE FROM board_column WHERE id = ?1").run(columnId); |
| 220 | 221 | logAction(boardId, { restoreColumns: [before], restoreCards: cards }); |
| 221 | 222 | return true; |
| 222 | })(); | |
| 223 | }).immediate(); | |
| 223 | 224 | } |
| 224 | 225 | |
| 225 | 226 | 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 | ||
| 237 | 238 | ); |
| 238 | 239 | logAction(boardId, { deleteCardIds: [id] }); |
| 239 | 240 | return getCard(boardId, id); |
| 240 | })(); | |
| 241 | }).immediate(); | |
| 241 | 242 | } |
| 242 | 243 | |
| 243 | 244 | export function getCard(boardId: string, cardId: string): Card | null { |
| @@ -256,7 +257,7 @@ export function editCard(boardId: string, cardId: string, title: string): boolea | ||
| 256 | 257 | db.query("UPDATE card SET title = ?2, updated_at = datetime('now') WHERE id = ?1").run(cardId, title); |
| 257 | 258 | logAction(boardId, { restoreCards: [before] }); |
| 258 | 259 | return true; |
| 259 | })(); | |
| 260 | }).immediate(); | |
| 260 | 261 | } |
| 261 | 262 | |
| 262 | 263 | // 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 | ||
| 302 | 303 | ); |
| 303 | 304 | logAction(boardId, { restoreCards }); |
| 304 | 305 | return true; |
| 305 | })(); | |
| 306 | }).immediate(); | |
| 306 | 307 | } |
| 307 | 308 | |
| 308 | 309 | export function deleteCard(boardId: string, cardId: string): boolean { |
| @@ -312,7 +313,7 @@ export function deleteCard(boardId: string, cardId: string): boolean { | ||
| 312 | 313 | db.query("DELETE FROM card WHERE id = ?1").run(cardId); |
| 313 | 314 | logAction(boardId, { restoreCards: [before] }); |
| 314 | 315 | return true; |
| 315 | })(); | |
| 316 | }).immediate(); | |
| 316 | 317 | } |
| 317 | 318 | |
| 318 | 319 | export function undo(boardId: string): boolean { |
| @@ -334,5 +335,5 @@ export function undo(boardId: string): boolean { | ||
| 334 | 335 | for (const id of payload.deleteCardIds ?? []) db.query("DELETE FROM card WHERE id = ?1").run(id); |
| 335 | 336 | db.query("DELETE FROM action WHERE id = ?1").run(row.id); |
| 336 | 337 | return true; |
| 337 | })(); | |
| 338 | }).immediate(); | |
| 338 | 339 | } |
new above
offline