Skip to content

Comments with quotes and backslashes are stored as declared on PostgreSQL and MySQL - #1754

Open
dimitri-yatsenko wants to merge 6 commits into
masterfrom
fix/pg-comment-quotes
Open

dimitri-yatsenko wants to merge 6 commits into
masterfrom
fix/pg-comment-quotes

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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_ESCAPES set, each backslash in a comment is stored doubled (every other character is stored as declared).

Before this change, compile_attribute wrote 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 (\n became a line break).

Changes

  • compile_attribute no longer escapes the comment. The comment it returns is the declared text.
  • New adapter method quote_comment(). Each adapter now quotes comments itself:
    • MySQL keeps the double-quoted 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 under NO_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.
    • PostgreSQL writes a comment with no backslash as before. A comment with a backslash is written as an E'...' literal with each backslash doubled. That form reads the same whether standard_conforming_strings is on or off.
  • format_column_definition and table_options_clause (MySQL), column_comment_ddl, table_comment_ddl and add_comment_sql (PostgreSQL), and the table comment in alter() use quote_comment().
  • Attribute.sql in heading.py now quotes its comment with MySQLAdapter.quote_comment() instead of writing COMMENT "{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 as say \"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:

  • Correct the comment in the class definition and run alter(). On PostgreSQL this needs alter() changes and renames attributes on PostgreSQL #1639, which lets alter() change comments there.
  • Set the comment directly with COMMENT ON COLUMN or COMMENT 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 run alter(), 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 with standard_conforming_strings on and off. MySQL is checked with and without NO_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 that compile_attribute returns the declared comment, and that Attribute.sql quotes 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:
    • heading comments and the table comment equal the declared text;
    • a table declared from describe() output has the same comments and the same describe();
    • alter() with an unchanged definition finds nothing to alter;
    • a foreign key copies the parent's comment exactly;
    • on MySQL, alter() writes the new comments as declared (skipped on PostgreSQL until alter() changes and renames attributes on PostgreSQL #1639 lets alter() change comments there);
    • with NO_BACKSLASH_ESCAPES (MySQL) the table declares and each comment is stored as declared except that each backslash is doubled, and with standard_conforming_strings off (PostgreSQL) the comments are still stored as declared.
      All of these except the unchanged-definition alter() test on PostgreSQL fail on master.
  • Full test suite passes locally on both backends.

Merge order and conflicts

Merge after #1639.

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.
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL stores a backslash before each double quote in an attribute comment

1 participant