|
| 1 | +from __future__ import with_statement |
| 2 | +import os |
| 3 | +from logging.config import fileConfig |
| 4 | + |
| 5 | +from sqlalchemy import engine_from_config |
| 6 | +from sqlalchemy import pool |
| 7 | + |
| 8 | +from alembic import context |
| 9 | + |
| 10 | +# this is the Alembic Config object, which provides |
| 11 | +# access to the values within the .ini file in use. |
| 12 | +config = context.config |
| 13 | + |
| 14 | +# Interpret the config file for Python logging. Some environments (CI or |
| 15 | +# trimmed alembic.ini) may not include all logger sections; guard against |
| 16 | +# that to avoid stopping migrations with a KeyError. |
| 17 | +if config.config_file_name is not None: |
| 18 | + try: |
| 19 | + fileConfig(config.config_file_name) |
| 20 | + except Exception: |
| 21 | + # Fall back to a minimal logging configuration if the ini is missing |
| 22 | + # expected logger sections (e.g. 'logger_sqlalchemy'). This makes |
| 23 | + # migrations resilient when run in different environments. |
| 24 | + import logging |
| 25 | + |
| 26 | + logging.basicConfig(level=logging.INFO) |
| 27 | + |
| 28 | +target_metadata = None |
| 29 | + |
| 30 | +# Use DATABASE_URL env if provided |
| 31 | +db_url = os.getenv("DATABASE_URL") or config.get_main_option("sqlalchemy.url") |
| 32 | +if db_url: |
| 33 | + # Only set the option if we have a valid string value. Avoid setting None |
| 34 | + # which causes ConfigParser type errors (option values must be strings). |
| 35 | + config.set_main_option("sqlalchemy.url", str(db_url)) |
| 36 | + |
| 37 | + |
| 38 | +def run_migrations_offline(): |
| 39 | + url = config.get_main_option("sqlalchemy.url") |
| 40 | + context.configure(url=url, target_metadata=target_metadata, literal_binds=True) |
| 41 | + |
| 42 | + with context.begin_transaction(): |
| 43 | + context.run_migrations() |
| 44 | + |
| 45 | + |
| 46 | +def run_migrations_online(): |
| 47 | + # Determine whether the configured URL uses an async driver. If so, |
| 48 | + # create an AsyncEngine and run the migrations inside an async context |
| 49 | + # while delegating the actual migration steps to a sync callable via |
| 50 | + # `connection.run_sync`. Otherwise, fall back to the classic sync path. |
| 51 | + url = config.get_main_option("sqlalchemy.url") |
| 52 | + |
| 53 | + def _do_run_migrations(connection): |
| 54 | + context.configure(connection=connection, target_metadata=target_metadata) |
| 55 | + with context.begin_transaction(): |
| 56 | + context.run_migrations() |
| 57 | + |
| 58 | + if url and url.startswith("postgresql+asyncpg"): |
| 59 | + # Async migration path |
| 60 | + from sqlalchemy.ext.asyncio import create_async_engine |
| 61 | + import asyncio |
| 62 | + |
| 63 | + async_engine = create_async_engine(url, future=True) |
| 64 | + |
| 65 | + async def run(): |
| 66 | + async with async_engine.connect() as connection: |
| 67 | + await connection.run_sync(_do_run_migrations) |
| 68 | + |
| 69 | + asyncio.run(run()) |
| 70 | + else: |
| 71 | + # Sync migration path (classic) |
| 72 | + connectable = engine_from_config( |
| 73 | + config.get_section(config.config_ini_section), |
| 74 | + prefix="sqlalchemy.", |
| 75 | + poolclass=pool.NullPool, |
| 76 | + ) |
| 77 | + |
| 78 | + with connectable.connect() as connection: |
| 79 | + _do_run_migrations(connection) |
| 80 | + |
| 81 | + |
| 82 | +if context.is_offline_mode(): |
| 83 | + run_migrations_offline() |
| 84 | +else: |
| 85 | + run_migrations_online() |
0 commit comments