|
| 1 | +"""Alembic environment for the v3 reference seller. |
| 2 | +
|
| 3 | +Uses SQLAlchemy's async engine (asyncpg) via the standard Alembic |
| 4 | +async pattern. Run from the examples/v3_reference_seller/ directory: |
| 5 | +
|
| 6 | + DATABASE_URL=postgresql+asyncpg://... alembic upgrade head |
| 7 | +
|
| 8 | +For autogenerate to capture every table, both src.models and src.audit |
| 9 | +must be imported before target_metadata is read. Missing either import |
| 10 | +silently omits that module's tables from the generated migration. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import os |
| 17 | +import sys |
| 18 | +from logging.config import fileConfig |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from alembic import context |
| 22 | +from sqlalchemy.ext.asyncio import create_async_engine |
| 23 | + |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | +# Path wiring — make ``src.*`` importable when env.py is executed from the |
| 26 | +# examples/v3_reference_seller/ directory by the ``alembic`` CLI. |
| 27 | +# --------------------------------------------------------------------------- |
| 28 | +_HERE = Path(__file__).resolve().parent.parent # examples/v3_reference_seller/ |
| 29 | +if str(_HERE) not in sys.path: |
| 30 | + sys.path.insert(0, str(_HERE)) |
| 31 | + |
| 32 | +# Import all ORM modules so their tables appear in Base.metadata. |
| 33 | +# Adding a new model file? Import it here or autogenerate will miss it. |
| 34 | +import src.audit # noqa: E402, F401 — registers AuditEventRow on Base.metadata |
| 35 | +from src.models import Base # noqa: E402 |
| 36 | + |
| 37 | +target_metadata = Base.metadata |
| 38 | + |
| 39 | +# --------------------------------------------------------------------------- |
| 40 | +# Alembic config |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | +config = context.config |
| 43 | + |
| 44 | +if config.config_file_name is not None: |
| 45 | + fileConfig(config.config_file_name) |
| 46 | + |
| 47 | +# DATABASE_URL comes from the environment; never hardcode it here. |
| 48 | +try: |
| 49 | + _db_url: str = os.environ["DATABASE_URL"] |
| 50 | +except KeyError: |
| 51 | + raise RuntimeError( |
| 52 | + "DATABASE_URL environment variable is not set. " |
| 53 | + "Example: DATABASE_URL=postgresql+asyncpg://postgres@localhost/adcp alembic upgrade head" |
| 54 | + ) from None |
| 55 | +config.set_main_option("sqlalchemy.url", _db_url) |
| 56 | + |
| 57 | + |
| 58 | +# --------------------------------------------------------------------------- |
| 59 | +# Migration helpers |
| 60 | +# --------------------------------------------------------------------------- |
| 61 | + |
| 62 | +def run_migrations_offline() -> None: |
| 63 | + """Emit SQL to stdout rather than connecting to the DB. |
| 64 | +
|
| 65 | + Useful for generating a migration script to review or apply manually. |
| 66 | + """ |
| 67 | + url = config.get_main_option("sqlalchemy.url") |
| 68 | + context.configure( |
| 69 | + url=url, |
| 70 | + target_metadata=target_metadata, |
| 71 | + literal_binds=True, |
| 72 | + dialect_opts={"paramstyle": "named"}, |
| 73 | + compare_type=True, |
| 74 | + ) |
| 75 | + with context.begin_transaction(): |
| 76 | + context.run_migrations() |
| 77 | + |
| 78 | + |
| 79 | +def do_run_migrations(connection) -> None: |
| 80 | + context.configure( |
| 81 | + connection=connection, |
| 82 | + target_metadata=target_metadata, |
| 83 | + compare_type=True, |
| 84 | + ) |
| 85 | + with context.begin_transaction(): |
| 86 | + context.run_migrations() |
| 87 | + |
| 88 | + |
| 89 | +async def run_migrations_online() -> None: |
| 90 | + """Create an async engine and run migrations inside a sync wrapper. |
| 91 | +
|
| 92 | + Alembic's migration functions are synchronous; ``run_sync`` bridges |
| 93 | + the gap so we can use an asyncpg engine end-to-end. |
| 94 | + """ |
| 95 | + connectable = create_async_engine(_db_url, echo=False) |
| 96 | + async with connectable.connect() as connection: |
| 97 | + await connection.run_sync(do_run_migrations) |
| 98 | + await connectable.dispose() |
| 99 | + |
| 100 | + |
| 101 | +if context.is_offline_mode(): |
| 102 | + run_migrations_offline() |
| 103 | +else: |
| 104 | + asyncio.run(run_migrations_online()) |
0 commit comments