From 2af6e438c9558eeddbfe0d27d5690660f3b798cf Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 27 Mar 2015 15:15:55 -0400 Subject: - 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. fixes #287 --- alembic/batch.py | 2 +- docs/build/changelog.rst | 11 +++++++++++ tests/test_batch.py | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/alembic/batch.py b/alembic/batch.py index e97f74e..6e5dc75 100644 --- a/alembic/batch.py +++ b/alembic/batch.py @@ -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): diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 7cbe49c..1d730e1 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -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 diff --git a/tests/test_batch.py b/tests/test_batch.py index 625ccc0..76f0c12 100644 --- a/tests/test_batch.py +++ b/tests/test_batch.py @@ -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() -- cgit v1.2.1