Schema 迁移 (Alembic)
Fonrex uses Alembic to manage database schema evolutions. Migrations run in an isolated environment prior to API service startup to ensure schema consistency.
Migration History
| Revision ID | 说明描述 | Key Changes |
|---|---|---|
001_initial | Initial schema | assets, prices_eod, fundamentals |
008_realtime | Realtime streaming | prices_intraday hypertable, realtime_subscriptions |
009_isin_listings | Asset & listing refactor | asset_listings, asset_mappings, partial ISIN unique index |
010_news_articles | News aggregation | news_articles table with url unique constraint |
011_provider_monitoring | Provider health suite | provider_health_log hypertable, provider_health_daily, provider_alerts |
Migration Execution Flow
- At container startup,
entrypoint.shexecutesalembic upgrade head. main.pychecks current schema status viadatabase/migrations.py.- If the stored revision in
alembic_versiondoes not matchhead,main.pymarksapp.state.db_available = Falseto prevent queries against incomplete schemas.
Creating a New Migration
To add a new table or column, generate a migration script inside the running container:
docker compose exec fonrex-api alembic revision -m "add_column_to_table"
Edit the generated file in alembic/versions/ and apply it:
docker compose exec fonrex-api alembic upgrade head