Files
Downloader-Bot/services/storage/database_url.py
Mak5er fc41bde6f9 fix(db): normalize alembic database URL handling
Resolve Alembic paths relative to the services directory, load DATABASE_URL from the project .env, and normalize async PostgreSQL URLs before migrations run.

Also extract shared database URL helpers and cover the behavior with database tests.
2026-04-20 12:02:07 +03:00

26 lines
708 B
Python

import re
ALEMBIC_DATABASE_URL_PLACEHOLDER = "${DATABASE_URL}"
def to_sync_database_url(database_url: str) -> str:
return re.sub(r"^postgresql\+asyncpg:", "postgresql:", database_url)
def to_async_database_url(database_url: str) -> str:
sync_url = to_sync_database_url(database_url)
return re.sub(r"^postgresql:", "postgresql+asyncpg:", sync_url)
def resolve_alembic_database_url(
configured_url: str | None,
env_database_url: str | None,
) -> str | None:
candidate = configured_url
if not candidate or candidate == ALEMBIC_DATABASE_URL_PLACEHOLDER:
candidate = env_database_url
if not candidate:
return None
return to_sync_database_url(candidate)