import asyncio from alembic import context from sqlalchemy.ext.asyncio import create_async_engine from app.models import Base from app.core.config import Settings config = context.config target_metadata = Base.metadata def include_object(obj, name, type_, reflected, compare_to): return name.startswith("visual_") if type_ == "table" else True def run(connection): context.configure( connection=connection, target_metadata=target_metadata, include_object=include_object, compare_type=True, ) with context.begin_transaction(): context.run_migrations() async def online(): url = config.attributes.get("url") or Settings.from_env().sqlalchemy_url engine = create_async_engine(url) async with engine.connect() as connection: await connection.run_sync(run) await engine.dispose() if context.is_offline_mode(): context.configure( url=config.get_main_option("sqlalchemy.url"), target_metadata=target_metadata, literal_binds=True, include_object=include_object, ) with context.begin_transaction(): context.run_migrations() else: asyncio.run(online())