]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
ExecuteSQLOp rendering respects alembic_module_prefix
authorAvery Fischer <avery@averyjfischer.com>
Tue, 13 May 2025 16:44:27 +0000 (12:44 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 13 May 2025 20:29:12 +0000 (22:29 +0200)
<!-- Provide a general summary of your proposed changes in the Title field above -->

### Description

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.

### Checklist

This pull request is:

- [ ] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [x] A short code fix
- please include the issue number, and create an issue if none exists, which
  must include a complete example of the issue.  one line code fixes without an
  issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.   one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
  include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.

Fixes #1656

Closes: #1657
Pull-request: https://github.com/sqlalchemy/alembic/pull/1657
Pull-request-sha: c67f4b7ce25c363d525cd2483370b0be024160d9

Change-Id: I6ca36642ed3755a2d96824498502ada6ae16f714

alembic/autogenerate/render.py
docs/build/unreleased/1656.rst [new file with mode: 0644]
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()
diff --git a/docs/build/unreleased/1656.rst b/docs/build/unreleased/1656.rst
new file mode 100644 (file)
index 0000000..3680aa7
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, operations
+    :tickets: 1656
+
+    The ExecuteSQLOp now takes into account the value configured
+    in :paramref:`configure.alembic_module_prefix` instead of always
+    defaulting to ``op.``.
+    Pull request curtesy of Avery Fischer.
index ed31183e8c0a2242c0dec692f751ccb3e72e3623..913e1017508486439ff42556820a218b642aa415 100644 (file)
@@ -2035,6 +2035,24 @@ 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"