Commit bdbc11e860
Verified · cmc
Layout: unified · split
alembic/env.py +5
| @@ -1,6 +1,7 @@ | |||
| 1 | from __future__ import annotations | 1 | from __future__ import annotations |
| 2 | 2 | ||
| 3 | from logging.config import fileConfig | 3 | from logging.config import fileConfig |
| 4 | import os | ||
| 4 | 5 | ||
| 5 | from alembic import context | 6 | from alembic import context |
| 6 | from sqlalchemy import engine_from_config, pool | 7 | from sqlalchemy import engine_from_config, pool |
| @@ -18,6 +19,10 @@ target_metadata = Base.metadata | |||
| 18 | 19 | ||
| 19 | 20 | ||
| 20 | def get_database_url() -> str: | 21 | def get_database_url() -> str: |
| 22 | env_database_url = os.getenv("DATABASE_URL") | ||
| 23 | if env_database_url: | ||
| 24 | return env_database_url | ||
| 25 | |||
| 21 | configured_url = config.get_main_option("sqlalchemy.url") | 26 | configured_url = config.get_main_option("sqlalchemy.url") |
| 22 | if configured_url: | 27 | if configured_url: |
| 23 | return configured_url | 28 | return configured_url |
tests/test_migrations.py +13
| @@ -115,3 +115,16 @@ def test_alembic_upgrade_adopts_legacy_schema(tmp_path) -> None: | |||
| 115 | assert columns["actor"]["nullable"] is False | 115 | assert columns["actor"]["nullable"] is False |
| 116 | assert "uq_tracked_repository_service_actor_name" in unique_constraints | 116 | assert "uq_tracked_repository_service_actor_name" in unique_constraints |
| 117 | assert actor == Settings().default_actor | 117 | assert actor == Settings().default_actor |
| 118 | |||
| 119 | |||
| 120 | def test_alembic_prefers_database_url_from_environment(tmp_path, monkeypatch) -> None: | ||
| 121 | database_path = tmp_path / "env-selected.db" | ||
| 122 | database_url = f"sqlite:///{database_path}" | ||
| 123 | config = Config(str(Path(__file__).resolve().parent.parent / "alembic.ini")) | ||
| 124 | config.set_main_option("script_location", str(Path(__file__).resolve().parent.parent / "alembic")) | ||
| 125 | monkeypatch.setenv("DATABASE_URL", database_url) | ||
| 126 | |||
| 127 | command.upgrade(config, "head") | ||
| 128 | |||
| 129 | inspector = inspect(create_engine(database_url)) | ||
| 130 | assert "actor_aliases" in inspector.get_table_names() | ||