summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNathan Louie <nxlouie@umich.edu>2022-12-13 12:58:09 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2022-12-15 11:44:48 -0500
commit4678d7f1da009689449a6550768139c32b50c646 (patch)
tree910b93354c60c1424fb00306f58474c4fcbc1674 /tests
parent3a5a7f33320d88363be18776a2076a19e600a593 (diff)
downloadalembic-4678d7f1da009689449a6550768139c32b50c646.tar.gz
add check command for upgrade diffs
Added new Alembic command ``alembic check``. This performs the widely requested feature of running an "autogenerate" comparison between the current database and the :class:`.MetaData` that's currently set up for autogenerate, returning an error code if the two do not match, based on current autogenerate settings. Pull request courtesy Nathan Louie. As this is a new feature we will call this 1.9.0 Fixes: #724 Closes: #1101 Pull-request: https://github.com/sqlalchemy/alembic/pull/1101 Pull-request-sha: 807ed545df70e7a10b913e2951a1b636f138a4ff Change-Id: I03b146eaf762be464a0ff0858ff5730cc9366c84
Diffstat (limited to 'tests')
-rw-r--r--tests/test_command.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/test_command.py b/tests/test_command.py
index e136c4e..5ec3567 100644
--- a/tests/test_command.py
+++ b/tests/test_command.py
@@ -9,7 +9,9 @@ from typing import cast
from sqlalchemy import exc as sqla_exc
from sqlalchemy import text
+from sqlalchemy import VARCHAR
from sqlalchemy.engine import Engine
+from sqlalchemy.sql.schema import Column
from alembic import __version__
from alembic import command
@@ -537,6 +539,63 @@ finally:
command.revision(self.cfg, sql=True)
+class CheckTest(TestBase):
+ def setUp(self):
+ self.env = staging_env()
+ self.cfg = _sqlite_testing_config()
+
+ def tearDown(self):
+ clear_staging_env()
+
+ def _env_fixture(self, version_table_pk=True):
+ env_file_fixture(
+ """
+
+from sqlalchemy import MetaData, engine_from_config
+target_metadata = MetaData()
+
+engine = engine_from_config(
+ config.get_section(config.config_ini_section),
+ prefix='sqlalchemy.')
+
+connection = engine.connect()
+
+context.configure(
+ connection=connection, target_metadata=target_metadata,
+ version_table_pk=%r
+)
+
+try:
+ with context.begin_transaction():
+ context.run_migrations()
+finally:
+ connection.close()
+ engine.dispose()
+
+"""
+ % (version_table_pk,)
+ )
+
+ def test_check_no_changes(self):
+ self._env_fixture()
+ command.check(self.cfg) # no problem
+
+ def test_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.AutogenerateDiffsDetected,
+ r"New upgrade operations detected: \[\('remove_column'",
+ command.check,
+ self.cfg,
+ )
+
+
class _StampTest:
def _assert_sql(self, emitted_sql, origin, destinations):
ins_expr = (