import asyncio from alembic import context from sqlalchemy import pool from sqlalchemy.ext.asyncio import create_async_engine from src.core.db.models import Base from src.core.settings import settings url = settings.sqlalchemy_database_url target_metadata = Base.metadata def run_migrations(connection): context.configure(connection=connection, target_metadata=target_metadata, compare_type=True) with context.begin_transaction(): context.run_migrations() async def run_online(): engine = create_async_engine(url, poolclass=pool.NullPool) try: async with engine.connect() as connection: await connection.run_sync(run_migrations) finally: await engine.dispose() if context.is_offline_mode(): context.configure(url=url, target_metadata=target_metadata, literal_binds=True) with context.begin_transaction(): context.run_migrations() else: asyncio.run(run_online())