]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Add kwargs support to DropIndexOp autogenerate render
authorJet Zhou <jet@gem.com>
Thu, 27 May 2021 18:26:51 +0000 (14:26 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 27 May 2021 18:31:32 +0000 (14:31 -0400)
Fixed issue where dialect-specific keyword arguments within the
:class:`.DropIndex` operation directive would not render in the
autogenerated Python code. As support was improved for adding dialect
specific arguments to directives as part of :ticket:`803`, in particular
arguments such as "postgresql_concurrently" which apply to the actual
create/drop of the index, support was needed for these to render even in a
drop index operation. Pull request courtesy Jet Zhou.

Fixes: #849
Closes: #852
Pull-request: https://github.com/sqlalchemy/alembic/pull/852
Pull-request-sha: 6392a287179ed746f709ba1e0e07ccab3ea8e4c6

Change-Id: I9b602178c32d6c6a41c0dbe0969a19bd4fa329bd

alembic/autogenerate/render.py
docs/build/unreleased/849.rst [new file with mode: 0644]
tests/test_autogen_render.py

index 58b469c5010d1b3d8150e4efdbfe096c48c37f52..79d02615ac18a06729975855026d886a1fc46ead 100644 (file)
@@ -272,14 +272,16 @@ def _add_index(autogen_context, op):
 
 @renderers.dispatch_for(ops.DropIndexOp)
 def _drop_index(autogen_context, op):
+    index = op.to_index()
+
     has_batch = autogen_context._has_batch
 
     if has_batch:
-        tmpl = "%(prefix)sdrop_index(%(name)r)"
+        tmpl = "%(prefix)sdrop_index(%(name)r%(kwargs)s)"
     else:
         tmpl = (
             "%(prefix)sdrop_index(%(name)r, "
-            "table_name=%(table_name)r%(schema)s)"
+            "table_name=%(table_name)r%(schema)s%(kwargs)s)"
         )
 
     text = tmpl % {
@@ -287,6 +289,18 @@ def _drop_index(autogen_context, op):
         "name": _render_gen_name(autogen_context, op.index_name),
         "table_name": _ident(op.table_name),
         "schema": ((", schema=%r" % _ident(op.schema)) if op.schema else ""),
+        "kwargs": (
+            ", "
+            + ", ".join(
+                [
+                    "%s=%s"
+                    % (key, _render_potential_expr(val, autogen_context))
+                    for key, val in index.kwargs.items()
+                ]
+            )
+        )
+        if len(index.kwargs)
+        else "",
     }
     return text
 
diff --git a/docs/build/unreleased/849.rst b/docs/build/unreleased/849.rst
new file mode 100644 (file)
index 0000000..b6afb33
--- /dev/null
@@ -0,0 +1,11 @@
+.. change::
+    :tags: bug, autogenerate
+    :tickets: 849
+
+    Fixed issue where dialect-specific keyword arguments within the
+    :class:`.DropIndex` operation directive would not render in the
+    autogenerated Python code. As support was improved for adding dialect
+    specific arguments to directives as part of :ticket:`803`, in particular
+    arguments such as "postgresql_concurrently" which apply to the actual
+    create/drop of the index, support was needed for these to render even in a
+    drop index operation. Pull request courtesy Jet Zhou.
index dbd3b10914c5f8f60a96fe97379e7397a7b45e55..d4ce43283af588826a866218501fdcc4035cc003 100644 (file)
@@ -240,6 +240,23 @@ class AutogenRenderTest(TestBase):
             "op.drop_index('test_active_code_idx', table_name='test')",
         )
 
+    @testing.emits_warning("Can't validate argument ")
+    def test_render_drop_index_custom_kwarg(self):
+        t = Table(
+            "test",
+            MetaData(),
+            Column("id", Integer, primary_key=True),
+            Column("active", Boolean()),
+            Column("code", String(255)),
+        )
+        idx = Index(None, t.c.active, t.c.code, somedialect_foobar="option")
+        op_obj = ops.DropIndexOp.from_index(idx)
+        eq_ignore_whitespace(
+            autogenerate.render_op_text(self.autogen_context, op_obj),
+            "op.drop_index(op.f('ix_test_active'), table_name='test', "
+            "somedialect_foobar='option')",
+        )
+
     def test_drop_index_batch(self):
         """
         autogenerate.render._drop_index