Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration version-check function #58

Merged
merged 3 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions pycds/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import pycds


def check_migration_version(
executor,
schema_name=pycds.get_schema_name(),
# `version` must be kept up to date with latest migration
# a test checks it, however, in case you don't
version='8fd8f556c548',
):
"""Check that the migration version of the database schema is compatible
with the current version of this package.

This implementation is quick and easy, relying on manual updating of the
correct version number.
"""
current = executor.execute(f"""
SELECT version_num
FROM {schema_name}.alembic_version
""").scalar()
if current != version:
raise ValueError(
f"Schema {schema_name} must be at Alembic version {version}; "
f"detected version {current}."
)


# TODO: Does this have any current utility? It is not used in any current code.
# Also its result can be achieved with 1 line:
# ...statement.compile(compile_kwargs={"literal_binds": True}))
Expand Down Expand Up @@ -37,14 +62,3 @@ def visit_bindparam(

compiler = LiteralCompiler(dialect, statement)
return compiler.process(statement)


def create_test_database(engine):
pycds.Base.metadata.create_all(bind=engine)







30 changes: 30 additions & 0 deletions tests/alembic_migrations/test_check_migration_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Test that `check_migration_version` passes on the latest migration
and raises an exception on other versions.

If this test fails, you must update `check_migration_version` with the latest
migration version.
"""
import pytest
from alembic import command
from .alembicverify_util import prepare_schema_from_migrations
from pycds.util import check_migration_version


@pytest.mark.usefixtures('new_db_left')
def test_check_migration_version(
uri_left, alembic_config_left, db_setup, env_config,
):
engine, script = prepare_schema_from_migrations(
uri_left, alembic_config_left, db_setup=db_setup
)

# Test against the most up to date migration in the database;
# this should pass, i.e., not raise an exception.
check_migration_version(engine)

# Back off to the previous migration.
command.downgrade(alembic_config_left, '-1')

# Now the checker should raise an exception.
with pytest.raises(ValueError):
check_migration_version(engine)