mirror of
https://github.com/Mak5er/Downloader-Bot
synced 2026-11-26 17:18:04 +08:00
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.
26 lines
708 B
Python
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)
|