import asyncio from logging.config import fileConfig from alembic import context from sqlalchemy.ext.asyncio import create_async_engine from app.core.config import settings from app.database import Base import app.models # noqa: F401 — registers Document, DocumentCategory, CategoryAssignment config = context.config config.set_main_option("sqlalchemy.url", settings.DATABASE_URL) if config.config_file_name: fileConfig(config.config_file_name) target_metadata = Base.metadata # Separate version table — must not collide with the main backend's alembic_version table. VERSION_TABLE = "alembic_version_doc_service" def run_migrations_offline(): context.configure( url=settings.DATABASE_URL, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, version_table=VERSION_TABLE, ) with context.begin_transaction(): context.run_migrations() def do_run_migrations(connection): context.configure( connection=connection, target_metadata=target_metadata, version_table=VERSION_TABLE, ) with context.begin_transaction(): context.run_migrations() async def run_migrations_online(): engine = create_async_engine(settings.DATABASE_URL) async with engine.connect() as conn: await conn.run_sync(do_run_migrations) await engine.dispose() if context.is_offline_mode(): run_migrations_offline() else: asyncio.run(run_migrations_online())