Pre-this-commit, the Hetzner DB had ALL 26 historical migrations applied
(via drizzle-kit push during the v0.6 cutover, task #15) but ZERO rows in
drizzle.__drizzle_migrations because push doesn't write the ledger. As a
result drizzle-kit migrate would have tried to re-apply every migration
from scratch on its first run — failing on the first CREATE TABLE for an
already-existing table, OR worse, silently producing drift if any IF NOT
EXISTS / IF EXISTS guards saved the run.
This commit closes that gap permanently.
NEW scripts/db/bootstrap-migration-ledger.ts (175 lines):
Reads all drizzle/NNNN_*.sql files. Computes sha256 of each (drizzle's
canonical hash format). Modes:
--dry-run (default) Print the SQL it would apply
--emit-sql Print to stdout (pipe to psql)
--write-journal Update drizzle/meta/_journal.json locally
--apply Run against DATABASE_URL via psql -f
Idempotent (uses ON CONFLICT DO NOTHING). Safe to re-run.
APPLIED to prod (postgresqlbridge.orangecat.ch):
CREATE SCHEMA IF NOT EXISTS drizzle;
CREATE TABLE IF NOT EXISTS drizzle.__drizzle_migrations (id, hash, created_at);
CREATE UNIQUE INDEX __drizzle_migrations_hash_idx ON (hash);
INSERT one row per migration (26 total) with computed sha256.
Verified: SELECT count(*) FROM drizzle.__drizzle_migrations → 26.
WROTE drizzle/meta/_journal.json:
26 entries, monotonic 'when' timestamps starting 2026-01-01.
Future 'drizzle-kit generate' calls will append entries on top;
'drizzle-kit migrate' will see existing entries' hashes match the
ledger and skip them, applying only new migrations.
GOING FORWARD:
Schema changes: edit src/db/schema/*.ts → drizzle-kit generate → review
the new drizzle/NNNN_*.sql → drizzle-kit migrate to apply.
No more direct 'drizzle-kit push' to prod (kept available for dev DBs).
No more manual 'psql -f drizzle/NNNN_*.sql' shenanigans.
Tech debt this resolves:
- Drift detection: works now. drizzle-kit migrate exits 0 if and only
if the ledger matches the file hashes.
- CI: future deploy pipelines can run drizzle-kit migrate as a step
and have confidence in the result.
- Team scaling: someone other than me can apply migrations without
'how does this DB actually get migrated' tribal knowledge.
Verified: tsc clean, 19/19 control-presenter tests pass. Prod DB
unaffected outside the new drizzle schema.