"""Alembic environment configuration. Sets up sys.path so that app packages are importable when running alembic from the backend directory. Alembic uses sync connections for migrations even with an async app. """ import os import sys # Ensure the backend root is on the path so app.* is importable sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from logging.config import fileConfig from alembic import context from sqlalchemy import engine_from_config, pool # Import models so that Base.metadata includes all tables from app.db.models import Base config = context.config fileConfig(config.config_file_name) def run_migrations_online() -> None: """Run migrations in 'online' mode using sync engine.""" connectable = engine_from_config( config.get_section(config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool, ) with connectable.connect() as connection: context.configure( connection=connection, target_metadata=Base.metadata, render_as_batch=True, ) with context.begin_transaction(): context.run_migrations() connectable.dispose() def run_migrations_offline() -> None: """Run migrations in 'offline' mode.""" url = config.get_main_option("sqlalchemy.url") context.configure( url=url, target_metadata=Base.metadata, render_as_batch=True, literal_binds=True, dialect_opts={"paramstyle": "batch"}, ) with context.begin_transaction(): context.run_migrations() if context.is_offline_mode(): run_migrations_offline() else: run_migrations_online()