From c08ff6220a3112f61de260e0a72814fedadd3ea5 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 17 Sep 2019 17:43:47 -0400 Subject: [PATCH] Use repr for table comment in create_table_comment / drop_table_comment 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 | 6 +++--- docs/build/unreleased/549.rst | 8 ++++++++ tests/test_autogen_render.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 docs/build/unreleased/549.rst diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index 24eeb4e7..0c522117 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -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 index 00000000..e9eacd58 --- /dev/null +++ b/docs/build/unreleased/549.rst @@ -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. diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index ef2130a3..3c28a03d 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -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): -- 2.47.2