fix: use DATABASE_URL_SYNC (Railway reference var) as primary Alembic URL

DATABASE_URL_SYNC is now set as a Railway reference variable pointing to
${{pgvector.DATABASE_URL}}, which resolves to the correct postgres superuser
credentials per environment (production, PR preview, fresh DBs). This handles
the bootstrap case where resolutionflow_admin doesn't exist yet.

Falls back to ADMIN_DATABASE_URL (sync-converted) for local dev only.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-04-11 03:42:07 +00:00
parent 4273ed0e5c
commit b9fcdd5d73

View File

@@ -29,7 +29,6 @@ from app.models.session_branch import SessionBranch # noqa: F401
from app.models.fork_point import ForkPoint # noqa: F401
from app.models.session_handoff import SessionHandoff # noqa: F401
from app.models.session_resolution_output import SessionResolutionOutput # noqa: F401
import os
from app.core.config import settings
@@ -38,21 +37,16 @@ def _alembic_sync_url() -> str:
"""Return a psycopg2-compatible sync URL for Alembic.
Priority order:
1. Railway native PG env vars (PGHOST/PGPASSWORD/PGUSER/PGDATABASE) — these
are auto-linked per-environment by the Railway PostgreSQL service, so they
always carry the correct superuser credentials for the current environment
(production, PR preview, etc.), including fresh DBs with no custom roles yet.
1. DATABASE_URL_SYNC — in Railway this is set as a reference variable
(${{pgvector.DATABASE_URL}}) that resolves to the correct postgres
superuser credentials for the current environment (production, PR preview,
etc.). This always works even on fresh databases before any custom roles
have been created, because it uses the postgres superuser.
2. ADMIN_DATABASE_URL (resolutionflow_admin, BYPASSRLS) converted to a sync
driver — used for local dev and stable environments where the role exists.
3. DATABASE_URL_SYNC — last resort / legacy fallback.
driver — fallback for local dev where DATABASE_URL_SYNC may not be set.
"""
pg_host = os.getenv("PGHOST")
pg_user = os.getenv("PGUSER")
pg_password = os.getenv("PGPASSWORD")
pg_db = os.getenv("PGDATABASE")
pg_port = os.getenv("PGPORT", "5432")
if all([pg_host, pg_user, pg_password, pg_db]):
return f"postgresql://{pg_user}:{pg_password}@{pg_host}:{pg_port}/{pg_db}"
if settings.DATABASE_URL_SYNC:
return settings.DATABASE_URL_SYNC
admin_url = settings.ADMIN_DATABASE_URL
if admin_url and "+asyncpg" in admin_url: