import asyncio import os from logging.config import fileConfig from sqlalchemy import pool from alembic import context from app.db import Base from app.postgres import create_postgres_engine config = context.config if config.config_file_name: fileConfig(config.config_file_name) database_url = os.environ["DATABASE_URL"] config.set_main_option("sqlalchemy.url", database_url.replace("%", "%%")) target_metadata = Base.metadata def do_run_migrations(connection) -> None: context.configure( connection=connection, target_metadata=target_metadata, include_schemas=True, version_table_schema="han_app", compare_type=True, ) with context.begin_transaction(): context.run_migrations() async def run_async_migrations() -> None: connectable = create_postgres_engine(database_url, poolclass=pool.NullPool) async with connectable.connect() as connection: await connection.run_sync(do_run_migrations) await connectable.dispose() if context.is_offline_mode(): context.configure( url=config.get_main_option("sqlalchemy.url"), target_metadata=target_metadata, literal_binds=True, include_schemas=True, version_table_schema="han_app", ) with context.begin_transaction(): context.run_migrations() else: asyncio.run(run_async_migrations())