]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- ensure DropIndex and other ops return the full object it received
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 17 Jul 2015 17:18:47 +0000 (13:18 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 17 Jul 2015 17:18:47 +0000 (13:18 -0400)
from autogenerate; in the immediate sense this should help with
modelsmigrationsync tests

alembic/operations/ops.py
tests/test_op.py

index 71e85159c9f53bfa21fed7cba85454f76929cc7e..7f26e65346eb2d50a64b3cfac86c64b7d19b647a 100644 (file)
@@ -812,6 +812,9 @@ class DropIndexOp(MigrateOperation):
         )
 
     def to_index(self, migration_context=None):
+        if self._orig_index is not None:
+            return self._orig_index
+
         schema_obj = schemaobj.SchemaObjects(migration_context)
 
         # need a dummy column name here since SQLAlchemy
index 36d4774f1626bec26b34f15e12ef3aded8ce6759..814d98e2b6dc7190b43beacdf997be48351df9a7 100644 (file)
@@ -7,10 +7,11 @@ from sqlalchemy import event
 
 from alembic import op
 from alembic.testing.fixtures import op_fixture
-from alembic.testing import eq_, assert_raises_message
+from alembic.testing import eq_, assert_raises_message, is_
 from alembic.testing import mock
 from alembic.testing.fixtures import TestBase
 from alembic.testing import config
+from alembic.operations import schemaobj, ops
 
 
 @event.listens_for(Table, "after_parent_attach")
@@ -938,3 +939,62 @@ class CustomOpTest(TestBase):
         context = op_fixture()
         op.create_sequence('foob')
         context.assert_("CREATE SEQUENCE foob")
+
+
+class EnsureOrigObjectFromToTest(TestBase):
+    """the to_XYZ and from_XYZ methods are used heavily in autogenerate.
+
+    It's critical that these methods, at least the "drop" form,
+    always return the *same* object if available so that all the info
+    passed into to_XYZ is maintained in the from_XYZ.
+
+
+    """
+
+    def test_drop_index(self):
+        schema_obj = schemaobj.SchemaObjects()
+        idx = schema_obj.index('x', 'y', ['z'])
+        op = ops.DropIndexOp.from_index(idx)
+        is_(
+            op.to_index(), idx
+        )
+
+    def test_create_index(self):
+        schema_obj = schemaobj.SchemaObjects()
+        idx = schema_obj.index('x', 'y', ['z'])
+        op = ops.CreateIndexOp.from_index(idx)
+        is_(
+            op.to_index(), idx
+        )
+
+    def test_drop_table(self):
+        schema_obj = schemaobj.SchemaObjects()
+        table = schema_obj.table('x', Column('q', Integer))
+        op = ops.DropTableOp.from_table(table)
+        is_(
+            op.to_table(), table
+        )
+
+    def test_create_table(self):
+        schema_obj = schemaobj.SchemaObjects()
+        table = schema_obj.table('x', Column('q', Integer))
+        op = ops.CreateTableOp.from_table(table)
+        is_(
+            op.to_table(), table
+        )
+
+    def test_drop_unique_constraint(self):
+        schema_obj = schemaobj.SchemaObjects()
+        const = schema_obj.unique_constraint('x', 'foobar', ['a'])
+        op = ops.DropConstraintOp.from_constraint(const)
+        is_(
+            op.to_constraint(), const
+        )
+
+    def test_drop_constraint_not_available(self):
+        op = ops.DropConstraintOp('x', 'y', type_='unique')
+        assert_raises_message(
+            ValueError,
+            "constraint cannot be produced",
+            op.to_constraint
+        )