Skip to content

Commit

Permalink
Allow passoing ExcludeConstraint to DropConstraint.from_constraint
Browse files Browse the repository at this point in the history
Added support for ``op.drop_constraint()`` to support PostrgreSQL
``ExcludeConstraint`` objects, as well as other constraint-like objects
that may be present in third party dialects, by resolving the ``type_``
parameter to be ``None`` for this case.   Autogenerate has also been
enhanced to exclude the ``type_`` parameter from rendering within this
command when  ``type_`` is ``None``.  Pull request courtesy David Hills.

Fixes: #1300
Closes: #1301
Pull-request: #1301
Pull-request-sha: b8643d1

Change-Id: I25599d24a8bba455ab1d9b88843d8d84627a72d5
  • Loading branch information
DJHills authored and zzzeek committed Aug 31, 2023
1 parent 0c1ad1e commit 733197e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
29 changes: 14 additions & 15 deletions alembic/autogenerate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,22 +398,21 @@ def _add_check_constraint(constraint, autogen_context):
def _drop_constraint(
autogen_context: AutogenContext, op: ops.DropConstraintOp
) -> str:
if autogen_context._has_batch:
template = "%(prefix)sdrop_constraint" "(%(name)r, type_=%(type)r)"
else:
template = (
"%(prefix)sdrop_constraint"
"(%(name)r, '%(table_name)s'%(schema)s, type_=%(type)r)"
)
prefix = _alembic_autogenerate_prefix(autogen_context)
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

text = template % {
"prefix": _alembic_autogenerate_prefix(autogen_context),
"name": _render_gen_name(autogen_context, op.constraint_name),
"table_name": _ident(op.table_name),
"type": op.constraint_type,
"schema": (", schema=%r" % _ident(op.schema)) if op.schema else "",
}
return text
params_strs = []
params_strs.append(repr(name))
if not autogen_context._has_batch:
params_strs.append(repr(_ident(op.table_name)))
if schema is not None:
params_strs.append(f"schema={schema!r}")
if type_ is not None:
params_strs.append(f"type_={type_!r}")

return f"{prefix}drop_constraint({', '.join(params_strs)})"


@renderers.dispatch_for(ops.AddColumnOp)
Expand Down
2 changes: 1 addition & 1 deletion alembic/operations/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def from_constraint(cls, constraint: Constraint) -> DropConstraintOp:
sqla_compat.constraint_name_or_none(constraint.name),
constraint_table.name,
schema=constraint_table.schema,
type_=types[constraint.__visit_name__],
type_=types.get(constraint.__visit_name__),
_reverse=AddConstraintOp.from_constraint(constraint),
)

Expand Down
12 changes: 12 additions & 0 deletions docs/build/unreleased/1300.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.. change::
:tags: bug, operations
:tickets: 1300

Added support for ``op.drop_constraint()`` to support PostrgreSQL
``ExcludeConstraint`` objects, as well as other constraint-like objects
that may be present in third party dialects, by resolving the ``type_``
parameter to be ``None`` for this case. Autogenerate has also been
enhanced to exclude the ``type_`` parameter from rendering within this
command when ``type_`` is ``None``. Pull request courtesy David Hills.


5 changes: 5 additions & 0 deletions tests/test_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ def test_drop_constraint_type(self):
op.drop_constraint("foo_bar_bat", "t1", type_="foreignkey")
context.assert_("ALTER TABLE t1 DROP CONSTRAINT foo_bar_bat")

def test_drop_constraint_type_generic(self):
context = op_fixture()
op.drop_constraint("foo_bar_bat", "t1")
context.assert_("ALTER TABLE t1 DROP CONSTRAINT foo_bar_bat")

def test_drop_constraint_legacy_type(self):
"""#1245"""
context = op_fixture()
Expand Down
24 changes: 24 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,30 @@ def test_inline_exclude_constraint_text(self):
"name='TExclID'))",
)

def test_drop_exclude_constraint(self):
"""test for #1300"""

autogen_context = self.autogen_context

m = MetaData()
t = Table(
"TTable", m, Column("XColumn", String), Column("YColumn", String)
)

op_obj = ops.DropConstraintOp.from_constraint(
ExcludeConstraint(
(t.c.XColumn, ">"),
where=t.c.XColumn != 2,
using="gist",
name="t_excl_x",
)
)

eq_ignore_whitespace(
autogenerate.render_op_text(autogen_context, op_obj),
"op.drop_constraint('t_excl_x', 'TTable')",
)

def test_json_type(self):
eq_ignore_whitespace(
autogenerate.render._repr_type(JSON(), self.autogen_context),
Expand Down

0 comments on commit 733197e

Please sign in to comment.