From: Jeff Horemans Date: Wed, 7 Aug 2024 11:51:26 +0000 (+0200) Subject: Fixed SQLite check constraint reflection regex to allow space in constraint name. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4a603279137d76016436bedcd7999b7756c4d6d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fixed SQLite check constraint reflection regex to allow space in constraint name. --- diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 545f320f61..b86ee24fb1 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -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 diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 4014eeff5d..3ccca50b66 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -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"""