diff --git a/.github/workflows/migration.yml b/.github/workflows/migration.yml new file mode 100644 index 0000000..d7293e4 --- /dev/null +++ b/.github/workflows/migration.yml @@ -0,0 +1,29 @@ +name: Python Alembic Migration Tests + +on: + push: + branches: master + pull_request: + branches: master + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest pytest-alembic + + - name: Run Tests + run: | + pytest tests/ -v \ No newline at end of file diff --git a/app/db/migrations/env.py b/app/db/migrations/env.py index 16be362..31cd602 100644 --- a/app/db/migrations/env.py +++ b/app/db/migrations/env.py @@ -60,11 +60,13 @@ def run_migrations_online() -> None: and associate a connection with the context. """ - connectable = engine_from_config( - config.get_section(config.config_ini_section), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - ) + connectable = context.config.attributes.get("connection", None) + if connectable is None: + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) with connectable.connect() as connection: context.configure( diff --git a/tests/test_migrations.py b/tests/test_migrations.py new file mode 100644 index 0000000..3919025 --- /dev/null +++ b/tests/test_migrations.py @@ -0,0 +1,16 @@ +import pytest +from pytest_alembic.tests import ( + test_model_definitions_match_ddl, + test_single_head_revision, + test_up_down_consistency, + test_upgrade, +) + + +@pytest.mark.tryfirst +def test_model_definitions_match_ddl(alembic_engine, alembic_config): + if "sqlite" in alembic_engine.url.drivername: + pytest.skip("Skipping DDL test for SQLite database") + from pytest_alembic.tests import test_model_definitions_match_ddl + + test_model_definitions_match_ddl(alembic_engine, alembic_config)