v1.4.1: Alembic — client_encoding=utf8 для psycopg3

Устраняет TypeError при определении версии PostgreSQL (bytes vs str).

Made-with: Cursor
This commit is contained in:
Alexandr
2026-04-03 08:50:11 +03:00
parent 349cea85a3
commit 0787745098
5 changed files with 19 additions and 5 deletions

View File

@ -20,6 +20,14 @@ if config.config_file_name is not None:
target_metadata = None
def _ensure_psycopg_client_encoding(url: str) -> str:
"""Иначе psycopg3 при SQL_ASCII на сервере отдаёт version() как bytes → падает SQLAlchemy re.match."""
if "+psycopg" not in url or "client_encoding=" in url:
return url
join = "&" if "?" in url else "?"
return f"{url}{join}client_encoding=utf8"
def get_sync_url() -> str:
url = os.environ.get("DATABASE_URL", "").strip()
if not url:
@ -30,7 +38,7 @@ def get_sync_url() -> str:
url = url.replace("postgresql://", "postgresql+psycopg://", 1)
if "+asyncpg" in url:
url = url.replace("+asyncpg", "+psycopg")
return url
return _ensure_psycopg_client_encoding(url)
def run_migrations_offline() -> None: