Skip to content

fix(migrations): raise on failure, track applied migrations, schema health check (#236) - #239

Merged
vybe merged 2 commits into
mainfrom
feature/236-migration-error-handling
Apr 1, 2026
Merged

vybe merged 2 commits into
mainfrom
feature/236-migration-error-handling

Conversation

@vybe

@vybe vybe commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replace silent print+continue in run_all_migrations with logger.error + raise so the backend refuses to start with an inconsistent schema
  • Add schema_migrations tracking table (PRIMARY KEY) to skip already-applied migrations and prevent multi-worker race conditions via INSERT OR IGNORE
  • Fresh-install no such table errors are gracefully skipped — init_schema will create the table with the correct current schema
  • GET /health returns 503 with {applied, expected} counts when migrations are incomplete

Changes

  • src/backend/db/migrations.py — rewritten run_all_migrations, added MIGRATIONS module-level constant
  • src/backend/main.py — health check schema consistency, module-level logger
  • tests/unit/test_migrations.py — 16 new unit tests (tracking, idempotency, failure propagation, concurrent-write safety)

Test Plan

  • 16 unit tests pass: pytest tests/unit/test_migrations.py -v
  • INSERT OR IGNORE race condition test passes
  • Fresh-install no such table skip test passes
  • Manual: restart backend, verify /health returns {"status": "healthy"} with migrations applied

Closes #236

🤖 Generated with Claude Code

vybe and others added 2 commits April 1, 2026 11:14
…ealth check

- Replace silent print+continue with logger.error + raise so backend
  refuses to start with an inconsistent schema
- Add schema_migrations tracking table (PRIMARY KEY) to skip already-applied
  migrations and prevent multi-worker race conditions (INSERT OR IGNORE)
- Fresh-install "no such table" errors are gracefully skipped so init_schema
  can create tables with the correct current schema
- /health endpoint returns 503 with migration counts when schema is incomplete
- 16 unit tests covering tracking, idempotency, failure propagation, and
  concurrent-write safety

Closes #236

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…ccurate health check

On first startup, migrations whose target table doesn't exist yet are
skipped-but-not-recorded. init_schema then creates the tables correctly,
but the health check's COUNT(*) < len(MIGRATIONS) would return 503
indefinitely. A second run_all_migrations call after init_schema re-runs
those skips as no-ops (all migrations guard with PRAGMA table_info) and
records them, keeping /health accurate on fresh installs.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
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.

bug: Migration silently fails — fan_out_id column missing after deploy, breaks chat

1 participant