From 92a4f5c602e6b94920da33afbbb4d163e1226eb1 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 19 May 2025 12:24:44 -0400 Subject: [PATCH] add autogen render for drop_constraint.if_exists 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 | 4 +++- docs/build/unreleased/1626.rst | 1 + docs/build/unreleased/1650.rst | 8 +++++--- tests/test_autogen_render.py | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index 50e7057d..4bad437a 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -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)})" diff --git a/docs/build/unreleased/1626.rst b/docs/build/unreleased/1626.rst index f64ac406..664e1958 100644 --- a/docs/build/unreleased/1626.rst +++ b/docs/build/unreleased/1626.rst @@ -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`` diff --git a/docs/build/unreleased/1650.rst b/docs/build/unreleased/1650.rst index b52154eb..7e1bc379 100644 --- a/docs/build/unreleased/1650.rst +++ b/docs/build/unreleased/1650.rst @@ -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. diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index be8cc19a..55e80230 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -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)) -- 2.47.3