MediaRouter / app /core /database_url.py
basyx's picture
Upload 340 files
3493993 verified
Raw
History Blame Contribute Delete
767 Bytes
from __future__ import annotations
def normalize_async_database_url(value: str) -> str:
"""Use the installed asyncpg dialect for bare PostgreSQL URLs.
Operators frequently supply a standard ``postgresql://`` URL. SQLAlchemy
otherwise selects synchronous psycopg2 for that form, which fails inside
this async application and can tempt deployments to add an unnecessary
synchronous driver. Explicit dialect URLs remain unchanged.
"""
normalized = value.strip()
if normalized.startswith("postgresql://"):
return "postgresql+asyncpg://" + normalized.removeprefix("postgresql://")
if normalized.startswith("postgres://"):
return "postgresql+asyncpg://" + normalized.removeprefix("postgres://")
return normalized