]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixes: #5170
authorEric Borczuk <eric@trialspark.com>
Thu, 27 Feb 2020 04:26:56 +0000 (23:26 -0500)
committerEric Borczuk <eric@trialspark.com>
Thu, 27 Feb 2020 05:05:18 +0000 (00:05 -0500)
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_reflection.py

index b30e7770498bf39e63f6a5afeedd478bd7f4cae6..0b8d6f197b11655e42d6440700749837205ef2cd 100644 (file)
@@ -3488,12 +3488,15 @@ class PGDialect(default.DefaultDialect):
             # "CHECK (((a = 1) OR ((a > 2) AND (a < 5))))"
             # "CHECK (((a > 1) AND (a < 5))) NOT VALID"
             # "CHECK (some_boolean_function(a))"
-            m = re.match(r"^CHECK *\((.+)\)( NOT VALID)?$", src)
+            # "CHECK (((a\n < 1)\n OR\n (a\n >= 5))\n)"
+
+            m = re.match(r"^CHECK *\((.+)\)( NOT VALID)?$", src, flags=re.DOTALL)
             if not m:
                 util.warn("Could not parse CHECK constraint text: %r" % src)
                 sqltext = ""
             else:
-                sqltext = re.sub(r"^\((.+)\)$", r"\1", m.group(1))
+                match_without_newlines = ''.join(m.group(1).splitlines())
+                sqltext = re.sub(r"^\((.+)\)$", r"\1", match_without_newlines)
             entry = {"name": name, "sqltext": sqltext}
             if m and m.group(2):
                 entry["dialect_options"] = {"not_valid": True}
index 830a54eef0dad1759a972b3e9fd6b553b07f3a44..092363e03868a25e24b2646707a6108784c62113 100644 (file)
@@ -1582,6 +1582,29 @@ class ReflectionTest(fixtures.TestBase):
             ):
                 testing.db.dialect.get_check_constraints(conn, "foo")
 
+    def test_reflect_extra_newlines(self):
+        rows = [("some name", "CHECK (\n(a \nIS\n NOT\n\n NULL\n)\n)")]
+        conn = mock.Mock(
+            execute=lambda *arg, **kw: mock.MagicMock(
+                fetchall=lambda: rows, __iter__=lambda self: iter(rows)
+            )
+        )
+        with mock.patch.object(
+            testing.db.dialect, "get_table_oid", lambda *arg, **kw: 1
+        ):
+            check_constraints = testing.db.dialect.get_check_constraints(
+                conn, "foo"
+            )
+            eq_(
+                check_constraints,
+                [
+                    {
+                        "name": "some name",
+                        "sqltext": "a IS NOT NULL",
+                    }
+                ],
+            )
+
     def test_reflect_with_not_valid_check_constraint(self):
         rows = [("some name", "CHECK ((a IS NOT NULL)) NOT VALID")]
         conn = mock.Mock(