Commits

Mike Bayer committed c9d832d Merge

Merged in aodag/alembic/feature.uniqueconstraint (pull request #10)

Comments (0)

Files changed (2)

alembic/autogenerate.py

             "prefix":_sqlalchemy_autogenerate_prefix(autogen_context)
         }
 
+def _render_unique_constraint(constraint, autogen_context):
+    
+    return "%(prefix)sUniqueConstraint(%(cols)s)" % {
+        'cols': ",".join(["'%s'" % c.name for c in constraint.columns]),
+            "prefix":_sqlalchemy_autogenerate_prefix(autogen_context)
+        }
 _constraint_renderers = {
     schema.PrimaryKeyConstraint:_render_primary_key,
     schema.ForeignKeyConstraint:_render_foreign_key,
+    schema.UniqueConstraint:_render_unique_constraint,
     schema.CheckConstraint:_render_check_constraint
 }

tests/test_autogenerate.py

 from sqlalchemy import MetaData, Column, Table, Integer, String, Text, \
-    Numeric, CHAR, ForeignKey, DATETIME, TypeDecorator, CheckConstraint
+    Numeric, CHAR, ForeignKey, DATETIME, TypeDecorator, CheckConstraint, Unicode
 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("address_id", Integer, ForeignKey("address.id")),
             Column("timestamp", DATETIME, server_default="NOW()"),
             Column("amount", Numeric(5, 2)),
             autogenerate._add_table(t, self.autogen_context),
             "op.create_table('test',"
             "sa.Column('id', sa.Integer(), nullable=False),"
+            "sa.Column('name', sa.Unicode(length=255), nullable=True),"
             "sa.Column('address_id', sa.Integer(), nullable=True),"
             "sa.Column('timestamp', sa.DATETIME(), "
                 "server_default='NOW()', "
                 "nullable=True),"
             "sa.Column('amount', sa.Numeric(precision=5, scale=2), nullable=True),"
             "sa.ForeignKeyConstraint(['address_id'], ['address.id'], ),"
-            "sa.PrimaryKeyConstraint('id')"
+            "sa.PrimaryKeyConstraint('id'),"
+            "sa.UniqueConstraint('name')"
             ")"
         )