From: Mike Bayer Date: Tue, 8 Oct 2013 21:08:48 +0000 (-0400) Subject: - use a "signature" for unique constraints so that we can also check on X-Git-Tag: rel_0_6_1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56a178d2181c84ab8bf2edbe6fdf8439f4;p=thirdparty%2Fsqlalchemy%2Falembic.git - use a "signature" for unique constraints so that we can also check on non-named unique constraints, e.g. those made by the unique=True flag --- diff --git a/alembic/autogenerate/compare.py b/alembic/autogenerate/compare.py index 2b2e39b1..ce2728f0 100644 --- a/alembic/autogenerate/compare.py +++ b/alembic/autogenerate/compare.py @@ -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) diff --git a/tests/test_autogenerate.py b/tests/test_autogenerate.py index 7517ce83..e8859dcb 100644 --- a/tests/test_autogenerate.py +++ b/tests/test_autogenerate.py @@ -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),