]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- Repaired support for the :meth:`.BatchOperations.create_index`
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 27 Mar 2015 19:15:55 +0000 (15:15 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 27 Mar 2015 19:15:55 +0000 (15:15 -0400)
directive, which was mis-named internally such that the operation
within a batch context could not proceed.
fixes #287

alembic/batch.py
docs/build/changelog.rst
tests/test_batch.py

index e97f74e6723996f2c18517877d4469cfd2a3207e..6e5dc75d0e5ff4a4189e0d30cd2633da725cadcf 100644 (file)
@@ -271,7 +271,7 @@ class ApplyBatchImpl(object):
         except KeyError:
             raise ValueError("No such constraint: '%s'" % const.name)
 
-    def add_index(self, idx):
+    def create_index(self, idx):
         self.indexes[idx.name] = idx
 
     def drop_index(self, idx):
index 7cbe49cfea04ea6a1d35ab67e2038ed280564ebd..1d730e1ed205c45bd29be9d71f68374245c70937 100644 (file)
@@ -3,6 +3,17 @@
 Changelog
 ==========
 
+.. changelog::
+    :version: 0.7.6
+
+    .. change::
+      :tags: bug, batch
+      :tickets: 287
+
+      Repaired support for the :meth:`.BatchOperations.create_index`
+      directive, which was mis-named internally such that the operation
+      within a batch context could not proceed.
+
 .. changelog::
     :version: 0.7.5
     :released: March 19, 2015
index 625ccc07af81283f618dce02578b28be3731a67a..76f0c1298233d26b974cba94cd564cef2edb4aab 100644 (file)
@@ -9,6 +9,7 @@ from alembic.operations import Operations
 from alembic.batch import ApplyBatchImpl
 from alembic.migration import MigrationContext
 
+from sqlalchemy import inspect
 from sqlalchemy import Integer, Table, Column, String, MetaData, ForeignKey, \
     UniqueConstraint, ForeignKeyConstraint, Index, Boolean, CheckConstraint, \
     Enum
@@ -464,11 +465,11 @@ class BatchApplyTest(TestBase):
             impl, colnames=['id', 'x', 'y'],
             ddl_not_contains="CONSTRAINT uq1 UNIQUE")
 
-    def test_add_index(self):
+    def test_create_index(self):
         impl = self._simple_fixture()
         ix = self.op._index('ix1', 'tname', ['y'])
 
-        impl.add_index(ix)
+        impl.create_index(ix)
         self._assert_impl(
             impl, colnames=['id', 'x', 'y'],
             ddl_contains="CREATE INDEX ix1")
@@ -876,6 +877,43 @@ class BatchRoundTripTest(TestBase):
             {"id": 5, "data": "d5", "x": 9, 'data2': 'hi'}
         ])
 
+    def test_create_drop_index(self):
+        insp = inspect(config.db)
+        eq_(
+            insp.get_indexes('foo'), []
+        )
+
+        with self.op.batch_alter_table("foo", recreate='always') as batch_op:
+            batch_op.create_index(
+                batch_op.f('ix_data'), ['data'], unique=True)
+
+        self._assert_data([
+            {"id": 1, "data": "d1", "x": 5},
+            {"id": 2, "data": "22", "x": 6},
+            {"id": 3, "data": "8.5", "x": 7},
+            {"id": 4, "data": "9.46", "x": 8},
+            {"id": 5, "data": "d5", "x": 9}
+        ])
+
+        insp = inspect(config.db)
+        eq_(
+            [
+                dict(unique=ix['unique'],
+                     name=ix['name'],
+                     column_names=ix['column_names'])
+                for ix in insp.get_indexes('foo')
+            ],
+            [{'unique': True, 'name': 'ix_data', 'column_names': ['data']}]
+        )
+
+        with self.op.batch_alter_table("foo", recreate='always') as batch_op:
+            batch_op.drop_index('ix_data')
+
+        insp = inspect(config.db)
+        eq_(
+            insp.get_indexes('foo'), []
+        )
+
 
 class BatchRoundTripMySQLTest(BatchRoundTripTest):
     __only_on__ = "mysql"
@@ -892,6 +930,9 @@ class BatchRoundTripMySQLTest(BatchRoundTripTest):
     def test_change_type(self):
         super(BatchRoundTripMySQLTest, self).test_change_type()
 
+    def test_create_drop_index(self):
+        super(BatchRoundTripMySQLTest, self).test_create_drop_index()
+
 
 class BatchRoundTripPostgresqlTest(BatchRoundTripTest):
     __only_on__ = "postgresql"
@@ -900,3 +941,5 @@ class BatchRoundTripPostgresqlTest(BatchRoundTripTest):
     def test_change_type(self):
         super(BatchRoundTripPostgresqlTest, self).test_change_type()
 
+    def test_create_drop_index(self):
+        super(BatchRoundTripPostgresqlTest, self).test_create_drop_index()