From: Mike Bayer Date: Fri, 13 Dec 2019 15:05:55 +0000 (-0500) Subject: Provide a context for render_python_code X-Git-Tag: rel_1_3_2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f35e2fd109d2bff913032782d35ad6b397452a70;p=thirdparty%2Fsqlalchemy%2Falembic.git Provide a context for render_python_code Fixed regression introduced by :ticket:`579` where server default rendering functions began to require a dialect implementation, however the :func:`.render_python_code` convenience function did not include one, thus causing the function to fail when used in a server default context. The function now accepts a migration context argument and also creates one against the default dialect if one is not provided. Change-Id: I190ddb8fb5bc54c6d850694e0015533c4997b71c Fixes: #635 --- diff --git a/alembic/autogenerate/api.py b/alembic/autogenerate/api.py index 709e97d0..db929d07 100644 --- a/alembic/autogenerate/api.py +++ b/alembic/autogenerate/api.py @@ -143,6 +143,7 @@ def render_python_code( render_as_batch=False, imports=(), render_item=None, + migration_context=None, ): """Render Python code given an :class:`.UpgradeOps` or :class:`.DowngradeOps` object. @@ -158,7 +159,15 @@ def render_python_code( "render_as_batch": render_as_batch, } - autogen_context = AutogenContext(None, opts=opts) + if migration_context is None: + from ..runtime.migration import MigrationContext + from sqlalchemy.engine.default import DefaultDialect + + migration_context = MigrationContext.configure( + dialect=DefaultDialect() + ) + + autogen_context = AutogenContext(migration_context, opts=opts) autogen_context.imports = set(imports) return render._indent( render._render_cmd_body(up_or_down_op, autogen_context) diff --git a/docs/build/unreleased/635.rst b/docs/build/unreleased/635.rst new file mode 100644 index 00000000..ba7bc6e5 --- /dev/null +++ b/docs/build/unreleased/635.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: bug, api, autogenerate + :tickets: 635 + + Fixed regression introduced by :ticket:`579` where server default rendering + functions began to require a dialect implementation, however the + :func:`.render_python_code` convenience function did not include one, thus + causing the function to fail when used in a server default context. The + function now accepts a migration context argument and also creates one + against the default dialect if one is not provided. + diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index f6229c96..e191b9f5 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -1662,6 +1662,46 @@ class AutogenRenderTest(TestBase): " # ### end Alembic commands ###", ) + def test_render_server_default_no_context(self): + uo = ops.UpgradeOps( + ops=[ + ops.CreateTableOp( + "sometable", + [Column("x", types.DateTime(), server_default=func.now())], + ) + ] + ) + + eq_ignore_whitespace( + autogenerate.render_python_code(uo), + "# ### commands auto generated by Alembic - please adjust! ###\n" + " op.create_table('sometable',\n" + " sa.Column('x', sa.DateTime(), " + "server_default=sa.text(!U'now()'), nullable=True)\n" + " )\n" + " # ### end Alembic commands ###", + ) + + def test_render_server_default_context_passed(self): + uo = ops.UpgradeOps( + ops=[ + ops.CreateTableOp( + "sometable", + [Column("x", types.DateTime(), server_default=func.now())], + ) + ] + ) + context = MigrationContext.configure(dialect_name="sqlite") + eq_ignore_whitespace( + autogenerate.render_python_code(uo, migration_context=context), + "# ### commands auto generated by Alembic - please adjust! ###\n" + " op.create_table('sometable',\n" + " sa.Column('x', sa.DateTime(), " + "server_default=sa.text(!U'(CURRENT_TIMESTAMP)'), nullable=True)\n" + " )\n" + " # ### end Alembic commands ###", + ) + def test_repr_custom_type_w_sqla_prefix(self): self.autogen_context.opts["user_module_prefix"] = None