From: Mike Bayer Date: Wed, 23 Nov 2011 22:17:31 +0000 (-0500) Subject: - drop constraint X-Git-Tag: rel_0_1_0~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc3bed36eb36abcc954f5ff9a6a9216afed9b53f;p=thirdparty%2Fsqlalchemy%2Falembic.git - drop constraint - ensure table attachment event occurs for unique constraint --- diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index ffbf6d12..c2db03c9 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -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: diff --git a/alembic/op.py b/alembic/op.py index 1732b40d..d4a76ce3 100644 --- a/alembic/op.py +++ b/alembic/op.py @@ -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. diff --git a/tests/test_op.py b/tests/test_op.py index d917384e..7d63e541 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -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'])