]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
add autogen render for drop_constraint.if_exists
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 19 May 2025 16:24:44 +0000 (12:24 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 19 May 2025 16:28:26 +0000 (12:28 -0400)
I've maintained all along that #1650 and #1626 are the same thing
and should have been one big PR.   Here we complete the autogen
portion of the #1650 PR that was omitted.

Fixes: #1650
Change-Id: I0e98feca6ce9aa908f27c8bf7af036d116f93404

alembic/autogenerate/render.py
docs/build/unreleased/1626.rst
docs/build/unreleased/1650.rst
tests/test_autogen_render.py

index 50e7057dbc20efda814a814cbd5bd0a39a9c1119..4bad437a263397908cf0c283d14d1a6684f2ac85 100644 (file)
@@ -442,7 +442,7 @@ def _drop_constraint(
     name = _render_gen_name(autogen_context, op.constraint_name)
     schema = _ident(op.schema) if op.schema else None
     type_ = _ident(op.constraint_type) if op.constraint_type else None
-
+    if_exists = op.if_exists
     params_strs = []
     params_strs.append(repr(name))
     if not autogen_context._has_batch:
@@ -451,6 +451,8 @@ def _drop_constraint(
             params_strs.append(f"schema={schema!r}")
     if type_ is not None:
         params_strs.append(f"type_={type_!r}")
+    if if_exists is not None:
+        params_strs.append(f"if_exists={if_exists}")
 
     return f"{prefix}drop_constraint({', '.join(params_strs)})"
 
index f64ac406d5d444ce54ca29d3476a23c99f9ee18d..664e195876c988e1af289de41cb58dbcee0ec85a 100644 (file)
@@ -1,5 +1,6 @@
 .. change::
     :tags: usecase, autogenerate, postgresql
+    :tickets: 1626
 
     Added :paramref:`.Operations.add_column.if_not_exists` and
     :paramref:`.Operations.drop_column.if_exists` to render ``IF [NOT] EXISTS``
index b52154ebc32713e0b8945dff7786cd0dba383e96..7e1bc379aa5f6186343509d3638114040723db79 100644 (file)
@@ -2,6 +2,8 @@
     :tags: usecase, operations
     :tickets: 1650
 
-    Added :paramref:`.op.drop_constraint.if_exists` parameter to
-    :func:`.op.drop_constraint` which will render "DROP CONSTRAINT IF EXISTS".
-    Pull request courtesy Aaron Griffin.
+    Added :paramref:`.Operations.drop_constraint.if_exists` parameter to
+    :meth:`.Operations.drop_constraint` which will render "DROP CONSTRAINT IF
+    EXISTS". The parameter also supports autogenerate rendering allowing them
+    to be turned on via a custom :class:`.Rewriter`.  Pull request courtesy
+    Aaron Griffin.
index be8cc19acc036ec10dc5af07b7422844b7597f1b..55e802307951d08868577fababbcf93f04b595ed 100644 (file)
@@ -451,6 +451,20 @@ class AutogenRenderTest(TestBase):
             "schema=foo.camel_schema, type_='unique')",
         )
 
+    def test_render_drop_unique_constraint_if_exists(self):
+        """
+        autogenerate.render._drop_constraint
+        """
+        t = self.table()
+        uq = UniqueConstraint(t.c.code, name="uq_test_code")
+        op_obj = ops.DropConstraintOp.from_constraint(uq)
+        op_obj.if_exists = True
+        eq_ignore_whitespace(
+            autogenerate.render_op_text(self.autogen_context, op_obj),
+            "op.drop_constraint('uq_test_code', 'test', "
+            "type_='unique', if_exists=True)",
+        )
+
     def test_add_fk_constraint(self):
         m = MetaData()
         Table("a", m, Column("id", Integer, primary_key=True))