Public: anyone can read this; the crew can change it.
Two people moving cards at once keep one order: transactional writes and positions that cannot collapse #19
Check · Review · Land · Live done — Landed and live in production.
scout/tempo:scout/15-two-people-moving-cards-at-once-keep-one → main 1 commit fork
3 files
changed +217
−85
README.md +4
| @@ -27,6 +27,10 @@ FORGE_APP_DATA=./data bun run src/tty.ts # the terminal interface | ||
| 27 | 27 | - **Undo**: reverses the last change to the board — a create, edit, |
| 28 | 28 | move, or delete of a card or column. It is a shared, board-wide undo |
| 29 | 29 | (there's no per-person history), which fits a board anyone can edit. |
| 30 | - **Concurrent drags**: two people dragging cards at once never tear | |
| 31 | each other's write — every move runs in one transaction — and cards | |
| 32 | are ordered by position, then id, so even a rare tie still sorts the | |
| 33 | same way for everyone. | |
| 30 | 34 | |
| 31 | 35 | The first paint is plain server-rendered HTML; `static/board.js` (named |
| 32 | 36 | by its content hash, cached for a year) then adds drag-and-drop and |
src/db.test.ts added +86
| @@ -0,0 +1,86 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | ||
| 6 | process.env.FORGE_APP_DATA = mkdtempSync(join(tmpdir(), "app-")); | |
| 7 | const db = await import("./db.ts"); | |
| 8 | ||
| 9 | function setup() { | |
| 10 | const board = db.createBoard("Board"); | |
| 11 | const column = db.listColumns(board.id)[0]; | |
| 12 | return { boardId: board.id, columnId: column.id }; | |
| 13 | } | |
| 14 | ||
| 15 | test("two cards created back to back get distinct positions in creation order", () => { | |
| 16 | const { boardId, columnId } = setup(); | |
| 17 | const a = db.createCard(boardId, columnId, "First")!; | |
| 18 | const b = db.createCard(boardId, columnId, "Second")!; | |
| 19 | expect(a.position).toBeLessThan(b.position); | |
| 20 | }); | |
| 21 | ||
| 22 | test("moving a card into a column that does not exist fails cleanly and writes no orphan", () => { | |
| 23 | const { boardId, columnId } = setup(); | |
| 24 | const card = db.createCard(boardId, columnId, "Homeless")!; | |
| 25 | const moved = db.moveCard(boardId, card.id, "no-such-column", 0); | |
| 26 | expect(moved).toBe(false); | |
| 27 | expect(db.getCard(boardId, card.id)!.column_id).toBe(columnId); | |
| 28 | }); | |
| 29 | ||
| 30 | test("repeated midpoint drops into the same slot cannot collapse positions", () => { | |
| 31 | const { boardId, columnId } = setup(); | |
| 32 | db.createCard(boardId, columnId, "lo"); | |
| 33 | db.createCard(boardId, columnId, "hi"); | |
| 34 | ||
| 35 | let last!: { id: string }; | |
| 36 | for (let i = 0; i < 60; i++) { | |
| 37 | last = db.createCard(boardId, columnId, `c${i}`)!; | |
| 38 | db.moveCard(boardId, last.id, columnId, 1); | |
| 39 | } | |
| 40 | ||
| 41 | const cards = db.listCards(boardId).filter((c) => c.column_id === columnId); | |
| 42 | const positions = cards.map((c) => c.position); | |
| 43 | expect(new Set(positions).size).toBe(positions.length); | |
| 44 | expect(positions.every(Number.isFinite)).toBe(true); | |
| 45 | expect(cards[1].id).toBe(last.id); | |
| 46 | }); | |
| 47 | ||
| 48 | test("undo of a renormalising move restores every renormalised card, not just the one that moved", () => { | |
| 49 | const { boardId, columnId } = setup(); | |
| 50 | db.createCard(boardId, columnId, "lo"); | |
| 51 | db.createCard(boardId, columnId, "hi"); | |
| 52 | for (let i = 0; i < 60; i++) { | |
| 53 | const c = db.createCard(boardId, columnId, `c${i}`)!; | |
| 54 | db.moveCard(boardId, c.id, columnId, 1); | |
| 55 | } | |
| 56 | const before = db.listCards(boardId).filter((c) => c.column_id === columnId); | |
| 57 | ||
| 58 | const mover = db.createCard(boardId, columnId, "mover")!; | |
| 59 | const moverBefore = db.getCard(boardId, mover.id)!; | |
| 60 | db.moveCard(boardId, mover.id, columnId, 1); | |
| 61 | const afterMove = db.listCards(boardId).filter((c) => c.column_id === columnId); | |
| 62 | expect(afterMove.length).toBe(before.length + 1); | |
| 63 | ||
| 64 | expect(db.undo(boardId)).toBe(true); | |
| 65 | const afterUndo = db.listCards(boardId).filter((c) => c.column_id === columnId && c.id !== mover.id); | |
| 66 | expect(afterUndo.map((c) => [c.id, c.position])).toEqual(before.map((c) => [c.id, c.position])); | |
| 67 | expect(db.getCard(boardId, mover.id)!.position).toBe(moverBefore.position); | |
| 68 | }); | |
| 69 | ||
| 70 | test("ties are ordered consistently by position then id", () => { | |
| 71 | const { boardId, columnId } = setup(); | |
| 72 | const a = db.createCard(boardId, columnId, "A")!; | |
| 73 | const b = db.createCard(boardId, columnId, "B")!; | |
| 74 | // Force a tie directly, as no code path in db.ts produces one; the | |
| 75 | // reader must still agree on one order if it ever happens. | |
| 76 | db.db.query("UPDATE card SET position = 1 WHERE id = ?1 OR id = ?2").run(a.id, b.id); | |
| 77 | ||
| 78 | const [first, second] = db.listCards(boardId).filter((c) => c.column_id === columnId); | |
| 79 | const [expectedFirst, expectedSecond] = [a.id, b.id].sort(); | |
| 80 | expect(first.id).toBe(expectedFirst); | |
| 81 | expect(second.id).toBe(expectedSecond); | |
| 82 | }); | |
| 83 | ||
| 84 | test("undo returns false with nothing to undo", () => { | |
| 85 | expect(db.undo("no-such-board")).toBe(false); | |
| 86 | }); |
src/db.ts +127 −85
| @@ -103,7 +103,10 @@ export function renameBoard(boardId: string, name: string): void { | ||
| 103 | 103 | |
| 104 | 104 | export function listColumns(boardId: string): Column[] { |
| 105 | 105 | return db |
| 106 | .query<Column, [string]>("SELECT id, board_id, name, position FROM board_column WHERE board_id = ?1 ORDER BY position") | |
| 106 | .query< | |
| 107 | Column, | |
| 108 | [string] | |
| 109 | >("SELECT id, board_id, name, position FROM board_column WHERE board_id = ?1 ORDER BY position, id") | |
| 107 | 110 | .all(boardId); |
| 108 | 111 | } |
| 109 | 112 | |
| @@ -112,7 +115,7 @@ export function listCards(boardId: string): Card[] { | ||
| 112 | 115 | .query< |
| 113 | 116 | Card, |
| 114 | 117 | [string] |
| 115 | >("SELECT id, board_id, column_id, title, position, created_at, updated_at FROM card WHERE board_id = ?1 ORDER BY position") | |
| 118 | >("SELECT id, board_id, column_id, title, position, created_at, updated_at FROM card WHERE board_id = ?1 ORDER BY position, id") | |
| 116 | 119 | .all(boardId); |
| 117 | 120 | } |
| 118 | 121 | |
| @@ -121,16 +124,18 @@ function nextPosition(rows: { position: number }[]): number { | ||
| 121 | 124 | } |
| 122 | 125 | |
| 123 | 126 | export function createColumn(boardId: string, name: string, explicitPosition?: number): Column { |
| 124 | const id = randomId(12); | |
| 125 | const position = explicitPosition ?? nextPosition(listColumns(boardId)); | |
| 126 | db.query("INSERT INTO board_column (id, board_id, name, position) VALUES (?1, ?2, ?3, ?4)").run( | |
| 127 | id, | |
| 128 | boardId, | |
| 129 | name, | |
| 130 | position, | |
| 131 | ); | |
| 132 | logAction(boardId, { deleteColumnIds: [id] }); | |
| 133 | return { id, board_id: boardId, name, position }; | |
| 127 | return db.transaction((): Column => { | |
| 128 | const id = randomId(12); | |
| 129 | const position = explicitPosition ?? nextPosition(listColumns(boardId)); | |
| 130 | db.query("INSERT INTO board_column (id, board_id, name, position) VALUES (?1, ?2, ?3, ?4)").run( | |
| 131 | id, | |
| 132 | boardId, | |
| 133 | name, | |
| 134 | position, | |
| 135 | ); | |
| 136 | logAction(boardId, { deleteColumnIds: [id] }); | |
| 137 | return { id, board_id: boardId, name, position }; | |
| 138 | })(); | |
| 134 | 139 | } |
| 135 | 140 | |
| 136 | 141 | export function getColumn(boardId: string, columnId: string): Column | null { |
| @@ -140,49 +145,57 @@ export function getColumn(boardId: string, columnId: string): Column | null { | ||
| 140 | 145 | } |
| 141 | 146 | |
| 142 | 147 | export function renameColumn(boardId: string, columnId: string, name: string): boolean { |
| 143 | const before = getColumn(boardId, columnId); | |
| 144 | if (!before) return false; | |
| 145 | db.query("UPDATE board_column SET name = ?2 WHERE id = ?1").run(columnId, name); | |
| 146 | logAction(boardId, { restoreColumns: [before] }); | |
| 147 | return true; | |
| 148 | return db.transaction((): boolean => { | |
| 149 | const before = getColumn(boardId, columnId); | |
| 150 | if (!before) return false; | |
| 151 | db.query("UPDATE board_column SET name = ?2 WHERE id = ?1").run(columnId, name); | |
| 152 | logAction(boardId, { restoreColumns: [before] }); | |
| 153 | return true; | |
| 154 | })(); | |
| 148 | 155 | } |
| 149 | 156 | |
| 150 | 157 | export function moveColumn(boardId: string, columnId: string, direction: "left" | "right"): boolean { |
| 151 | const columns = listColumns(boardId); | |
| 152 | const i = columns.findIndex((c) => c.id === columnId); | |
| 153 | if (i < 0) return false; | |
| 154 | const j = direction === "left" ? i - 1 : i + 1; | |
| 155 | if (j < 0 || j >= columns.length) return false; | |
| 156 | const before = [columns[i], columns[j]]; | |
| 157 | db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[i].id, columns[j].position); | |
| 158 | db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[j].id, columns[i].position); | |
| 159 | logAction(boardId, { restoreColumns: before }); | |
| 160 | return true; | |
| 158 | return db.transaction((): boolean => { | |
| 159 | const columns = listColumns(boardId); | |
| 160 | const i = columns.findIndex((c) => c.id === columnId); | |
| 161 | if (i < 0) return false; | |
| 162 | const j = direction === "left" ? i - 1 : i + 1; | |
| 163 | if (j < 0 || j >= columns.length) return false; | |
| 164 | const before = [columns[i], columns[j]]; | |
| 165 | db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[i].id, columns[j].position); | |
| 166 | db.query("UPDATE board_column SET position = ?2 WHERE id = ?1").run(columns[j].id, columns[i].position); | |
| 167 | logAction(boardId, { restoreColumns: before }); | |
| 168 | return true; | |
| 169 | })(); | |
| 161 | 170 | } |
| 162 | 171 | |
| 163 | 172 | export function deleteColumn(boardId: string, columnId: string): boolean { |
| 164 | const before = getColumn(boardId, columnId); | |
| 165 | if (!before) return false; | |
| 166 | const cards = listCards(boardId).filter((c) => c.column_id === columnId); | |
| 167 | db.query("DELETE FROM board_column WHERE id = ?1").run(columnId); | |
| 168 | logAction(boardId, { restoreColumns: [before], restoreCards: cards }); | |
| 169 | return true; | |
| 173 | return db.transaction((): boolean => { | |
| 174 | const before = getColumn(boardId, columnId); | |
| 175 | if (!before) return false; | |
| 176 | const cards = listCards(boardId).filter((c) => c.column_id === columnId); | |
| 177 | db.query("DELETE FROM board_column WHERE id = ?1").run(columnId); | |
| 178 | logAction(boardId, { restoreColumns: [before], restoreCards: cards }); | |
| 179 | return true; | |
| 180 | })(); | |
| 170 | 181 | } |
| 171 | 182 | |
| 172 | 183 | export function createCard(boardId: string, columnId: string, title: string): Card | null { |
| 173 | const column = getColumn(boardId, columnId); | |
| 174 | if (!column) return null; | |
| 175 | const id = randomId(12); | |
| 176 | const position = nextPosition(listCards(boardId).filter((c) => c.column_id === columnId)); | |
| 177 | db.query("INSERT INTO card (id, board_id, column_id, title, position) VALUES (?1, ?2, ?3, ?4, ?5)").run( | |
| 178 | id, | |
| 179 | boardId, | |
| 180 | columnId, | |
| 181 | title, | |
| 182 | position, | |
| 183 | ); | |
| 184 | logAction(boardId, { deleteCardIds: [id] }); | |
| 185 | return getCard(boardId, id); | |
| 184 | return db.transaction((): Card | null => { | |
| 185 | const column = getColumn(boardId, columnId); | |
| 186 | if (!column) return null; | |
| 187 | const id = randomId(12); | |
| 188 | const position = nextPosition(listCards(boardId).filter((c) => c.column_id === columnId)); | |
| 189 | db.query("INSERT INTO card (id, board_id, column_id, title, position) VALUES (?1, ?2, ?3, ?4, ?5)").run( | |
| 190 | id, | |
| 191 | boardId, | |
| 192 | columnId, | |
| 193 | title, | |
| 194 | position, | |
| 195 | ); | |
| 196 | logAction(boardId, { deleteCardIds: [id] }); | |
| 197 | return getCard(boardId, id); | |
| 198 | })(); | |
| 186 | 199 | } |
| 187 | 200 | |
| 188 | 201 | export function getCard(boardId: string, cardId: string): Card | null { |
| @@ -195,60 +208,89 @@ export function getCard(boardId: string, cardId: string): Card | null { | ||
| 195 | 208 | } |
| 196 | 209 | |
| 197 | 210 | export function editCard(boardId: string, cardId: string, title: string): boolean { |
| 198 | const before = getCard(boardId, cardId); | |
| 199 | if (!before) return false; | |
| 200 | db.query("UPDATE card SET title = ?2, updated_at = datetime('now') WHERE id = ?1").run(cardId, title); | |
| 201 | logAction(boardId, { restoreCards: [before] }); | |
| 202 | return true; | |
| 211 | return db.transaction((): boolean => { | |
| 212 | const before = getCard(boardId, cardId); | |
| 213 | if (!before) return false; | |
| 214 | db.query("UPDATE card SET title = ?2, updated_at = datetime('now') WHERE id = ?1").run(cardId, title); | |
| 215 | logAction(boardId, { restoreCards: [before] }); | |
| 216 | return true; | |
| 217 | })(); | |
| 203 | 218 | } |
| 204 | 219 | |
| 205 | 220 | // Moves a card into a column at a 0-based index among that column's |
| 206 | 221 | // other cards (as currently ordered), computing a fresh fractional |
| 207 | // position from its new neighbours. | |
| 222 | // position from its new neighbours. When the neighbours are too close | |
| 223 | // for a midpoint to land strictly between them, the whole column is | |
| 224 | // renormalised to 1..n first — logged alongside the move so undo puts | |
| 225 | // every row back exactly where it was. | |
| 208 | 226 | export function moveCard(boardId: string, cardId: string, columnId: string, index: number): boolean { |
| 209 | const before = getCard(boardId, cardId); | |
| 210 | const column = getColumn(boardId, columnId); | |
| 211 | if (!before || !column) return false; | |
| 212 | const siblings = listCards(boardId).filter((c) => c.column_id === columnId && c.id !== cardId); | |
| 213 | const at = Math.max(0, Math.min(index, siblings.length)); | |
| 214 | const prev = siblings[at - 1]; | |
| 215 | const next = siblings[at]; | |
| 216 | const position = !prev && !next ? 1 : !prev ? next.position - 1 : !next ? prev.position + 1 : (prev.position + next.position) / 2; | |
| 217 | db.query("UPDATE card SET column_id = ?2, position = ?3, updated_at = datetime('now') WHERE id = ?1").run( | |
| 218 | cardId, | |
| 219 | columnId, | |
| 220 | position, | |
| 221 | ); | |
| 222 | logAction(boardId, { restoreCards: [before] }); | |
| 223 | return true; | |
| 227 | return db.transaction((): boolean => { | |
| 228 | const before = getCard(boardId, cardId); | |
| 229 | const column = getColumn(boardId, columnId); | |
| 230 | if (!before || !column) return false; | |
| 231 | const siblings = listCards(boardId).filter((c) => c.column_id === columnId && c.id !== cardId); | |
| 232 | const at = Math.max(0, Math.min(index, siblings.length)); | |
| 233 | const prev = siblings[at - 1]; | |
| 234 | const next = siblings[at]; | |
| 235 | const position = !prev && !next ? 1 : !prev ? next.position - 1 : !next ? prev.position + 1 : (prev.position + next.position) / 2; | |
| 236 | ||
| 237 | const collapsed = !Number.isFinite(position) || (prev !== undefined && position <= prev.position) || (next !== undefined && position >= next.position); | |
| 238 | if (!collapsed) { | |
| 239 | db.query("UPDATE card SET column_id = ?2, position = ?3, updated_at = datetime('now') WHERE id = ?1").run( | |
| 240 | cardId, | |
| 241 | columnId, | |
| 242 | position, | |
| 243 | ); | |
| 244 | logAction(boardId, { restoreCards: [before] }); | |
| 245 | return true; | |
| 246 | } | |
| 247 | ||
| 248 | const ordered = [...siblings.slice(0, at), before, ...siblings.slice(at)]; | |
| 249 | const restoreCards: Card[] = [before]; | |
| 250 | const renormalize = db.query("UPDATE card SET position = ?2 WHERE id = ?1"); | |
| 251 | ordered.forEach((c, i) => { | |
| 252 | if (c.id === cardId) return; | |
| 253 | restoreCards.push({ ...c }); | |
| 254 | renormalize.run(c.id, i + 1); | |
| 255 | }); | |
| 256 | db.query("UPDATE card SET column_id = ?2, position = ?3, updated_at = datetime('now') WHERE id = ?1").run( | |
| 257 | cardId, | |
| 258 | columnId, | |
| 259 | at + 1, | |
| 260 | ); | |
| 261 | logAction(boardId, { restoreCards }); | |
| 262 | return true; | |
| 263 | })(); | |
| 224 | 264 | } |
| 225 | 265 | |
| 226 | 266 | export function deleteCard(boardId: string, cardId: string): boolean { |
| 227 | const before = getCard(boardId, cardId); | |
| 228 | if (!before) return false; | |
| 229 | db.query("DELETE FROM card WHERE id = ?1").run(cardId); | |
| 230 | logAction(boardId, { restoreCards: [before] }); | |
| 231 | return true; | |
| 267 | return db.transaction((): boolean => { | |
| 268 | const before = getCard(boardId, cardId); | |
| 269 | if (!before) return false; | |
| 270 | db.query("DELETE FROM card WHERE id = ?1").run(cardId); | |
| 271 | logAction(boardId, { restoreCards: [before] }); | |
| 272 | return true; | |
| 273 | })(); | |
| 232 | 274 | } |
| 233 | 275 | |
| 234 | 276 | export function undo(boardId: string): boolean { |
| 235 | const row = db | |
| 236 | .query<{ id: number; payload: string }, [string]>("SELECT id, payload FROM action WHERE board_id = ?1 ORDER BY id DESC LIMIT 1") | |
| 237 | .get(boardId); | |
| 238 | if (!row) return false; | |
| 239 | const payload = JSON.parse(row.payload) as Payload; | |
| 240 | const restoreColumn = db.query( | |
| 241 | "INSERT OR REPLACE INTO board_column (id, board_id, name, position) VALUES (?1, ?2, ?3, ?4)", | |
| 242 | ); | |
| 243 | const restoreCard = db.query( | |
| 244 | "INSERT OR REPLACE INTO card (id, board_id, column_id, title, position, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", | |
| 245 | ); | |
| 246 | db.transaction(() => { | |
| 277 | return db.transaction((): boolean => { | |
| 278 | const row = db | |
| 279 | .query<{ id: number; payload: string }, [string]>("SELECT id, payload FROM action WHERE board_id = ?1 ORDER BY id DESC LIMIT 1") | |
| 280 | .get(boardId); | |
| 281 | if (!row) return false; | |
| 282 | const payload = JSON.parse(row.payload) as Payload; | |
| 283 | const restoreColumn = db.query( | |
| 284 | "INSERT OR REPLACE INTO board_column (id, board_id, name, position) VALUES (?1, ?2, ?3, ?4)", | |
| 285 | ); | |
| 286 | const restoreCard = db.query( | |
| 287 | "INSERT OR REPLACE INTO card (id, board_id, column_id, title, position, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", | |
| 288 | ); | |
| 247 | 289 | for (const c of payload.restoreColumns ?? []) restoreColumn.run(c.id, c.board_id, c.name, c.position); |
| 248 | 290 | for (const c of payload.restoreCards ?? []) restoreCard.run(c.id, c.board_id, c.column_id, c.title, c.position, c.created_at, c.updated_at); |
| 249 | 291 | for (const id of payload.deleteColumnIds ?? []) db.query("DELETE FROM board_column WHERE id = ?1").run(id); |
| 250 | 292 | for (const id of payload.deleteCardIds ?? []) db.query("DELETE FROM card WHERE id = ?1").run(id); |
| 251 | 293 | db.query("DELETE FROM action WHERE id = ?1").run(row.id); |
| 294 | return true; | |
| 252 | 295 | })(); |
| 253 | return true; | |
| 254 | 296 | } |
new above
offline