]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Optimized SQLite's check constraint reflection pattern.
authorJeff Horemans <jeff.horemans@vortex-financials.be>
Wed, 7 Aug 2024 15:10:49 +0000 (17:10 +0200)
committerJeff Horemans <jeff.horemans@vortex-financials.be>
Wed, 7 Aug 2024 15:10:49 +0000 (17:10 +0200)
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

index 16b8b8cc909fc4c446e3c0b05f0c440d9406a934..a093065aee26ae5f81a1b36a0223fab8a94f9900 100644 (file)
@@ -2624,7 +2624,14 @@ class SQLiteDialect(default.DefaultDialect):
             connection, table_name, schema=schema, **kw
         )
 
-        CHECK_PATTERN = r"(?:CONSTRAINT ([^\n^\t]+) +)?CHECK *\( *(.+?) *\)(?:, ?\n|\n) *"
+        # Notes:
+        # * The pattern currently matches any character for the name of the constraint
+        #   except tab and newline characters.
+        # * Because check constraints in the table data can contain space, newline and tab characters,
+        #   the pattern matches any character untill either the beginning of the next CONSTRAINT
+        #   statement using a non-capturing non-consuming group (allowing the next one to match),
+        #   or the end of the table definition e.g. newline and closing ')'.
+        CHECK_PATTERN = r"(?:CONSTRAINT ([^\t\n]+) )?CHECK \((.+?)\)(?:, *\n\t?(?=CONSTRAINT)|\n\))"
         cks = []
 
         for match in re.finditer(CHECK_PATTERN, table_data or "", re.I|re.S):
index 3ccca50b664a8a1eab3211de3d9f64b4beb2a22a..c7e20d4c1aeeaa758c441f1aeb05cf9a50e08bc7 100644 (file)
@@ -2464,7 +2464,6 @@ class ConstraintReflectionTest(fixtures.TestBase):
                 {"sqltext": "q > 1 AND q < 6", "name": None},
             ],
         )
-        print(inspector.get_check_constraints("r"))
         eq_(
             inspector.get_check_constraints("r"),
             [