From: Avery Fischer Date: Tue, 13 May 2025 16:44:27 +0000 (-0400) Subject: ExecuteSQLOp rendering respects alembic_module_prefix X-Git-Tag: rel_1_16_0~19 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=76791bbb42b4b2829dc79df5ea5234ac7dc226b0;p=thirdparty%2Fsqlalchemy%2Falembic.git ExecuteSQLOp rendering respects alembic_module_prefix ### 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: #` 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: #` 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 --- diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index ffc277db..66c67673 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -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 index 00000000..3680aa72 --- /dev/null +++ b/docs/build/unreleased/1656.rst @@ -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. diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index ed31183e..913e1017 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -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"