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 #213

opened this

Merged 24d 5h ago, live in production

This pull logs which health check fails at boot and why, and it repairs a database whose schema is behind its version. It is untested against the real preview database, which I can't see. My first two fixes were guesses at one missing column at a time.

Notes

opened this

This pull logs which health check fails at boot and why, and it repairs a database whose schema is behind its version. It is untested against the real preview database, which I can't see.

My first two fixes were guesses at one missing column at a time. The preview's database had skipped whole migrations (a migration inserted mid-list leaves a database already past that position without its columns). Each deploy then surfaced the next missing column. So I replaced the guessing with a general repair.

What changed

  • Boot diagnosis (the issue's scope):
    • failedCheckDetail(db) in src/health.ts returns {name, message} | null, and failedCheck keeps its signature.
    • src/server.ts runs the checks once at boot on a read-only connection. On a failure it writes deal-clock: health check "<name>" fails at boot: <db message> to stderr and keeps listening.
    • A healthy boot adds no line, and /healthz still answers 503 with the check's name only.
  • reconcile(db) in src/db.ts:
    • It builds the pristine schema in memory and adds any table, index or column the real database lacks.
    • It runs only when failedCheck is non-null, so a healthy boot pays nothing.
    • It only adds, never drops or rewrites, so no row changes. A not-null column with no default is added nullable, as SQLite requires.
  • Repair migrations: I kept the 29 appended one-line ADD COLUMN repairs from the earlier pushes. They are pinned in src/migrations.pin.json as appends only. Dropping them would edit the pins, so I left them. reconcile covers the same ground and more.
  • Tests, all green locally (src/migrations, src/health, src/db, src/upgrade, src/hold, src/deals, src/server and src/terminal test files):
    • A database missing columns and a table, opened with openDb, reads healthy afterwards and keeps its deal and date rows. Its lead_days comes back as 3.
    • The health detail test names dates and no such column: place, and returns null on a fresh database.

Not done

  • reconcile restores columns without their CHECK constraints, and it does not fix a rebuilt table whose old CHECK is wrong. Those need the actual database to see.
  • I ran no deploy. If the preview still fails, the boot line will now name the check and the database's message.
  • No contract cell changes.

To try it After this deploys, /healthz on the preview should answer 200, and a boot on a broken database logs the failing check.

🤖 Generated with Claude Code

Closes #205

1 earlier comment · 1 event
commented

This pull logs which health check fails at boot and why, and it repairs a database whose schema is behind its version. It is untested against the real preview database, which I can't see.

My first two fixes were guesses at one missing column at a time. The preview's database had skipped whole migrations (a migration inserted mid-list leaves a database already past that position without its columns). Each deploy then surfaced the next missing column. So I replaced the guessing with a general repair.

What changed

  • Boot diagnosis (the issue's scope):
    • failedCheckDetail(db) in src/health.ts returns {name, message} | null, and failedCheck keeps its signature.
    • src/server.ts runs the checks once at boot on a read-only connection. On a failure it writes deal-clock: health check "<name>" fails at boot: <db message> to stderr and keeps listening.
    • A healthy boot adds no line, and /healthz still answers 503 with the check's name only.
  • reconcile(db) in src/db.ts:
    • It builds the pristine schema in memory and adds any table, index or column the real database lacks.
    • It runs only when failedCheck is non-null, so a healthy boot pays nothing.
    • It only adds, never drops or rewrites, so no row changes. A not-null column with no default is added nullable, as SQLite requires.
  • Repair migrations: I kept the 29 appended one-line ADD COLUMN repairs from the earlier pushes. They are pinned in src/migrations.pin.json as appends only. Dropping them would edit the pins, so I left them. reconcile covers the same ground and more.
  • Tests, all green locally (src/migrations, src/health, src/db, src/upgrade, src/hold, src/deals, src/server and src/terminal test files):
    • A database missing columns and a table, opened with openDb, reads healthy afterwards and keeps its deal and date rows. Its lead_days comes back as 3.
    • The health detail test names dates and no such column: place, and returns null on a fresh database.

Not done

  • reconcile restores columns without their CHECK constraints, and it does not fix a rebuilt table whose old CHECK is wrong. Those need the actual database to see.
  • I ran no deploy. If the preview still fails, the boot line will now name the check and the database's message.
  • No contract cell changes.

To try it After this deploys, /healthz on the preview should answer 200, and a boot on a broken database logs the failing check.

🤖 Generated with Claude Code

the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

A boot on a database the first paint can't read now writes one line to the app's log. The line names the health check that fails and quotes the database's own error, for example health check "dates" fails at boot: no such column: place. A healthy boot writes nothing. /healthz answers exactly as before: the check's name and never the error text.

What changed

  • src/health.ts: failedCheckDetail(db) returns the failing check's name and the database's message. failedCheck keeps its signature and still returns only the name. The new reportHealthAtBoot(db, title, log) writes the one line when a check fails.
  • src/server.ts: serve calls reportHealthAtBoot on the database openDb just opened. There is no longer a second read-only connection that rebuilt the app.db path, and no code that only runs inside import.meta.main.
  • src/health.test.ts: two tests boot serve on a temporary directory and capture stderr:
    • with deadline.place dropped, stderr holds exactly one line naming dates and no such column: place;
    • on a fresh database, stderr stays empty.

    The first test fails when the call in serve is removed; I checked this by taking it out and running the tests.

Removed at review

  • reconcile() and its call in openDb are gone.
  • So are the 29 appended ADD COLUMN migrations and their pins. src/db.ts, src/migrations.pin.json and src/migrations.test.ts are back to what is on main.
  • The slice(0, -29) workaround in src/health.test.ts is gone with them.

Not done here Repairing a database whose columns don't match the schema version it records is now its own issue, #214. Any repair needs the real database's schema in hand, and has to keep the columns' CHECK constraints.

Checks

  • bun test src/health.test.ts src/migrations.test.ts src/live.test.ts src/db.test.ts: 18 pass, 0 fail.
  • bun x tsc --noEmit did not type-check anything: the repository has no tsconfig.json, so tsc only prints its help text.
  • scripts/gate.ts and scripts/fast.ts don't exist in this repository.
  • Nothing under src/web/ changed, so the design lint wasn't needed.

To try it: in a scratch copy of a data directory, drop a column the first paint reads (e.g. ALTER TABLE deadline DROP COLUMN place) and start bun run src/server.ts with that directory. The log shows the check and the SQLite error, and /healthz answers 503 naming only dates.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs