diff --git a/backend/alembic/env.py b/backend/alembic/env.py index 7f9503b4..62b009f5 100644 --- a/backend/alembic/env.py +++ b/backend/alembic/env.py @@ -31,11 +31,25 @@ from app.models.session_handoff import SessionHandoff # noqa: F401 from app.models.session_resolution_output import SessionResolutionOutput # noqa: F401 from app.core.config import settings + +def _alembic_sync_url() -> str: + """Return a psycopg2-compatible sync URL for Alembic. + + Prefers ADMIN_DATABASE_URL (resolutionflow_admin, BYPASSRLS) converted to + a sync driver. Falls back to DATABASE_URL_SYNC for local dev where the + admin URL may not be configured separately. + """ + admin_url = settings.ADMIN_DATABASE_URL + if admin_url and "+asyncpg" in admin_url: + return admin_url.replace("postgresql+asyncpg://", "postgresql://") + return settings.DATABASE_URL_SYNC + + # this is the Alembic Config object config = context.config # Override sqlalchemy.url with the sync version for migrations -config.set_main_option("sqlalchemy.url", settings.DATABASE_URL_SYNC) +config.set_main_option("sqlalchemy.url", _alembic_sync_url()) # Interpret the config file for Python logging. if config.config_file_name is not None: @@ -86,7 +100,7 @@ def run_migrations_online() -> None: from sqlalchemy import create_engine connectable = create_engine( - settings.DATABASE_URL_SYNC, + _alembic_sync_url(), poolclass=pool.NullPool, )