]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Use repr for table comment in create_table_comment / drop_table_comment
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Sep 2019 21:43:47 +0000 (17:43 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Sep 2019 21:45:04 +0000 (17:45 -0400)
Fixed bug where rendering of comment text for table-level comments  within
:meth:`.Operations.create_table_comment` and
:meth:`.Operations.drop_table_comment` was not properly quote-escaped
within rendered Python code for autogenerate.

Fixes: #549
Change-Id: I0356cf0eb7c6c6dd3f765bd3ed6e81bccf62468c

alembic/autogenerate/render.py
docs/build/unreleased/549.rst [new file with mode: 0644]
tests/test_autogen_render.py

index 24eeb4e7ac431040d58d4cf4d1bf7340bdb476b6..0c52211767acd1a5ffa8708ddcab12ab71877c35 100644 (file)
@@ -131,8 +131,8 @@ def _render_create_table_comment(autogen_context, op):
     return templ.format(
         prefix=_alembic_autogenerate_prefix(autogen_context),
         tname=op.table_name,
-        comment="'%s'" % op.comment if op.comment is not None else None,
-        existing="'%s'" % op.existing_comment
+        comment="%r" % op.comment if op.comment is not None else None,
+        existing="%r" % op.existing_comment
         if op.existing_comment is not None
         else None,
         schema="'%s'" % op.schema if op.schema is not None else None,
@@ -153,7 +153,7 @@ def _render_drop_table_comment(autogen_context, op):
     return templ.format(
         prefix=_alembic_autogenerate_prefix(autogen_context),
         tname=op.table_name,
-        existing="'%s'" % op.existing_comment
+        existing="%r" % op.existing_comment
         if op.existing_comment is not None
         else None,
         schema="'%s'" % op.schema if op.schema is not None else None,
diff --git a/docs/build/unreleased/549.rst b/docs/build/unreleased/549.rst
new file mode 100644 (file)
index 0000000..e9eacd5
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, autogenerate
+    :tickets: 549
+
+    Fixed bug where rendering of comment text for table-level comments  within
+    :meth:`.Operations.create_table_comment` and
+    :meth:`.Operations.drop_table_comment` was not properly quote-escaped
+    within rendered Python code for autogenerate.
index ef2130a3651281a9011b30497f9187d72f6ea764..3c28a03d9a5a43d3a4eff2a145b6451ddef7a44a 100644 (file)
@@ -1856,6 +1856,23 @@ class AutogenRenderTest(TestBase):
             ")",
         )
 
+    @config.requirements.comments_api
+    def test_render_create_table_comment_with_quote_op(self):
+        op_obj = ops.CreateTableCommentOp(
+            "table_name",
+            "This is john's comment",
+            existing_comment='This was john\'s "comment"',
+        )
+        eq_ignore_whitespace(
+            autogenerate.render_op_text(self.autogen_context, op_obj),
+            "op.create_table_comment("
+            "   'table_name',"
+            '   "This is john\'s comment",'
+            "   existing_comment='This was john\\'s \"comment\"',"
+            "   schema=None"
+            ")",
+        )
+
     def test_render_create_table_comment_op_with_existing_comment(self):
         op_obj = ops.CreateTableCommentOp(
             "table_name", "comment", existing_comment="old comment"
@@ -1895,6 +1912,19 @@ class AutogenRenderTest(TestBase):
             ")",
         )
 
+    def test_render_drop_table_comment_op_existing_with_quote(self):
+        op_obj = ops.DropTableCommentOp(
+            "table_name", existing_comment="This was john's comment"
+        )
+        eq_ignore_whitespace(
+            autogenerate.render_op_text(self.autogen_context, op_obj),
+            "op.drop_table_comment("
+            "   'table_name',"
+            '   existing_comment="This was john\'s comment",'
+            "   schema=None"
+            ")",
+        )
+
 
 class RenderNamingConventionTest(TestBase):
     def setUp(self):