|
| 1 | +"""Alembic env — supports both sync (offline / CLI) and async runtimes. |
| 2 | +
|
| 3 | +DATABASE_URL is resolved via db.engine.get_database_url so the same code |
| 4 | +path applies whether you run alembic from the CLI or programmatically |
| 5 | +during app startup. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import asyncio |
| 11 | +import os |
| 12 | +import sys |
| 13 | +from logging.config import fileConfig |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from alembic import context |
| 17 | +from sqlalchemy import engine_from_config, pool |
| 18 | +from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine |
| 19 | + |
| 20 | +# Make `src/` importable |
| 21 | +ROOT = Path(__file__).resolve().parent.parent |
| 22 | +SRC = ROOT / "src" |
| 23 | +if str(SRC) not in sys.path: |
| 24 | + sys.path.insert(0, str(SRC)) |
| 25 | + |
| 26 | +from db.base import metadata as base_metadata # noqa: E402 |
| 27 | +from db.engine import get_database_url # noqa: E402 |
| 28 | +import db.models # noqa: E402,F401 (registers tables on metadata) |
| 29 | +from sqlmodel import SQLModel # noqa: E402 |
| 30 | + |
| 31 | +config = context.config |
| 32 | + |
| 33 | +if config.config_file_name is not None: |
| 34 | + try: |
| 35 | + fileConfig(config.config_file_name) |
| 36 | + except Exception: |
| 37 | + pass |
| 38 | + |
| 39 | +target_metadata = SQLModel.metadata |
| 40 | + |
| 41 | + |
| 42 | +def _sync_url() -> str: |
| 43 | + """Alembic prefers a sync URL for offline mode. Convert async drivers.""" |
| 44 | + url = get_database_url() |
| 45 | + return ( |
| 46 | + url.replace("+aiosqlite", "") |
| 47 | + .replace("postgresql+asyncpg", "postgresql+psycopg") |
| 48 | + .replace("mysql+aiomysql", "mysql+pymysql") |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def run_migrations_offline() -> None: |
| 53 | + context.configure( |
| 54 | + url=_sync_url(), |
| 55 | + target_metadata=target_metadata, |
| 56 | + literal_binds=True, |
| 57 | + dialect_opts={"paramstyle": "named"}, |
| 58 | + render_as_batch=True, # SQLite-friendly |
| 59 | + ) |
| 60 | + with context.begin_transaction(): |
| 61 | + context.run_migrations() |
| 62 | + |
| 63 | + |
| 64 | +def do_run_migrations(connection) -> None: |
| 65 | + context.configure( |
| 66 | + connection=connection, |
| 67 | + target_metadata=target_metadata, |
| 68 | + render_as_batch=True, |
| 69 | + ) |
| 70 | + with context.begin_transaction(): |
| 71 | + context.run_migrations() |
| 72 | + |
| 73 | + |
| 74 | +async def run_async_migrations() -> None: |
| 75 | + connectable: AsyncEngine = create_async_engine( |
| 76 | + get_database_url(), poolclass=pool.NullPool, future=True |
| 77 | + ) |
| 78 | + async with connectable.connect() as connection: |
| 79 | + await connection.run_sync(do_run_migrations) |
| 80 | + await connectable.dispose() |
| 81 | + |
| 82 | + |
| 83 | +def run_migrations_online() -> None: |
| 84 | + asyncio.run(run_async_migrations()) |
| 85 | + |
| 86 | + |
| 87 | +if context.is_offline_mode(): |
| 88 | + run_migrations_offline() |
| 89 | +else: |
| 90 | + run_migrations_online() |
0 commit comments