]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Provide a context for render_python_code
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Dec 2019 15:05:55 +0000 (10:05 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Dec 2019 18:37:44 +0000 (13:37 -0500)
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
alembic/autogenerate/api.py
docs/build/unreleased/635.rst [new file with mode: 0644]
tests/test_autogen_render.py

index 709e97d0f463dd2b1416820eabccdac7b5b058d2..db929d07b6efa6e1097643e41dd37781cbf4a636 100644 (file)
@@ -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 (file)
index 0000000..ba7bc6e
--- /dev/null
@@ -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.
+
index f6229c9623d0a179432270286b2a853e21034a2d..e191b9f52e57e0b4bac79da41f2f43fb2f79f564 100644 (file)
@@ -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