Skip to content

Commit 733197e

Browse files
DJHillszzzeek
authored andcommitted
Allow passoing ExcludeConstraint to DropConstraint.from_constraint
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
1 parent 0c1ad1e commit 733197e

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

alembic/autogenerate/render.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -398,22 +398,21 @@ def _add_check_constraint(constraint, autogen_context):
398398
def _drop_constraint(
399399
autogen_context: AutogenContext, op: ops.DropConstraintOp
400400
) -> str:
401-
if autogen_context._has_batch:
402-
template = "%(prefix)sdrop_constraint" "(%(name)r, type_=%(type)r)"
403-
else:
404-
template = (
405-
"%(prefix)sdrop_constraint"
406-
"(%(name)r, '%(table_name)s'%(schema)s, type_=%(type)r)"
407-
)
401+
prefix = _alembic_autogenerate_prefix(autogen_context)
402+
name = _render_gen_name(autogen_context, op.constraint_name)
403+
schema = _ident(op.schema) if op.schema else None
404+
type_ = _ident(op.constraint_type) if op.constraint_type else None
408405

409-
text = template % {
410-
"prefix": _alembic_autogenerate_prefix(autogen_context),
411-
"name": _render_gen_name(autogen_context, op.constraint_name),
412-
"table_name": _ident(op.table_name),
413-
"type": op.constraint_type,
414-
"schema": (", schema=%r" % _ident(op.schema)) if op.schema else "",
415-
}
416-
return text
406+
params_strs = []
407+
params_strs.append(repr(name))
408+
if not autogen_context._has_batch:
409+
params_strs.append(repr(_ident(op.table_name)))
410+
if schema is not None:
411+
params_strs.append(f"schema={schema!r}")
412+
if type_ is not None:
413+
params_strs.append(f"type_={type_!r}")
414+
415+
return f"{prefix}drop_constraint({', '.join(params_strs)})"
417416

418417

419418
@renderers.dispatch_for(ops.AddColumnOp)

alembic/operations/ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def from_constraint(cls, constraint: Constraint) -> DropConstraintOp:
171171
sqla_compat.constraint_name_or_none(constraint.name),
172172
constraint_table.name,
173173
schema=constraint_table.schema,
174-
type_=types[constraint.__visit_name__],
174+
type_=types.get(constraint.__visit_name__),
175175
_reverse=AddConstraintOp.from_constraint(constraint),
176176
)
177177

docs/build/unreleased/1300.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.. change::
2+
:tags: bug, operations
3+
:tickets: 1300
4+
5+
Added support for ``op.drop_constraint()`` to support PostrgreSQL
6+
``ExcludeConstraint`` objects, as well as other constraint-like objects
7+
that may be present in third party dialects, by resolving the ``type_``
8+
parameter to be ``None`` for this case. Autogenerate has also been
9+
enhanced to exclude the ``type_`` parameter from rendering within this
10+
command when ``type_`` is ``None``. Pull request courtesy David Hills.
11+
12+

tests/test_op.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,11 @@ def test_drop_constraint_type(self):
808808
op.drop_constraint("foo_bar_bat", "t1", type_="foreignkey")
809809
context.assert_("ALTER TABLE t1 DROP CONSTRAINT foo_bar_bat")
810810

811+
def test_drop_constraint_type_generic(self):
812+
context = op_fixture()
813+
op.drop_constraint("foo_bar_bat", "t1")
814+
context.assert_("ALTER TABLE t1 DROP CONSTRAINT foo_bar_bat")
815+
811816
def test_drop_constraint_legacy_type(self):
812817
"""#1245"""
813818
context = op_fixture()

tests/test_postgresql.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,30 @@ def test_inline_exclude_constraint_text(self):
12631263
"name='TExclID'))",
12641264
)
12651265

1266+
def test_drop_exclude_constraint(self):
1267+
"""test for #1300"""
1268+
1269+
autogen_context = self.autogen_context
1270+
1271+
m = MetaData()
1272+
t = Table(
1273+
"TTable", m, Column("XColumn", String), Column("YColumn", String)
1274+
)
1275+
1276+
op_obj = ops.DropConstraintOp.from_constraint(
1277+
ExcludeConstraint(
1278+
(t.c.XColumn, ">"),
1279+
where=t.c.XColumn != 2,
1280+
using="gist",
1281+
name="t_excl_x",
1282+
)
1283+
)
1284+
1285+
eq_ignore_whitespace(
1286+
autogenerate.render_op_text(autogen_context, op_obj),
1287+
"op.drop_constraint('t_excl_x', 'TTable')",
1288+
)
1289+
12661290
def test_json_type(self):
12671291
eq_ignore_whitespace(
12681292
autogenerate.render._repr_type(JSON(), self.autogen_context),

0 commit comments

Comments
 (0)