This commit is contained in:
@@ -31,6 +31,31 @@ class Settings(BaseSettings):
|
||||
)
|
||||
|
||||
|
||||
def sqlalchemy_database_url(database_url: str) -> str:
|
||||
"""Add SQLAlchemy's Psycopg 3 driver to a PostgreSQL URL.
|
||||
|
||||
``Settings.database_url`` remains a plain ``postgresql://`` URL because
|
||||
Psycopg 3 accepts that form directly. Alembic and SQLAlchemy need the
|
||||
explicit ``postgresql+psycopg://`` dialect, while the rest of the URL
|
||||
(including query parameters such as ``sslmode``) must remain unchanged.
|
||||
|
||||
Args:
|
||||
database_url: PostgreSQL connection URL from the process environment.
|
||||
|
||||
Returns:
|
||||
The URL with SQLAlchemy's Psycopg 3 dialect prefix.
|
||||
|
||||
Raises:
|
||||
ValueError: If the URL does not use a supported PostgreSQL scheme.
|
||||
"""
|
||||
|
||||
if database_url.startswith("postgresql+psycopg://"):
|
||||
return database_url
|
||||
if database_url.startswith("postgresql://"):
|
||||
return "postgresql+psycopg://" + database_url.removeprefix("postgresql://")
|
||||
raise ValueError("database_url must use postgresql:// or postgresql+psycopg://")
|
||||
|
||||
|
||||
@lru_cache
|
||||
def get_settings() -> Settings:
|
||||
"""Return one immutable-by-convention configuration object per process.
|
||||
|
||||
Reference in New Issue
Block a user