Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nxlouie committed Oct 18, 2022
1 parent 7c4d160 commit 2be534c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
10 changes: 8 additions & 2 deletions alembic/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def revision(
the other parameters, this option is only available via programmatic
use of :func:`.command.revision`
:param check: instead of generating a revision, checks if this revision
will contain upgrade ops; no new ops will have no action; new ops will
error; this is the ``--check`` option to ``alembic revision``.
"""

script_directory = ScriptDirectory.from_config(config)
Expand Down Expand Up @@ -238,11 +242,13 @@ def retrieve_migrations(rev, context):

if check:
if not autogenerate:
util.err("check flag cannot be used without autogenerate flag.")
raise util.CommandError(
"Check flag cannot be used without autogenerate flag"
)
migration_script = revision_context.generated_revisions[-1]
diffs = migration_script.upgrade_ops.as_diffs()
if diffs:
util.err(f"Revision has upgrade ops to run: {diffs}.")
raise util.RevisionOpsNotEmptyError(f"Revision has upgrade ops to run: {diffs}.")
else:
log.info("Revision has no upgrade ops to run.")
else:
Expand Down
2 changes: 1 addition & 1 deletion alembic/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .editor import open_in_editor
from .exc import CommandError
from .exc import CommandError, RevisionOpsNotEmptyError
from .langhelpers import _with_legacy_names
from .langhelpers import asbool
from .langhelpers import dedupe_tuple
Expand Down
3 changes: 3 additions & 0 deletions alembic/util/exc.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class CommandError(Exception):
pass

class RevisionOpsNotEmptyError(Exception):
pass
33 changes: 32 additions & 1 deletion tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from typing import cast

from sqlalchemy import exc as sqla_exc
from sqlalchemy import text
from sqlalchemy import VARCHAR, text
from sqlalchemy.engine import Engine
from sqlalchemy.sql.schema import Column

from alembic import __version__
from alembic import command
Expand Down Expand Up @@ -385,6 +386,36 @@ def test_create_rev_autogen_need_to_select_head(self):
self.cfg,
autogenerate=True,
)

def test_rev_check_with_no_autogen(self):
self._env_fixture()
assert_raises_message(
util.CommandError,
"Check flag cannot be used without autogenerate flag",
command.revision,
self.cfg,
autogenerate=False,
check=True,
)

def test_rev_autogen_check_no_changes(self):
self._env_fixture()
command.revision(self.cfg, autogenerate=True, check=True) # no problem

def test_rev_autogen_check_changes_detected(self):
self._env_fixture()
with mock.patch(
"alembic.operations.ops.UpgradeOps.as_diffs",
return_value=[('remove_column', None, 'foo', Column('old_data', VARCHAR()))]
):
assert_raises_message(
util.RevisionOpsNotEmptyError,
"Revision has upgrade ops to run:",
command.revision,
self.cfg,
autogenerate=True,
check=True,
)

def test_pk_constraint_normally_prevents_dupe_rows(self):
self._env_fixture()
Expand Down

0 comments on commit 2be534c

Please sign in to comment.