]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- drop constraint
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 23 Nov 2011 22:17:31 +0000 (17:17 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 23 Nov 2011 22:17:31 +0000 (17:17 -0500)
- ensure table attachment event occurs for unique constraint

alembic/ddl/impl.py
alembic/op.py
tests/test_op.py

index ffbf6d1282e42292a7239077bf077f8d3921ea18..c2db03c9c9adb4dd6b73e31f8f553dfbf712f26e 100644 (file)
@@ -96,6 +96,9 @@ class DefaultImpl(object):
     def add_constraint(self, const):
         self._exec(schema.AddConstraint(const))
 
+    def drop_constraint(self, const):
+        self._exec(schema.DropConstraint(const))
+
     def create_table(self, table):
         self._exec(schema.CreateTable(table))
         for index in table.indexes:
index 1732b40dc1997bd52e8ddc2ac87b29485d4fc03a..d4a76ce3a0516858c4943cae1dcd3bb7c7036333 100644 (file)
@@ -10,6 +10,7 @@ __all__ = sorted([
             'alter_column', 
             'add_column',
             'drop_column',
+            'drop_constraint',
             'create_foreign_key', 
             'create_table',
             'drop_table',
@@ -41,7 +42,11 @@ def _foreign_key_constraint(name, source, referent, local_cols, remote_cols):
 def _unique_constraint(name, source, local_cols, **kw):
     t = schema.Table(source, schema.MetaData(), 
                 *[schema.Column(n, NULLTYPE) for n in local_cols])
-    return schema.UniqueConstraint(*t.c, name=name, **kw)
+    uq = schema.UniqueConstraint(*t.c, name=name, **kw)
+    # TODO: need event tests to ensure the event
+    # is fired off here
+    t.append_constraint(uq)
+    return uq
 
 def _table(name, *columns, **kw):
     m = schema.MetaData()
@@ -290,6 +295,13 @@ def drop_index(name):
     """
     get_impl().drop_index(_index(name, 'foo', []))
 
+def drop_constraint(name, tablename):
+    """Drop a constraint of the given name"""
+    t = _table(tablename)
+    const = schema.Constraint(name=name)
+    t.append_constraint(const)
+    get_impl().drop_constraint(const)
+
 def bulk_insert(table, rows):
     """Issue a "bulk insert" operation using the current change context.
     
index d917384ef6e1eef7c2ece132bb2180c85c747944..7d63e54156d311ee499c70a3ecdbd8e1402036be 100644 (file)
@@ -77,6 +77,13 @@ def test_add_unique_constraint():
         "ALTER TABLE t1 ADD CONSTRAINT uk_test UNIQUE (foo, bar)"
     )
 
+def test_drop_constraint():
+    context = _op_fixture()
+    op.drop_constraint('foo_bar_bat', 't1')
+    context.assert_(
+        "ALTER TABLE t1 DROP CONSTRAINT foo_bar_bat"
+    )
+
 def test_create_index():
     context = _op_fixture()
     op.create_index('ik_test', 't1', ['foo', 'bar'])