"""Alembic environment (async) for cloud-mode migrations. Reads the DB URL from the DATABASE_URL env var and targets cloud.models metadata, so `alembic revision --autogenerate` works out of the box. """ import asyncio import os from logging.config import fileConfig from sqlalchemy import pool from sqlalchemy.ext.asyncio import async_engine_from_config from alembic import context # Register model metadata. from cloud.database import Base import cloud.models # noqa: F401 config = context.config if config.config_file_name is not None: fileConfig(config.config_file_name) db_url = os.environ.get("DATABASE_URL", "") if db_url: config.set_main_option("sqlalchemy.url", db_url) target_metadata = Base.metadata def run_migrations_offline(): context.configure( url=config.get_main_option("sqlalchemy.url"), target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, ) with context.begin_transaction(): context.run_migrations() def _do_run(connection): context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() async def run_migrations_online(): connectable = async_engine_from_config( config.get_section(config.config_ini_section, {}), prefix="sqlalchemy.", poolclass=pool.NullPool, ) async with connectable.connect() as connection: await connection.run_sync(_do_run) await connectable.dispose() if context.is_offline_mode(): run_migrations_offline() else: asyncio.run(run_migrations_online())