Public: anyone can read this; the crew can change it.
A database left at any earlier schema boots on main with its deals intact, and the clock, a deal page and People answer 200 on it, checked by a test for every past version #155
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/151-a-database-left-at-any-earlier-schema-bo → main 2 commits fork
3 files
changed +187
−1
AGENTS.md +1 −1
| @@ -95,7 +95,7 @@ To add a concept: | ||
| 95 | 95 | resource's row in `contract.json`, its door in `src/contract.test.ts`, |
| 96 | 96 | and a test in `src/actions.test.ts` for what the table cannot say. |
| 97 | 97 | - **A migration** — append to `migrations` in `src/db.ts`. Never edit one |
| 98 | that has run. `bun run src/db.ts --check` shows the version. | |
| 98 | that has run. `bun run src/db.ts --check` shows the version. `src/upgrade.test.ts` rebuilds a database at every earlier version (each of the first n migrations, with a deal, party, date and reminder in that version's columns) and boots it; a new migration must keep it green, so a data migration has to survive rows written before it. | |
| 99 | 99 | - **A page** — a row in `src/routes.ts` and a function in `src/pages/`, |
| 100 | 100 | wrapped in the one `layout` (`src/pages/layout.ts`). A form posts, and |
| 101 | 101 | comes back to the page it names in `to` (checked by `backTo`). |
src/upgrade.history.json added +52
Diff not shown — a line 2,467 characters wide (limit 2,000). +52
View filesrc/upgrade.test.ts added +134
| @@ -0,0 +1,134 @@ | ||
| 1 | import { afterAll, expect, test } from "bun:test"; | |
| 2 | import { Database } from "bun:sqlite"; | |
| 3 | import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { migrations } from "./db.ts"; | |
| 7 | import { serve } from "./server.ts"; | |
| 8 | import { sign } from "./who.ts"; | |
| 9 | import pinned from "./migrations.pin.json"; | |
| 10 | import earlier from "./upgrade.history.json"; | |
| 11 | ||
| 12 | // A database stopped at any earlier schema comes up whole: its deal, party, | |
| 13 | // date and reminder survive and the pages that read them answer 200. | |
| 14 | // Rows are written from PRAGMA table_info, so they hold only the columns | |
| 15 | // that existed at that version, never today's insert helpers. | |
| 16 | ||
| 17 | const KEY = "upgrade-test-key"; | |
| 18 | const root = mkdtempSync(join(tmpdir(), "app-upgrade-")); | |
| 19 | afterAll(() => rmSync(root, { recursive: true, force: true })); | |
| 20 | ||
| 21 | const ADDRESS = "7 Old Version Road"; | |
| 22 | // Values by column name; any other required column falls back by its type. | |
| 23 | const known: Record<string, Record<string, string | number>> = { | |
| 24 | deal: { address: ADDRESS, contract_date: "2026-10-01" }, | |
| 25 | party: { role: "buyer", name: "Pat Buyer", contact: "[email protected]" }, | |
| 26 | deadline: { kind: "inspection", label: "Inspection", due: "2026-10-12", owes: "buyer" }, | |
| 27 | reminder: { moment: "before", due: "2026-10-12", label: "Inspection", party_name: "Pat Buyer", by_name: "Ana" }, | |
| 28 | }; | |
| 29 | ||
| 30 | function write(db: Database, table: string, extra: Record<string, string | number> = {}) { | |
| 31 | const columns = db | |
| 32 | .query<{ name: string; type: string; notnull: number; dflt_value: string | null; pk: number }, []>(`PRAGMA table_info(${table})`) | |
| 33 | .all(); | |
| 34 | if (columns.length === 0) return false; | |
| 35 | const values: Record<string, string | number> = {}; | |
| 36 | for (const c of columns) { | |
| 37 | if (c.pk === 1) continue; | |
| 38 | const given = extra[c.name] ?? known[table]?.[c.name]; | |
| 39 | if (given !== undefined) values[c.name] = given; | |
| 40 | else if (c.notnull === 1 && c.dflt_value === null) values[c.name] = c.type.toUpperCase().includes("INT") ? 1 : "x"; | |
| 41 | } | |
| 42 | const names = Object.keys(values); | |
| 43 | db.query(`INSERT INTO ${table} (${names.join(", ")}) VALUES (${names.map((_, i) => `?${i + 1}`).join(", ")})`).run(...Object.values(values)); | |
| 44 | return true; | |
| 45 | } | |
| 46 | ||
| 47 | function build(list: string[], n: number, name: string): { dir: string; has: Set<string> } { | |
| 48 | const has = new Set<string>(); | |
| 49 | const dir = join(root, name); | |
| 50 | mkdirSync(dir, { recursive: true }); | |
| 51 | const db = new Database(join(dir, "app.db")); | |
| 52 | db.exec("PRAGMA foreign_keys = ON"); | |
| 53 | db.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))"); | |
| 54 | list.slice(0, n).forEach((sql, i) => { | |
| 55 | try { | |
| 56 | db.exec(sql); | |
| 57 | } catch (error) { | |
| 58 | // As migrate tolerates it: a column some history already added. | |
| 59 | if (!(/^ALTER TABLE \w+ ADD COLUMN/.test(sql) && String(error).includes("duplicate column name"))) throw error; | |
| 60 | } | |
| 61 | db.query("INSERT INTO schema_version (version) VALUES (?1)").run(i + 1); | |
| 62 | }); | |
| 63 | if (write(db, "deal", { author_id: "u1", author_name: "Ana" })) { | |
| 64 | has.add("deal"); | |
| 65 | if (write(db, "party", { deal_id: 1 })) has.add("party"); | |
| 66 | if (write(db, "deadline", { deal_id: 1 })) has.add("deadline"); | |
| 67 | if (has.has("party") && has.has("deadline") && write(db, "reminder", { deal_id: 1, deadline_id: 1, party_id: 1 })) has.add("reminder"); | |
| 68 | } | |
| 69 | db.close(); | |
| 70 | return { dir, has }; | |
| 71 | } | |
| 72 | ||
| 73 | /** Every table's columns, as table_info reports them: what the code reads. */ | |
| 74 | function shape(db: Database): Record<string, string[]> { | |
| 75 | const out: Record<string, string[]> = {}; | |
| 76 | const tables = db.query<{ name: string }, []>("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name <> 'schema_version' ORDER BY name").all(); | |
| 77 | for (const { name } of tables) | |
| 78 | out[name] = db.query<{ name: string; type: string; notnull: number }, []>(`PRAGMA table_info(${name})`).all().map((c) => `${c.name} ${c.type} ${c.notnull}`).sort(); | |
| 79 | return out; | |
| 80 | } | |
| 81 | const fresh = new Database(":memory:"); | |
| 82 | migrations.forEach((sql) => { | |
| 83 | try { | |
| 84 | fresh.exec(sql); | |
| 85 | } catch (error) { | |
| 86 | if (!String(error).includes("duplicate column name")) throw error; | |
| 87 | } | |
| 88 | }); | |
| 89 | const wanted = shape(fresh); | |
| 90 | ||
| 91 | const identity = `u1|ana|user|${Date.now() + 600_000}`; | |
| 92 | const headers = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 93 | ||
| 94 | async function boots(list: string[], n: number, name: string) { | |
| 95 | { | |
| 96 | const { dir, has } = build(list, n, name); | |
| 97 | const hadDeal = has.has("deal"); | |
| 98 | const server = serve({ port: 0, dataDir: dir, identityKey: KEY }); | |
| 99 | try { | |
| 100 | const base = `http://${server.hostname}:${server.port}`; | |
| 101 | const get = (path: string) => fetch(base + path, { headers }); | |
| 102 | const check = new Database(join(dir, "app.db"), { readonly: true }); | |
| 103 | expect(check.query<{ v: number }, []>("SELECT max(version) AS v FROM schema_version").get()!.v).toBe(migrations.length); | |
| 104 | const deal = check.query<{ id: number; address: string; contract_date: string }, []>("SELECT id, address, contract_date FROM deal WHERE id = 1 AND address = '" + ADDRESS + "'").get(); | |
| 105 | if (hadDeal) { | |
| 106 | expect(deal).toEqual({ id: 1, address: ADDRESS, contract_date: "2026-10-01" }); | |
| 107 | if (has.has("party")) expect(check.query("SELECT name FROM party WHERE deal_id = 1").get()).toEqual({ name: "Pat Buyer" }); | |
| 108 | if (has.has("deadline")) expect(check.query("SELECT label, due FROM deadline WHERE deal_id = 1 AND kind = 'inspection'").get()).toEqual({ label: "Inspection", due: "2026-10-12" }); | |
| 109 | if (has.has("reminder")) expect(check.query("SELECT count(*) AS c FROM reminder WHERE deal_id = 1").get()).toEqual({ c: 1 }); | |
| 110 | } | |
| 111 | expect(shape(check), `schema at version ${n} differs from a fresh database`).toEqual(wanted); | |
| 112 | check.close(); | |
| 113 | const pages = ["/", "/people"]; | |
| 114 | if (hadDeal) pages.push("/deals/1", "/deals/1/timeline"); | |
| 115 | for (const path of pages) { | |
| 116 | const response = await get(path); | |
| 117 | expect(response.status, `${path} at version ${n}`).toBe(200); | |
| 118 | const text = await response.text(); | |
| 119 | if (hadDeal && path !== "/people") expect(text).toContain(ADDRESS); | |
| 120 | } | |
| 121 | } finally { | |
| 122 | server.stop(true); | |
| 123 | } | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | for (let n = 1; n <= pinned.length; n++) | |
| 128 | test(`a database left at migration ${n} of ${pinned.length} boots with its deal and the pages answer 200`, () => boots(migrations, n, `v${n}`)); | |
| 129 | ||
| 130 | // A database written by code whose list is NOT a prefix of today's: src/upgrade.history.json is | |
| 131 | // the migrations as they stood before the Profile line was inserted mid-list (601f210^). Production | |
| 132 | // held such a database, and a boot that only runs the entries past its version skips the line column. | |
| 133 | test("a database made by the migration list from before the Profile line is brought whole, though today's list is not an extension of it", () => | |
| 134 | boots(earlier, earlier.length, "history")); |
new above
offline