]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix single quote escaping for the SQL comments
authorDamien Garaud <damien.garaud@oslandia.com>
Wed, 23 Jan 2019 16:13:37 +0000 (11:13 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 23 Jan 2019 16:49:58 +0000 (11:49 -0500)
Fixed issue in new comment support where autogenerated Python code
for comments wasn't using ``repr()`` thus causing issues with
quoting.  Pull request courtesy Damien Garaud.

Fixes: #529
Closes: #530
Pull-request: https://github.com/sqlalchemy/alembic/pull/530
Pull-request-sha: c85f2de986da6c1231a18732b097ff890e67977e

Change-Id: I7e55cc1968ba83765386c66bd1f1e69a593f133a

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

index 64dbaebf00a579fc4bdf8c5a008d5505bcaec4ee..3fb0a422daa67860b47a4a3a129b923d2240a715 100644 (file)
@@ -614,7 +614,7 @@ def _render_column(column, autogen_context):
 
     comment = sqla_compat._comment_attribute(column)
     if comment:
-        opts.append(("comment", "'%s'" % comment))
+        opts.append(("comment", '%r' % comment))
 
     # TODO: for non-ascii colname, assign a "key"
     return "%(prefix)sColumn(%(name)r, %(type)s, %(kw)s)" % {
diff --git a/docs/build/unreleased/529.rst b/docs/build/unreleased/529.rst
new file mode 100644 (file)
index 0000000..764e42d
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+   :tags: bug, autogenerate
+   :tickets: 529
+
+   Fixed issue in new comment support where autogenerated Python code
+   for comments wasn't using ``repr()`` thus causing issues with
+   quoting.  Pull request courtesy Damien Garaud.
index 7673a493088c70e120d771375d14d58c7a391102..4d010a419f45059f5f7ad1723f8e9e8e01c00880 100644 (file)
@@ -1155,6 +1155,19 @@ class AutogenRenderTest(TestBase):
             "comment='This is a comment')",
         )
 
+    @config.requirements.comments_api
+    def test_render_col_comment_with_quote(self):
+        c = Column("some_key", Integer, comment="This is a john's comment")
+        Table("some_table", MetaData(), c)
+        result = autogenerate.render._render_column(c, self.autogen_context)
+        print(result)
+        eq_ignore_whitespace(
+            result,
+            "sa.Column('some_key', sa.Integer(), "
+            "nullable=True, "
+            "comment=\"This is a john's comment\")",
+        )
+
     def test_render_col_autoinc_false_mysql(self):
         c = Column("some_key", Integer, primary_key=True, autoincrement=False)
         Table("some_table", MetaData(), c)