fix: use resolutionflow_admin for Alembic migrations (avoid postgres superuser)
DATABASE_URL_SYNC uses the postgres superuser whose password is unavailable in Railway after Phase 1 switched runtime to the app role. resolutionflow_admin (BYPASSRLS) is the correct role for migrations. Derive a psycopg2 sync URL from ADMIN_DATABASE_URL; fall back to DATABASE_URL_SYNC for local dev environments where ADMIN_DATABASE_URL is not set separately. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user