]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- use a "signature" for unique constraints so that we can also check on
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Oct 2013 21:08:48 +0000 (17:08 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Oct 2013 21:08:48 +0000 (17:08 -0400)
non-named unique constraints, e.g. those made by the unique=True flag

alembic/autogenerate/compare.py
tests/test_autogenerate.py

index 2b2e39b108d9d582764988bfdfa11130ea74a006..ce2728f0b4da9d9b579cc0fda8044e45ad14138d 100644 (file)
@@ -142,14 +142,30 @@ def _compare_columns(schema, tname, object_filters, conn_table, metadata_table,
         if col_diff:
             diffs.append(col_diff)
 
+class _uq_constraint_sig(object):
+    def __init__(self, const):
+        self.const = const
+        self.name = const.name
+        self.sig = tuple(sorted([col.name for col in const.columns]))
+
+    def __eq__(self, other):
+        if self.name is not None and other.name is not None:
+            return other.name == self.name
+        else:
+            return self.sig == other.sig
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
+
+    def __hash__(self):
+        return hash(self.sig)
 
 def _compare_uniques(schema, tname, object_filters, conn_table,
             metadata_table, diffs, autogen_context, inspector):
 
     m_objs = dict(
-        (i.name, i) for i in metadata_table.constraints
-        if isinstance(i, sa_schema.UniqueConstraint)
-        and i.name is not None
+        (_uq_constraint_sig(uq), uq) for uq in metadata_table.constraints
+        if isinstance(uq, sa_schema.UniqueConstraint)
     )
     m_keys = set(m_objs.keys())
 
@@ -164,9 +180,9 @@ def _compare_uniques(schema, tname, object_filters, conn_table,
         return None
 
     c_objs = dict(
-        (i['name'], _make_unique_constraint(i, conn_table))
-        for i in conn_uniques
-        if i['name'] is not None
+        (_uq_constraint_sig(uq), uq)
+        for uq in
+        (_make_unique_constraint(uq_def, conn_table) for uq_def in conn_uniques)
     )
     c_keys = set(c_objs)
 
index 7517ce8368dff32c23bb86f82d68306a3c1b8e3d..e8859dcb6da5a4b5022bc64b8c239099cdf4a810 100644 (file)
@@ -777,6 +777,11 @@ class AutogenerateUniqueIndexTest(AutogenTest, TestCase):
             ),
             Index('order_user_id_amount_idx', 'user_id', 'amount')
         )
+
+        Table('item', m,
+                Column('x', Integer),
+                UniqueConstraint('x', name="db_generated_name")
+            )
         return m
 
 
@@ -809,6 +814,12 @@ class AutogenerateUniqueIndexTest(AutogenTest, TestCase):
             CheckConstraint('amount >= 0', name='ck_order_amount')
         )
 
+        # test mismatch between unique=True and
+        # named uq constraint
+        Table('item', m,
+                Column('x', Integer, unique=True)
+            )
+
         Table('extra', m,
                 Column('foo', Integer, index=True),
                 Column('bar', Integer),