render_as_batch=False,
imports=(),
render_item=None,
+ migration_context=None,
):
"""Render Python code given an :class:`.UpgradeOps` or
:class:`.DowngradeOps` object.
"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)
--- /dev/null
+.. 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.
+
" # ### 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