if const._create_rule is None or const._create_rule(self):
self._exec(schema.AddConstraint(const))
- def drop_constraint(self, const: Constraint) -> None:
- self._exec(schema.DropConstraint(const))
+ def drop_constraint(self, const: Constraint, **kw: Any) -> None:
+ self._exec(schema.DropConstraint(const, **kw))
def rename_table(
self,
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
) -> None:
r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
quoting of the schema outside of the default behavior, use
the SQLAlchemy construct
:class:`~sqlalchemy.sql.elements.quoted_name`.
+ :param if_exists: If True, adds IF EXISTS operator when
+ dropping the constraint
+
+ .. versionadded:: 1.15.4
"""
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
) -> None:
r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
quoting of the schema outside of the default behavior, use
the SQLAlchemy construct
:class:`~sqlalchemy.sql.elements.quoted_name`.
+ :param if_exists: If True, adds IF EXISTS operator when
+ dropping the constraint
+
+ .. versionadded:: 1.15.4
""" # noqa: E501
...
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
_reverse: Optional[AddConstraintOp] = None,
) -> None:
self.constraint_name = constraint_name
self.table_name = table_name
self.constraint_type = type_
self.schema = schema
+ self.if_exists = if_exists
self._reverse = _reverse
def reverse(self) -> AddConstraintOp:
type_: Optional[str] = None,
*,
schema: Optional[str] = None,
+ if_exists: Optional[bool] = None,
) -> None:
r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
quoting of the schema outside of the default behavior, use
the SQLAlchemy construct
:class:`~sqlalchemy.sql.elements.quoted_name`.
+ :param if_exists: If True, adds IF EXISTS operator when
+ dropping the constraint
+
+ .. versionadded:: 1.15.4
"""
- op = cls(constraint_name, table_name, type_=type_, schema=schema)
+ op = cls(constraint_name, table_name, type_=type_, schema=schema, if_exists=if_exists)
return operations.invoke(op)
@classmethod
def drop_constraint(
operations: "Operations", operation: "ops.DropConstraintOp"
) -> None:
+ kw = {}
+ if operation.if_exists is not None:
+ kw["if_exists"] = operation.if_exists
operations.impl.drop_constraint(
operations.schema_obj.generic_constraint(
operation.constraint_name,
operation.table_name,
operation.constraint_type,
schema=operation.schema,
- )
+ ),
+ **kw
)
op.drop_constraint("foo_bar_bat", "t1", schema="foo")
context.assert_("ALTER TABLE foo.t1 DROP CONSTRAINT foo_bar_bat")
+ def test_drop_constraint_if_exists(self):
+ context = op_fixture()
+ op.drop_constraint("foo_bar_bat", "t1", if_exists=True)
+ context.assert_("ALTER TABLE t1 DROP CONSTRAINT IF EXISTS foo_bar_bat")
+
def test_create_index(self):
context = op_fixture()
op.create_index("ik_test", "t1", ["foo", "bar"])