Repository navigation
Comments with quotes and backslashes are stored as declared on PostgreSQL and MySQL - #1754
Open
dimitri-yatsenko wants to merge 6 commits into
Open
dimitri-yatsenko wants to merge 6 commits into
dimitri-yatsenko wants to merge 6 commits into
Conversation
compile_attribute wrote each double quote in an attribute comment as \", which MySQL reads as a quote but PostgreSQL stores with the backslash. The escaping moves into the adapters through a new quote_comment(): - MySQL keeps the double-quoted COMMENT "..." form and escapes the comment like quote_string, doubling a double quote so the literal stays closed under NO_BACKSLASH_ESCAPES. Table comments, which were written unescaped, use it too, including in alter(). - PostgreSQL writes a comment containing a backslash as an E'...' literal, which reads the same under either standard_conforming_strings setting.
Single quotes need no escape inside a double-quoted literal, and the type marker of an enum attribute contains them. Leaving them as they are keeps the SQL of those columns unchanged and stores them exactly under NO_BACKSLASH_ESCAPES.
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
November 23, 2026 23:12
Attribute.sql wrote COMMENT "{comment}" without escaping. It now uses
MySQLAdapter.quote_identifier and quote_comment, so a comment with a double
quote or a backslash reads back as declared. Comments without either produce
the same SQL as before.
The test comments now read as ordinary notes with double quotes, single quotes, commas, semicolons, dashes and backslashes. They still cover each case: a quote of either kind after a backslash followed by more text, a trailing backslash, backslash sequences, doubled quotes, and both quoting settings. The NO_BACKSLASH_ESCAPES test now checks every stored comment, including that each backslash is stored doubled.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1729
Summary
A comment with a double quote, a single quote or a backslash is now stored exactly as declared, on MySQL and on PostgreSQL. This applies to attribute comments and table comments. One exception: on MySQL with
NO_BACKSLASH_ESCAPESset, each backslash in a comment is stored doubled (every other character is stored as declared).Before this change,
compile_attributewrote each double quote in an attribute comment as\". MySQL read that as", but PostgreSQL kept the backslash. So on PostgreSQL,describe()showed the backslashes, and each table declared from that output added more. On MySQL, a table comment with a double quote failed to declare, and a backslash in a comment was read as an escape (\nbecame a line break).Changes
compile_attributeno longer escapes the comment. The comment it returns is the declared text.quote_comment(). Each adapter now quotes comments itself:COMMENT "..."form. Only two characters are escaped: a backslash is doubled, and a double quote is doubled ("") instead of written as\". Because a double quote is doubled rather than escaped with a backslash, a comment with a double quote also declares underNO_BACKSLASH_ESCAPES, and its double quotes are stored as declared. In that mode MySQL reads a backslash literally, so each backslash in a comment is stored doubled. A comment without a double quote or a backslash produces the same SQL as before, including the:enum(...):marker of an enum attribute.E'...'literal with each backslash doubled. That form reads the same whetherstandard_conforming_stringsis on or off.format_column_definitionandtable_options_clause(MySQL),column_comment_ddl,table_comment_ddlandadd_comment_sql(PostgreSQL), and the table comment inalter()usequote_comment().Attribute.sqlinheading.pynow quotes its comment withMySQLAdapter.quote_comment()instead of writingCOMMENT "{comment}"as is. Nothing in the library calls this property today. Its clause has always used MySQL syntax, and a comment without a double quote or a backslash produces the same SQL as before.Comments already stored
This change does not rewrite comments that are already stored. A PostgreSQL table declared before this change keeps any backslashes that the old escaping added to its comments. For example, a comment declared as
say "hi"is stored assay \"hi\".describe()still shows those backslashes. A table declared from that output now keeps them exactly as written, instead of adding more.To clean such a comment, do either of these:
alter(). On PostgreSQL this needs alter() changes and renames attributes on PostgreSQL #1639, which letsalter()change comments there.COMMENT ON COLUMNorCOMMENT ON TABLE. Keep the:type:marker at the start of a column comment, for example:varchar(16):say "hi".On MySQL, double quotes in existing comments were already stored as declared. A MySQL comment that was declared with a backslash sequence, such as
\n, was stored with that sequence read as an escape (here, a line break). It is not rewritten either. To clean it, correct the comment in the class definition and runalter(), which already changes comments on MySQL.Tests
tests/unit/test_comment_quoting.py: a small reader parses each written literal the way the server does. For ordinary comments with double quotes, single quotes, commas, semicolons, dashes and backslashes (including a backslash before a quote, a backslash sequence such as\n, and a trailing backslash), it checks that the literal ends where the statement ends and reads back as the comment. PostgreSQL is checked withstandard_conforming_stringson and off. MySQL is checked with and withoutNO_BACKSLASH_ESCAPES; with it, the test checks that the literal still ends where the statement ends and that a comment without a backslash reads back as declared. It also checks thatcompile_attributereturns the declared comment, and thatAttribute.sqlquotes its comment the same way and writes plain comments as before. 53 of these 96 tests fail on master.tests/integration/test_comment_quoting.py, on both backends:describe()output has the same comments and the samedescribe();alter()with an unchanged definition finds nothing to alter;alter()writes the new comments as declared (skipped on PostgreSQL until alter() changes and renames attributes on PostgreSQL #1639 letsalter()change comments there);NO_BACKSLASH_ESCAPES(MySQL) the table declares and each comment is stored as declared except that each backslash is doubled, and withstandard_conforming_stringsoff (PostgreSQL) the comments are still stored as declared.All of these except the unchanged-definition
alter()test on PostgreSQL fail on master.Merge order and conflicts
Merge after #1639.
alter()indeclare.py: it moves the table-comment clause intoMySQLAdapter.alter_table_ddl. To rebase, take 1639'salter()and changeclauses.append(f'COMMENT="{table_comment}"')inMySQLAdapter.alter_table_ddltoclauses.append(f"COMMENT={self.quote_comment(table_comment)}"), then remove the PostgreSQL skip intest_alter_writes_comments_as_declared. Its pinned MySQL statements intests/unit/test_alter_sql.pystay valid, because comments without double quotes or backslashes produce the same SQL as before. With 1639 and this change together, the integration tests above pass on both backends, includingalter()on PostgreSQL finding nothing to alter.