Commits

Mike Bayer committed 797b459

- [feature] Added support for UniqueConstraint
in autogenerate, courtesy Atsushi Odagiri

  • Participants
  • Parent commits b3ccfd8

Comments (0)

Files changed (3)

+0.3.2
+=====
+- [feature] Added support for UniqueConstraint
+  in autogenerate, courtesy Atsushi Odagiri
+
 0.3.1
 =====
 - [bug] bulk_insert() fixes:

File alembic/autogenerate.py

         }
 
 def _render_unique_constraint(constraint, autogen_context):
-    
-    return "%(prefix)sUniqueConstraint(%(cols)s)" % {
+    opts = []
+    if constraint.name:
+        opts.append(("name", "'%s'" % constraint.name))
+    return "%(prefix)sUniqueConstraint(%(cols)s%(opts)s)" % {
+        'opts':", " + (", ".join("%s=%s" % (k, v) for k, v in opts)) if opts else "",
         'cols': ",".join(["'%s'" % c.name for c in constraint.columns]),
             "prefix":_sqlalchemy_autogenerate_prefix(autogen_context)
         }

File tests/test_autogenerate.py

 from sqlalchemy import MetaData, Column, Table, Integer, String, Text, \
-    Numeric, CHAR, ForeignKey, DATETIME, TypeDecorator, CheckConstraint, Unicode
+    Numeric, CHAR, ForeignKey, DATETIME, TypeDecorator, CheckConstraint, Unicode,\
+    UniqueConstraint
 from sqlalchemy.types import NULLTYPE
 from sqlalchemy.engine.reflection import Inspector
 from alembic import autogenerate
         m = MetaData()
         t = Table('test', m,
             Column('id', Integer, primary_key=True),
-            Column('name', Unicode(255), unique=True),
+            Column('name', Unicode(255)),
             Column("address_id", Integer, ForeignKey("address.id")),
             Column("timestamp", DATETIME, server_default="NOW()"),
             Column("amount", Numeric(5, 2)),
+            UniqueConstraint("name", name="uq_name"),
+            UniqueConstraint("timestamp"),
         )
         eq_ignore_whitespace(
             autogenerate._add_table(t, self.autogen_context),
             "sa.Column('amount', sa.Numeric(precision=5, scale=2), nullable=True),"
             "sa.ForeignKeyConstraint(['address_id'], ['address.id'], ),"
             "sa.PrimaryKeyConstraint('id'),"
-            "sa.UniqueConstraint('name')"
+            "sa.UniqueConstraint('name', name='uq_name'),"
+            "sa.UniqueConstraint('timestamp')"
             ")"
         )