]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed SQLite check constraint reflection regex to allow space in constraint name.
authorJeff Horemans <jeff.horemans@vortex-financials.be>
Wed, 7 Aug 2024 11:51:26 +0000 (13:51 +0200)
committerJeff Horemans <jeff.horemans@vortex-financials.be>
Wed, 7 Aug 2024 11:51:26 +0000 (13:51 +0200)
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

index 545f320f6111110ca01e76f5b45b3c1229803364..b86ee24fb1ec17af929b27f6930f44e3959513dc 100644 (file)
@@ -2624,7 +2624,7 @@ class SQLiteDialect(default.DefaultDialect):
             connection, table_name, schema=schema, **kw
         )
 
-        CHECK_PATTERN = r"(?:CONSTRAINT ([^\s]+) +)?CHECK *\( *(.+?) *\)(?:, ?\n|\n) *"
+        CHECK_PATTERN = r"(?:CONSTRAINT ([^\n^\t]+) +)?CHECK *\( *(.+?) *\)(?:, ?\n|\n) *"
         cks = []
         # NOTE: we aren't using re.S here because we actually are
         # taking advantage of each CHECK constraint being all on one
index 4014eeff5debc932eb9fa74240ac40424133409b..3ccca50b664a8a1eab3211de3d9f64b4beb2a22a 100644 (file)
@@ -1819,7 +1819,8 @@ class ConstraintReflectionTest(fixtures.TestBase):
             # intentional new line
             Table("r", meta, Column("id", Integer), Column("value", Integer), PrimaryKeyConstraint("id"),
                   CheckConstraint("id > 0"),
-                  CheckConstraint("((value > 0) AND \n\t(value < 100))", name='ck_r_value'),
+                  CheckConstraint("((value > 0) AND \n\t(value < 100))", name='ck_r_value_multiline'),
+                  CheckConstraint("value IS NOT NULL", name='ck_r_value with spaces'),
             )
 
             meta.create_all(conn)
@@ -2463,6 +2464,15 @@ class ConstraintReflectionTest(fixtures.TestBase):
                 {"sqltext": "q > 1 AND q < 6", "name": None},
             ],
         )
+        print(inspector.get_check_constraints("r"))
+        eq_(
+            inspector.get_check_constraints("r"),
+            [
+                {"sqltext": "value IS NOT NULL", "name": "ck_r_value with spaces"},
+                {"sqltext": "((value > 0) AND \n\t(value < 100))", "name": "ck_r_value_multiline"},
+                {"sqltext": "id > 0", "name": None},
+            ],
+        )
 
     @testing.combinations(
         ("plain_name", "plain_name"),
@@ -2513,14 +2523,6 @@ class ConstraintReflectionTest(fixtures.TestBase):
         else:
             assert False
 
-    def test_multiline_check_constraints(self):
-        inspector = inspect(testing.db)
-        constraints = inspector.get_check_constraints('r')
-        eq_(constraints[1]['name'], None)
-        eq_(constraints[1]['sqltext'], "id > 0")
-        eq_(constraints[0]['name'], "ck_r_value")
-        eq_(constraints[0]['sqltext'], "((value > 0) AND \n\t(value < 100))")
-
 class SavepointTest(fixtures.TablesTest):
     """test that savepoints work when we use the correct event setup"""