From a5c5eb6cc3aa6f100cba86f105dc1445f1e3e955 Mon Sep 17 00:00:00 2001 From: chihlasm Date: Fri, 10 Apr 2026 04:03:32 +0000 Subject: [PATCH] fix: convert DATABASE_URL_SYNC from property to overridable field for Alembic superuser URL Co-Authored-By: Claude Sonnet 4.6 --- backend/app/core/config.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/app/core/config.py b/backend/app/core/config.py index d8cbfe2f..5d31b789 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -23,10 +23,19 @@ class Settings(BaseSettings): return v.replace("postgresql://", "postgresql+asyncpg://", 1) return v - @property - def DATABASE_URL_SYNC(self) -> str: - """Get sync URL by removing asyncpg prefix from DATABASE_URL.""" - return self.DATABASE_URL.replace("postgresql+asyncpg://", "postgresql://", 1) + # Sync URL for Alembic migrations. Defaults to DATABASE_URL (sync-converted). + # Set explicitly in .env to use a different role for migrations (e.g. superuser) + # when DATABASE_URL has been switched to the app role. + DATABASE_URL_SYNC: str = "" + + @field_validator("DATABASE_URL_SYNC", mode="before") + @classmethod + def default_database_url_sync(cls, v: str, info) -> str: + """Fall back to sync-converted DATABASE_URL if not explicitly set.""" + if not v: + base = info.data.get("DATABASE_URL", "") + return base.replace("postgresql+asyncpg://", "postgresql://", 1) + return v # Admin database — resolutionflow_admin role, BYPASSRLS. # Used by /admin/* endpoints. Defaults to DATABASE_URL for local dev.