Adopt Alembic: autogenerated baseline, stamp-or-upgrade startup

The baseline (c3da001a0eb3) was autogenerated against an empty Postgres
and schema-diffed against the live dev schema until column-identical
(two model fixes fell out: deferred_to_next_cycle now declares its
server_default, usersettings.data is JSONB on Postgres to match the
live type). Startup replaces init_db/run_migrations with
run_alembic_upgrade(): pre-Alembic databases that already match the
baseline are adopted via stamp; fresh databases build from the
migration. The section is serialized with a pg advisory lock because
prod uvicorn runs 2 workers, each executing the lifespan. Verified:
adoption + idempotent re-run on the dev DB, fresh 13-table build on an
empty DB, full container boot, 55/55 tests. (ARCH-02, BE-19)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Carlos Escalante
2026-06-09 20:31:55 -06:00
parent c725c1487d
commit 9a25c4baab
10 changed files with 519 additions and 73 deletions

View File

@@ -20,7 +20,7 @@ from app.auth import (
verify_admin_credentials,
)
from app.config import settings
from app.db import get_session, init_db, run_migrations
from app.db import get_session, run_alembic_upgrade
from app.seed import seed_db
from app.services.exchange_rate import refresh_rates_periodically
@@ -65,8 +65,7 @@ def _pair_orphan_tool_calls(messages: list) -> list:
@asynccontextmanager
async def lifespan(app: FastAPI):
init_db()
run_migrations()
run_alembic_upgrade()
seed_db()
rate_refresh_task = asyncio.create_task(refresh_rates_periodically())
try: