From 919763b43ce334be621f29a4b9fd3edd7e6c6927 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Dani=C3=ABl=20van=20Eeden?= Date: Wed, 23 Jun 2021 10:00:43 -0400 Subject: [PATCH] Don't require space after MySQL pk comment MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Made a small adjustment in the table reflection feature of the MySQL dialect to accommodate for alternate MySQL-oriented databases such as TiDB which include their own "comment" directives at the end of a constraint directive within "CREATE TABLE" where the format doesn't have the additional space character after the comment, in this case the TiDB "clustered index" feature. Pull request courtesy Daniël van Eeden. Fixes: #6659 Closes: #6660 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6660 Pull-request-sha: 98791a441b2e3d26aa7cef9d3e2907fac113e30f Change-Id: I20d206e0cdb809e4c6997b87159edf41249f7cd9 --- doc/build/changelog/unreleased_14/6659.rst | 10 ++++++++++ lib/sqlalchemy/dialects/mysql/reflection.py | 2 +- test/dialect/mysql/test_reflection.py | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_14/6659.rst diff --git a/doc/build/changelog/unreleased_14/6659.rst b/doc/build/changelog/unreleased_14/6659.rst new file mode 100644 index 0000000000..15e9c095b3 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6659.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: usecase, mysql + :tickets: 6659 + + Made a small adjustment in the table reflection feature of the MySQL + dialect to accommodate for alternate MySQL-oriented databases such as TiDB + which include their own "comment" directives at the end of a constraint + directive within "CREATE TABLE" where the format doesn't have the + additional space character after the comment, in this case the TiDB + "clustered index" feature. Pull request courtesy Daniël van Eeden. diff --git a/lib/sqlalchemy/dialects/mysql/reflection.py b/lib/sqlalchemy/dialects/mysql/reflection.py index b78034de27..a1ad3adfe0 100644 --- a/lib/sqlalchemy/dialects/mysql/reflection.py +++ b/lib/sqlalchemy/dialects/mysql/reflection.py @@ -416,7 +416,7 @@ class MySQLTableDefinitionParser(object): r"(?: +KEY_BLOCK_SIZE *[ =]? *(?P\S+))?" r"(?: +WITH PARSER +(?P\S+))?" r"(?: +COMMENT +(?P(\x27\x27|\x27([^\x27])*?\x27)+))?" - r"(?: +/\*(?P.+)\*/ +)?" + r"(?: +/\*(?P.+)\*/ *)?" r",?$" % quotes ) diff --git a/test/dialect/mysql/test_reflection.py b/test/dialect/mysql/test_reflection.py index 5b785a7a41..3091a6a7da 100644 --- a/test/dialect/mysql/test_reflection.py +++ b/test/dialect/mysql/test_reflection.py @@ -1148,6 +1148,10 @@ class RawReflectionTest(fixtures.TestBase): assert not regex.match( " PRIMARY KEY (`id`) USING BTREE KEY_BLOCK_SIZE = = 16" ) + # test #6659 (TiDB) + assert regex.match( + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */" + ) assert regex.match(" KEY (`id`) USING BTREE COMMENT 'comment'") # `SHOW CREATE TABLE` returns COMMENT '''comment' # after creating table with COMMENT '\'comment' @@ -1164,6 +1168,10 @@ class RawReflectionTest(fixtures.TestBase): "/*!50100 WITH PARSER `ngram` */ " ) + m = regex.match(" PRIMARY KEY (`id`)") + eq_(m.group("type"), "PRIMARY") + eq_(m.group("columns"), "`id`") + def test_key_reflection_columns(self): regex = self.parser._re_key exprs = self.parser._re_keyexprs -- 2.47.2