Skip to content

chore(db): replace init_db.sql with authoritative generated schema.sql#59

Open
MoltyCel wants to merge 1 commit into
mainfrom
chore/schema-of-record
Open

chore(db): replace init_db.sql with authoritative generated schema.sql#59
MoltyCel wants to merge 1 commit into
mainfrom
chore/schema-of-record

Conversation

@MoltyCel
Copy link
Copy Markdown
Owner

Problem

init_db.sql had silently become a non-functional schema-of-record. It
defined 11 tables; the live moltstack DB has 53 (+2 views, 91
indexes, 17 FK constraints). Worse, the 11 shared tables had diverged
structurally — e.g. api_keys carried a phantom id column and was
missing email/rate_limit/tier/label/notes; agents used
public_key vs the live public_key_hex; credentials used a UUID id
vs the live serial.

A fresh instance built from init_db.sql would have crashed on startup.
fork-ci.yml masked this with per-run ALTER TABLE / CREATE TABLE
workarounds (api_keys.email, request_log) instead of fixing the source.

Surfaced during post-merge review of #58.

Change

File What
schema.sql newpg_dump --schema-only of the live DB; authoritative, generated, self-documenting regeneration header
init_db.sql removed — superseded
.github/workflows/fork-ci.yml loads schema.sql with -v ON_ERROR_STOP=1; ALTER/CREATE workarounds dropped
.gitignore !init_db.sql!schema.sql

Verification

  • Dump is self-contained: all 5 FK targets defined; the excluded
    dated legacy table caep_events_legacy_20260415 has no inbound FK or
    view dependency (checked via pg_constraint / pg_depend); 0 legacy
    references remain in the dump.
  • The pytest-run CI job is now the live replay test — it loads
    schema.sql into a fresh postgres:16 container with ON_ERROR_STOP=1,
    so a broken schema-of-record fails the build loudly.

Not touched (intentionally)

migrations/2026-05-14_credit_schema_alignment.sql and
docs/specs/2026-05-14_credit-middleware-schema-alignment.md mention
init_db.sql in historical context — left as-is (applied migration /
dated spec).

Follow-up

schema.sql is generated. Long term it should be regenerated from the
live DB on a cadence, or schema changes should flow through migrations/
and be reflected here — otherwise drift returns.

🤖 Generated with Claude Code

init_db.sql had drifted to 11 tables while the live DB has 53 — and
even the 11 shared tables diverged structurally: api_keys carried a
phantom `id` column and was missing email/rate_limit/tier/label/notes;
agents used `public_key` vs live `public_key_hex`; credentials used a
UUID id vs live serial. A fresh instance built from init_db.sql would
have crashed on startup.

fork-ci.yml papered over this with per-run ALTER TABLE / CREATE TABLE
workarounds (api_keys.email, request_log) instead of fixing the source.

This replaces init_db.sql with schema.sql — a `pg_dump --schema-only`
of the live DB: 53 tables, 2 views, 91 indexes, 17 FK constraints, the
credit_transactions append-only trigger. The dated legacy table
caep_events_legacy_20260415 is excluded; verified self-contained (no
FK or view depends on it, all 5 FK targets defined). schema.sql is a
generated artifact and carries a regeneration header — do not hand-edit.

CI now loads schema.sql directly with `-v ON_ERROR_STOP=1`, so the
build fails loudly if the schema-of-record ever breaks again. The
ALTER/CREATE alignment workarounds are removed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant