"""Alembic environment. Runs on a connection handed over by app.db.migrate (or the CLI via alembic.ini).""" import asyncio from alembic import context from sqlalchemy.engine import Connection from app.config import DatabaseSettings from app.db.models import Base from app.db.session import make_engine target_metadata = Base.metadata def _run(connection: Connection) -> None: context.configure( connection=connection, target_metadata=target_metadata, render_as_batch=connection.dialect.name == "sqlite", # SQLite can't ALTER most things in place compare_type=True, ) with context.begin_transaction(): context.run_migrations() async def _run_cli() -> None: engine = make_engine(DatabaseSettings().url) try: async with engine.begin() as conn: await conn.run_sync(_run) finally: await engine.dispose() connection = context.config.attributes.get("connection") if connection is not None: _run(connection) else: asyncio.run(_run_cli())