]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
MySQL: Ignore unique indexes when removing implicit indexes
authorJohannes Erdfelt <johannes@erdfelt.com>
Thu, 4 Dec 2014 17:42:46 +0000 (09:42 -0800)
committerJohannes Erdfelt <johannes@erdfelt.com>
Thu, 4 Dec 2014 17:55:07 +0000 (09:55 -0800)
MySQL will implicitly create indexes when using foreign keys. Alembic
attempts to remove those implicit indexes so they don't appear as
removes when comparing metadata.

However, unique indexes with the same name as a column are considered
as possibly implicitly created causing alembic to emit a spurious
'remove_constraint'.

Since MySQL will never implicitly create unique indexes, they can be
safely ignored when removing the implicit indexes.

Fixes #251

alembic/ddl/mysql.py
tests/test_autogen_indexes.py

index b93d29fc492555f493844e1fb95fabe0890902a0..cb9d6349db2c8edda52d18a0d98d2d6d7f7ca957 100644 (file)
@@ -102,6 +102,8 @@ class MySQLImpl(DefaultImpl):
             # the name of the index may be the column name or it may
             # be the name of the FK constraint.
             for col in idx.columns:
+                if idx.unique:
+                    continue
                 if idx.name == col.name:
                     conn_indexes.remove(idx)
                     removed.add(idx.name)
index ad839c8317d480627b318513aef6a8032457a1b6..e2d9b4eafc39daabeda0b15e7fa51c3a1dcd40eb 100644 (file)
@@ -586,6 +586,25 @@ class MySQLUniqueIndexTest(AutogenerateUniqueIndexTest):
         else:
             assert False, "unexpected success"
 
+    def test_unique_index_foreign_key(self):
+        m1 = MetaData()
+        m2 = MetaData()
+
+        Table('unq_idx', m1,
+              Column('id', Integer, primary_key=True),
+              Column('x', String(20)),
+              Index('x', 'x', unique=True)
+              )
+
+        Table('unq_idx', m2,
+              Column('id', Integer, primary_key=True),
+              Column('x', String(20)),
+              Index('x', 'x', unique=True)
+              )
+
+        diffs = self._fixture(m1, m2)
+        eq_(diffs, [])
+
 
 class NoUqReflectionIndexTest(NoUqReflection, AutogenerateUniqueIndexTest):
     reports_unique_constraints = False