]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fix quotes regexp for SQLite CHECK constraints
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 28 Mar 2022 17:46:24 +0000 (13:46 -0400)
committermike bayer <mike_mp@zzzcomputing.com>
Mon, 28 Mar 2022 20:50:20 +0000 (20:50 +0000)
Fixed bug where the name of CHECK constraints under SQLite would not be
reflected if the name were created using quotes, as is the case when the
name uses mixed case or special characters.

Fixes: #5463
Change-Id: Ic3b1e0a0385fb9e727b0880e90815ea2814df313
(cherry picked from commit cb52b934000047278dbb63d0cfffdb4eae1f669c)

doc/build/changelog/unreleased_14/5463.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/sqlite/base.py
lib/sqlalchemy/testing/suite/test_reflection.py

diff --git a/doc/build/changelog/unreleased_14/5463.rst b/doc/build/changelog/unreleased_14/5463.rst
new file mode 100644 (file)
index 0000000..5de6182
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, sqlite, reflection
+    :tickets: 5463
+
+    Fixed bug where the name of CHECK constraints under SQLite would not be
+    reflected if the name were created using quotes, as is the case when the
+    name uses mixed case or special characters.
+
index 7ba9700d709d85d9fbf76932d9a305e0e0f30a58..49e4b5c19552b06a45583d3d7eec9788f6e165ec 100644 (file)
@@ -2449,17 +2449,21 @@ class SQLiteDialect(default.DefaultDialect):
         if not table_data:
             return []
 
-        CHECK_PATTERN = r"(?:CONSTRAINT (\w+) +)?" r"CHECK *\( *(.+) *\),? *"
+        CHECK_PATTERN = r"(?:CONSTRAINT (.+) +)?" r"CHECK *\( *(.+) *\),? *"
         check_constraints = []
         # NOTE: we aren't using re.S here because we actually are
         # taking advantage of each CHECK constraint being all on one
         # line in the table definition in order to delineate.  This
         # necessarily makes assumptions as to how the CREATE TABLE
         # was emitted.
+
         for match in re.finditer(CHECK_PATTERN, table_data, re.I):
-            check_constraints.append(
-                {"sqltext": match.group(2), "name": match.group(1)}
-            )
+            name = match.group(1)
+
+            if name:
+                name = re.sub(r'^"|"$', "", name)
+
+            check_constraints.append({"sqltext": match.group(2), "name": name})
 
         return check_constraints
 
index a1f2e3bc94b05a7748d9a26b8ed13cd074064ee6..459a4d8211c58d7d64153dfcc905270381d803f1 100644 (file)
@@ -1152,7 +1152,9 @@ class ComponentReflectionTestExtra(fixtures.TestBase):
             metadata,
             Column("a", Integer()),
             sa.CheckConstraint("a > 1 AND a < 5", name="cc1"),
-            sa.CheckConstraint("a = 1 OR (a > 2 AND a < 5)", name="cc2"),
+            sa.CheckConstraint(
+                "a = 1 OR (a > 2 AND a < 5)", name="UsesCasing"
+            ),
             schema=schema,
         )
 
@@ -1179,8 +1181,8 @@ class ComponentReflectionTestExtra(fixtures.TestBase):
         eq_(
             reflected,
             [
+                {"name": "UsesCasing", "sqltext": "a = 1 or a > 2 and a < 5"},
                 {"name": "cc1", "sqltext": "a > 1 and a < 5"},
-                {"name": "cc2", "sqltext": "a = 1 or a > 2 and a < 5"},
             ],
         )