]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
ExecuteSQLOp rendering respects alembic_module_prefix
authorAvery Fischer <avery@averyjfischer.com>
Mon, 12 May 2025 11:50:08 +0000 (13:50 +0200)
committerAvery Fischer <avery@averyjfischer.com>
Mon, 12 May 2025 14:16:03 +0000 (16:16 +0200)
Rendering ExecuteSQLOp did not respect the alembic_module_prefix setting
due
to having op. hard-coded in its template.

This change updates the render logic to use the same
_alembic_autogenerate_prefix() as it seems the rest of the ops use.

---

Fixes #1656

alembic/autogenerate/render.py
tests/test_autogen_render.py

index ffc277db81d174333d0f8892d28e464343ebcc37..66c67673360380e28ad9f9cc3dff8841a11ae131 100644 (file)
@@ -1122,7 +1122,10 @@ def _execute_sql(autogen_context: AutogenContext, op: ops.ExecuteSQLOp) -> str:
             "Autogenerate rendering of SQL Expression language constructs "
             "not supported here; please use a plain SQL string"
         )
-    return "op.execute(%r)" % op.sqltext
+    return "{prefix}execute({sqltext!r})".format(
+        prefix=_alembic_autogenerate_prefix(autogen_context),
+        sqltext=op.sqltext,
+    )
 
 
 renderers = default_renderers.branch()
index ed31183e8c0a2242c0dec692f751ccb3e72e3623..cf71da071a90afa39e80cd0b59ae8477e949e6ab 100644 (file)
@@ -2035,6 +2035,15 @@ class AutogenRenderTest(TestBase):
             op_obj,
         )
 
+    @testing.combinations(("test.",), (None,), argnames="alembic_module_prefix")
+    def test_render_executesql_alembic_module_prefix(self, alembic_module_prefix):
+        self.autogen_context.opts.update(alembic_module_prefix=alembic_module_prefix)
+        op_obj = ops.ExecuteSQLOp("drop table foo")
+        eq_(
+            autogenerate.render_op_text(self.autogen_context, op_obj),
+            f"{alembic_module_prefix or ''}execute('drop table foo')",
+        )
+
     def test_render_alter_column_modify_comment(self):
         op_obj = ops.AlterColumnOp(
             "sometable", "somecolumn", modify_comment="This is a comment"