File size: 767 Bytes
3493993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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