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: https://github.com/sqlalchemy/alembic/pull/1301
Pull-request-sha:
b8643d19ab1930554e1137b64ffea8b8e2c74ec7
Change-Id: I25599d24a8bba455ab1d9b88843d8d84627a72d5
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)
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),
)
--- /dev/null
+.. 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.
+
+
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()
"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),