Public: anyone can read this; the crew can change it.
A boot on a database the first paint cannot read says which health check fails, and why, in the app's log #207
mason/deal-clock:mason/205-a-boot-on-a-database-the-first-paint-can → main 3 commits fork
6 files
changed +130
−6
src/db.ts +33
| @@ -572,6 +572,39 @@ export const migrations: string[] = [ | ||
| 572 | 572 | // An inspection or a final walkthrough is an appointment: an optional time and place, like the closing's. |
| 573 | 573 | `ALTER TABLE deadline ADD COLUMN time TEXT CHECK (time IS NULL OR time GLOB '[0-2][0-9]:[0-5][0-9]')`, |
| 574 | 574 | `ALTER TABLE deadline ADD COLUMN place TEXT CHECK (place IS NULL OR length(place) BETWEEN 1 AND 200)`, |
| 575 | // Repair: the closing's time and place were added mid-list, so a database already past that | |
| 576 | // position never got them and its first paint fails. Tolerated as a duplicate where they exist. | |
| 577 | `ALTER TABLE deal ADD COLUMN closing_time TEXT CHECK (closing_time IS NULL OR closing_time GLOB '[0-2][0-9]:[0-5][0-9]')`, | |
| 578 | `ALTER TABLE deal ADD COLUMN closing_place TEXT CHECK (closing_place IS NULL OR length(closing_place) BETWEEN 1 AND 200)`, | |
| 579 | // Repair, whole: a database whose version ran ahead of migrations inserted mid-list may lack any | |
| 580 | // column added by a one-line ALTER. Each is tolerated as a duplicate where it exists. | |
| 581 | `ALTER TABLE reminder ADD COLUMN subject TEXT NOT NULL DEFAULT ''`, | |
| 582 | `ALTER TABLE reminder ADD COLUMN body TEXT NOT NULL DEFAULT ''`, | |
| 583 | `ALTER TABLE deadline_move ADD COLUMN notify INTEGER NOT NULL DEFAULT 0`, | |
| 584 | `ALTER TABLE deadline_move ADD COLUMN amendment INTEGER NOT NULL DEFAULT 0`, | |
| 585 | `ALTER TABLE deadline ADD COLUMN lead_days INTEGER NOT NULL DEFAULT 3 CHECK (lead_days BETWEEN 1 AND 30)`, | |
| 586 | `ALTER TABLE audit ADD COLUMN what TEXT`, | |
| 587 | `ALTER TABLE deadline ADD COLUMN waived_at TEXT`, | |
| 588 | `ALTER TABLE deadline ADD COLUMN waived_by TEXT`, | |
| 589 | `ALTER TABLE deadline ADD COLUMN owes_from TEXT`, | |
| 590 | `ALTER TABLE deadline ADD COLUMN owes_by TEXT`, | |
| 591 | `ALTER TABLE deadline ADD COLUMN owes_on TEXT`, | |
| 592 | `ALTER TABLE deadline ADD COLUMN note TEXT NOT NULL DEFAULT ''`, | |
| 593 | `ALTER TABLE deal ADD COLUMN fell_at TEXT`, | |
| 594 | `ALTER TABLE deal ADD COLUMN fell_reason TEXT`, | |
| 595 | `ALTER TABLE deal ADD COLUMN fell_by TEXT`, | |
| 596 | `ALTER TABLE reminder_hold ADD COLUMN subject TEXT NOT NULL DEFAULT ''`, | |
| 597 | `ALTER TABLE reminder_hold ADD COLUMN body TEXT NOT NULL DEFAULT ''`, | |
| 598 | `ALTER TABLE deal ADD COLUMN sale_price INTEGER CHECK (sale_price IS NULL OR sale_price BETWEEN 0 AND 100000000)`, | |
| 599 | `ALTER TABLE deal ADD COLUMN earnest_amount INTEGER CHECK (earnest_amount IS NULL OR earnest_amount BETWEEN 0 AND 100000000)`, | |
| 600 | `ALTER TABLE party ADD COLUMN email TEXT NOT NULL DEFAULT ''`, | |
| 601 | `ALTER TABLE party ADD COLUMN phone TEXT NOT NULL DEFAULT ''`, | |
| 602 | `ALTER TABLE deal ADD COLUMN side TEXT NOT NULL DEFAULT 'buyer' CHECK (side IN ('buyer', 'listing', 'both'))`, | |
| 603 | `ALTER TABLE deal ADD COLUMN commission_rate INTEGER CHECK (commission_rate IS NULL OR commission_rate BETWEEN 0 AND 1000)`, | |
| 604 | `ALTER TABLE profile ADD COLUMN zone TEXT NOT NULL DEFAULT 'America/New_York' CHECK (length(zone) BETWEEN 1 AND 64)`, | |
| 605 | `ALTER TABLE deal ADD COLUMN cash INTEGER NOT NULL DEFAULT 0 CHECK (cash IN (0, 1))`, | |
| 606 | `ALTER TABLE deadline ADD COLUMN time TEXT CHECK (time IS NULL OR time GLOB '[0-2][0-9]:[0-5][0-9]')`, | |
| 607 | `ALTER TABLE deadline ADD COLUMN place TEXT CHECK (place IS NULL OR length(place) BETWEEN 1 AND 200)`, | |
| 575 | 608 | ]; |
| 576 | 609 | |
| 577 | 610 | export function openDb(dir = dataDir): Database { |
src/health.test.ts +14
| @@ -2,6 +2,7 @@ import { expect, test } from "bun:test"; | ||
| 2 | 2 | import { Database } from "bun:sqlite"; |
| 3 | 3 | import { migrate, migrations } from "./db.ts"; |
| 4 | 4 | import { routes } from "./routes.ts"; |
| 5 | import { failedCheckDetail } from "./health.ts"; | |
| 5 | 6 | |
| 6 | 7 | const handler = routes.find((r) => r.path === "/healthz")!.handle as (c: unknown) => Response; |
| 7 | 8 | const ask = (db: Database) => handler({ db }); |
| @@ -24,6 +25,7 @@ test("a database missing profile.line answers 503 naming profile, and nothing el | ||
| 24 | 25 | const db = new Database(":memory:"); |
| 25 | 26 | db.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))"); |
| 26 | 27 | migrations |
| 28 | .slice(0, -29) | |
| 27 | 29 | .filter((m) => !/profile ADD COLUMN line/.test(m)) |
| 28 | 30 | .forEach((sql, i) => { |
| 29 | 31 | db.exec(sql); |
| @@ -49,3 +51,15 @@ test("a database missing a table the waiting count reads answers 503 naming wait | ||
| 49 | 51 | expect(response.status).toBe(503); |
| 50 | 52 | expect(((await response.json()) as { failed: string }).failed).toBe("waiting"); |
| 51 | 53 | }); |
| 54 | ||
| 55 | test("the failed-check detail names the check and the database's own message; a fresh database has none", () => { | |
| 56 | const fresh = new Database(":memory:"); | |
| 57 | migrate(fresh); | |
| 58 | expect(failedCheckDetail(fresh)).toBeNull(); | |
| 59 | const db = new Database(":memory:"); | |
| 60 | migrate(db); | |
| 61 | db.exec("ALTER TABLE deadline DROP COLUMN place"); | |
| 62 | const detail = failedCheckDetail(db); | |
| 63 | expect(detail?.name).toBe("dates"); | |
| 64 | expect(detail?.message).toMatch(/no such column: place/); | |
| 65 | }); |
src/health.ts +13 −5
| @@ -24,15 +24,23 @@ const CHECKS: readonly (readonly [name: string, sql: string])[] = [ | ||
| 24 | 24 | ], |
| 25 | 25 | ]; |
| 26 | 26 | |
| 27 | /** The name of the first check that throws, else null. Never the error: it can carry SQL. */ | |
| 28 | export function failedCheck(db: Database): string | null { | |
| 27 | /** | |
| 28 | * The first check that throws, with the database's own message, else null. | |
| 29 | * For the log only: the message can carry SQL and must never reach an HTTP answer. | |
| 30 | */ | |
| 31 | export function failedCheckDetail(db: Database): { name: string; message: string } | null { | |
| 29 | 32 | for (const [name, sql] of CHECKS) { |
| 30 | 33 | try { |
| 31 | 34 | db.query(sql).get(); |
| 32 | } catch { | |
| 33 | // Neither the error nor the SQL leaves: the answer names the check and nothing else. | |
| 34 | return name; | |
| 35 | } catch (error) { | |
| 36 | return { name, message: error instanceof Error ? error.message : String(error) }; | |
| 35 | 37 | } |
| 36 | 38 | } |
| 37 | 39 | return null; |
| 38 | 40 | } |
| 41 | ||
| 42 | /** The name of the first check that throws, else null. Never the error: it can carry SQL. */ | |
| 43 | export function failedCheck(db: Database): string | null { | |
| 44 | // Neither the error nor the SQL leaves: the answer names the check and nothing else. | |
| 45 | return failedCheckDetail(db)?.name ?? null; | |
| 46 | } |
src/migrations.pin.json +29
| @@ -57,5 +57,34 @@ | ||
| 57 | 57 | "7e011fb3dab2123101aa981ef9050d8f8d2c34fb3ba137ecc832f86d464b98a2", |
| 58 | 58 | "429a2adfb36ff61def2d1979556a9db87f4758663fa9c56b97e599b3bc13b773", |
| 59 | 59 | "c5df0d263e04d4e07f9df383beb57f033966af540bb228d9bbbd7b57f3620408", |
| 60 | "4a52d9de76ede6d34d831d9bc6fc1d3d3a501a5d3652bf63695a9de49f2a7d39", | |
| 61 | "7255f8baa8700202884be4fbfbd925383182f00a049f8fe49df190cabbf90632", | |
| 62 | "cf6972801626419644e27b9bc4e6d338367e1c6fe67bb5ca338dc0c9bb408fc1", | |
| 63 | "b9200d9459a6e60206a98c6de4618e6ffeea4126b74c8aa5d7ef0561f9f052e4", | |
| 64 | "23415bdc4bee35d8d64a003f96fb6aefa0980e9e5047b16326adb7fdf2f541f2", | |
| 65 | "2569347b7f614579d909ce927f8b08788f88d65f745b63ddf8de8d11ee63fb02", | |
| 66 | "1efe0c811b2f7046249fdba4f1629fb74f6754937917b5c8624cba4c196cdac4", | |
| 67 | "7d15b45bb6b20cc4b0b8a2f4f5fc06a6d9436017d9b04642ada60dfc4396aa80", | |
| 68 | "3f108f94a689e99698937f8329e65ddd129ca6d7046f97ddd354db39c2635b4b", | |
| 69 | "7f8986d8e50e0e8366d4f67d58b3121032ec6e32fa594659b1bd42c5fc79d0de", | |
| 70 | "26d27c436e6fc94690d0cbea29af62adfdf4360eca50940bd58bd63e11bb2a29", | |
| 71 | "ac0af3e527d87fe393f7dd06607754668e88e655386adbee973a0604cea25b7a", | |
| 72 | "3175f6484390097d68daac7f987e9461d10a7b4f4b85216c0eebdb9a02d0c714", | |
| 73 | "fcf79269e83bc0d9c0e6fd9bb4b0e2c29676ca2674e3555914bc76f8b0653d78", | |
| 74 | "484e2ce356a18a28f5878b33be3f50dcbb0adbc09c35f37cef438bef0aaea9c5", | |
| 75 | "5f4bf810d1c3d703865b29f0bbab9f16c064fa2054a7ceaa7eb2c5f4041811d7", | |
| 76 | "8a2d7d68ce199e88441da7d5eb8a4780f4d1cc5caadeea9b2c3bd8dc1886dcfd", | |
| 77 | "1543897fd9b75e65d7727e3c6f6ddf0990a1e925e08e1e145ecc2d54dba0767a", | |
| 78 | "b4ae3234ae229a16146d06fc391217a838e862ea25a418a2eb0f63a15a49eb62", | |
| 79 | "db97a5da0b4aba4612841222fdae50fc27fff51383f74c5d525ab9bf161fccc6", | |
| 80 | "dec7a2e2f471dd1b426da3cd890af77e2852fb71268bb3c8271c66e3d72971f2", | |
| 81 | "d08fe630a2ddaacc08d5945dc67b5103052a9096ac5a27aca68bc7faa66ef70d", | |
| 82 | "7ef5159c0e2d5ae4df22f23263024f8f70298e935112d1042cf1ee1224ca2f23", | |
| 83 | "9e45e8c8bd82eab1cae6ffdba0e122be3831f8beebc157941490811897505c0c", | |
| 84 | "fd1b67b66fb8ff34326ca5cb86ab04678619dfea420f21d8f85b2d05c999907f", | |
| 85 | "f836a1da11c1f51a255301c4ee62c441612baada0345c1310f4149e5d5ccf5b9", | |
| 86 | "3fbd9e1851d9bc86a3a4c5adfc9f2300712c450b66a87d253cfcb91586cbbe25", | |
| 87 | "7e011fb3dab2123101aa981ef9050d8f8d2c34fb3ba137ecc832f86d464b98a2", | |
| 88 | "c5df0d263e04d4e07f9df383beb57f033966af540bb228d9bbbd7b57f3620408", | |
| 60 | 89 | "4a52d9de76ede6d34d831d9bc6fc1d3d3a501a5d3652bf63695a9de49f2a7d39" |
| 61 | 90 | ] |
src/migrations.test.ts +23 −1
| @@ -2,6 +2,7 @@ import { expect, test } from "bun:test"; | ||
| 2 | 2 | import { Database } from "bun:sqlite"; |
| 3 | 3 | import { migrate, migrations } from "./db.ts"; |
| 4 | 4 | import { agentOf } from "./profile.ts"; |
| 5 | import { failedCheck } from "./health.ts"; | |
| 5 | 6 | import pinned from "./migrations.pin.json"; |
| 6 | 7 | |
| 7 | 8 | const hash = (sql: string) => new Bun.CryptoHasher("sha256").update(sql).digest("hex"); |
| @@ -13,7 +14,7 @@ test("every migration is pinned by position; a pull may only append", () => { | ||
| 13 | 14 | }); |
| 14 | 15 | |
| 15 | 16 | test("a database made before the Profile line gets the column and reads the agent", () => { |
| 16 | const before = migrations.filter((m) => !/profile ADD COLUMN line|commission_rate|profile ADD COLUMN zone|ADD COLUMN cash|'anniversary'|deadline ADD COLUMN (time|place)/.test(m)); | |
| 17 | const before = migrations.slice(0, -29).filter((m) => !/profile ADD COLUMN line|commission_rate|profile ADD COLUMN zone|ADD COLUMN cash|'anniversary'|deadline ADD COLUMN (time|place)/.test(m)); | |
| 17 | 18 | const db = new Database(":memory:"); |
| 18 | 19 | db.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))"); |
| 19 | 20 | before.forEach((sql, i) => { |
| @@ -52,3 +53,24 @@ test("a database at every earlier version boots with its deals unchanged and cas | ||
| 52 | 53 | expect(rows, `version ${version}`).toEqual(seeded ? [{ address: "1 Old St", cash: 0 }] : []); |
| 53 | 54 | } |
| 54 | 55 | }); |
| 56 | ||
| 57 | test("a database past the closing time and place, but never given them, is repaired to a healthy first paint", () => { | |
| 58 | const db = new Database(":memory:"); | |
| 59 | migrate(db); | |
| 60 | db.exec("ALTER TABLE deal DROP COLUMN closing_time"); | |
| 61 | db.exec("ALTER TABLE deal DROP COLUMN closing_place"); | |
| 62 | db.exec("DELETE FROM schema_version WHERE version > (SELECT max(version) - 29 FROM schema_version)"); | |
| 63 | expect(failedCheck(db)).toBe("deals"); | |
| 64 | migrate(db); | |
| 65 | expect(failedCheck(db)).toBeNull(); | |
| 66 | }); | |
| 67 | ||
| 68 | test("a database that skipped the date lead is repaired too", () => { | |
| 69 | const db = new Database(":memory:"); | |
| 70 | migrate(db); | |
| 71 | db.exec("ALTER TABLE deadline DROP COLUMN lead_days"); | |
| 72 | db.exec("DELETE FROM schema_version WHERE version > (SELECT max(version) - 29 FROM schema_version)"); | |
| 73 | expect(failedCheck(db)).toBe("dates"); | |
| 74 | migrate(db); | |
| 75 | expect(failedCheck(db)).toBeNull(); | |
| 76 | }); |
src/server.ts +18
| @@ -7,8 +7,11 @@ | ||
| 7 | 7 | * serves what the forge said; a test calls `serve` with its own. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { Database } from "bun:sqlite"; | |
| 11 | import { join } from "node:path"; | |
| 10 | 12 | import { openDb } from "./db.ts"; |
| 11 | 13 | import { dataDir, identityKey, title, where } from "./env.ts"; |
| 14 | import { failedCheckDetail } from "./health.ts"; | |
| 12 | 15 | import { html, sourceBytes, text } from "./http.ts"; |
| 13 | 16 | import { follow } from "./live.ts"; |
| 14 | 17 | import { match, notFound } from "./routes.ts"; |
| @@ -123,4 +126,19 @@ if (import.meta.main) { | ||
| 123 | 126 | console.log( |
| 124 | 127 | `${title} listening on http://${server.hostname}:${server.port} (${where})`, |
| 125 | 128 | ); |
| 129 | // The same checks /healthz runs, once, so the cause of a 503 is in the log. | |
| 130 | // A read-only connection: this never writes. Row data is never logged. | |
| 131 | try { | |
| 132 | const probe = new Database(join(dataDir, "app.db"), { readonly: true }); | |
| 133 | try { | |
| 134 | const failed = failedCheckDetail(probe); | |
| 135 | if (failed !== null) { | |
| 136 | console.error(`${title}: health check "${failed.name}" fails at boot: ${failed.message}`); | |
| 137 | } | |
| 138 | } finally { | |
| 139 | probe.close(); | |
| 140 | } | |
| 141 | } catch (error) { | |
| 142 | console.error(`${title}: health check could not run at boot: ${error instanceof Error ? error.message : error}`); | |
| 143 | } | |
| 126 | 144 | } |
new above
offline