fix: auto-convert postgres:// and postgresql:// to postgresql+asyncpg:// in settings
Some checks failed
CI / API Lint (push) Has been cancelled
CI / Detect changes (push) Has been cancelled
CI / API Tests (push) Has been cancelled
CI / Scanner Lint (push) Has been cancelled
CI / Scanner Tests (push) Has been cancelled
CI / Banner Lint & Typecheck (push) Has been cancelled
CI / Banner Tests (push) Has been cancelled
CI / Banner Build (push) Has been cancelled
CI / Admin UI Typecheck (push) Has been cancelled
CI / Admin UI Tests (push) Has been cancelled
CI / Admin UI Build (push) Has been cancelled

This commit is contained in:
Ami
2026-04-21 14:29:23 +07:00
parent 1c2bdbf310
commit d3af80145b

View File

@@ -117,6 +117,26 @@ class Settings(BaseSettings):
rate_limit_enabled: bool = True
rate_limit_per_minute: int = 120
@model_validator(mode="after")
def _normalize_database_url(self) -> "Settings":
"""Auto-fix common database URL schemes for asyncpg compatibility.
Platforms like Easypanel emit DATABASE_URL as ``postgres://...``
(shortcut or legacy scheme). SQLAlchemy expects the dialect name
``postgresql://`` and we need the ``+asyncpg`` driver suffix for
the async engine. Normalise both cases here so the rest of the
codebase can always assume ``postgresql+asyncpg://``.
"""
if self.database_url.startswith("postgres://"):
self.database_url = self.database_url.replace(
"postgres://", "postgresql+asyncpg://", 1,
)
elif self.database_url.startswith("postgresql://"):
self.database_url = self.database_url.replace(
"postgresql://", "postgresql+asyncpg://", 1,
)
return self
@model_validator(mode="after")
def _check_production_safety(self) -> "Settings":
"""Refuse to start with unsafe defaults in non-dev environments."""