Read the database revision with the Alembic API #19

Merged
alexander merged 3 commits from alembic-api into develop 2026-12-02 14:05:17 +01:00
2 changed files with 28 additions and 1 deletions
Showing only changes of commit 35d0c7b409 - Show all commits

Test reading the database revision
All checks were successful
CI / Checks (Python 3.13) (push) Successful in 2m50s

In the "sequentially" variant of the test database,
`current_db_revision()` must return the head of the migrations;
in the "all_at_once" variant, there is no version table.
Alexander Hess 2026-12-02 13:51:49 +01:00
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -12,7 +12,7 @@ import velofy
from velofy.db import connection
def current_db_revision() -> str | None: # pragma: no cover
def current_db_revision() -> str | None:
"""The `alembic` revision the database is at."""
with connection.get_engine().connect() as conn:
context = migration.MigrationContext.configure(

View file

@ -1,7 +1,11 @@
"""Test the `velofy.console.decorators` module."""
import click
import pytest
from alembic import config as alembic_config
from alembic import script as alembic_script
import velofy
from velofy.console import decorators
@ -36,3 +40,26 @@ def test_stage_refuses_at_another_revision(cli, monkeypatch):
assert "stage executed" not in result.output
assert current in result.output
assert REQUIRED in result.output
@pytest.mark.db
@pytest.mark.usefixtures("db_connection")
def test_current_db_revision(monkeypatch, request):
"""`current_db_revision()` reads the revision of the test database."""
# The migrations name the test database's version table
# after the test schema, as configured in migrations/env.py
monkeypatch.setattr(
velofy.config,
"ALEMBIC_TABLE",
f"{velofy.config.ALEMBIC_TABLE}_{velofy.config.CLEAN_SCHEMA}",
)
result = decorators.current_db_revision()
if request.node.callspec.params["db_connection"] == "sequentially":
cfg = alembic_config.Config("alembic.ini")
head = alembic_script.ScriptDirectory.from_config(cfg).get_current_head()
assert result == head
else: # "all_at_once" creates no version table
assert result is None