]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
PGDialect.get_check_constraints: Handle "NOT VALID" 4825/head
authorBill Finn <bill@angaza.com>
Mon, 26 Aug 2019 21:22:44 +0000 (21:22 +0000)
committerBill Finn <bill@angaza.com>
Mon, 26 Aug 2019 21:25:14 +0000 (21:25 +0000)
PostgreSQL supports creating "not valid" check constraints that contain
the string " NOT VALID" in their declaration. Here, we update our
regular expression to properly parse check constraints that have yet to
be validated.

Fixes #4824.

lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_reflection.py

index 5aea02cfcaa3adb26a4931d909b054669ddc4386..022d9cd809479bdbb2d4ee9cdebeee50a32ec97f 100644 (file)
@@ -3451,7 +3451,7 @@ class PGDialect(default.DefaultDialect):
         # "CHECK (((a > 1) AND (a < 5)))"
         # "CHECK (((a = 1) OR ((a > 2) AND (a < 5))))"
         def match_cons(src):
-            m = re.match(r"^CHECK *\(\((.+)\)\)$", src)
+            m = re.match(r"^CHECK *\(\((.+)\)\)( NOT VALID)?$", src)
             if not m:
                 util.warn("Could not parse CHECK constraint text: %r" % src)
                 return ""
index ea72d571056f29101015a3cec9036014515cbb03..2149decd62fcd3a13c65bd7cbf2613635dff4fa5 100644 (file)
@@ -1535,6 +1535,25 @@ class ReflectionTest(fixtures.TestBase):
             ):
                 testing.db.dialect.get_check_constraints(conn, "foo")
 
+    def test_reflect_with_not_valid_check_constraint(self):
+        conn = mock.Mock(
+            execute=lambda *arg, **kw: mock.Mock(
+                fetchall=lambda: [
+                    ("some name", "CHECK ((a IS NOT NULL)) NOT VALID")
+                ]
+            )
+        )
+        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,
+                [{u"name": u"some name", u"sqltext": u"a IS NOT NULL"}],
+            )
+
 
 class CustomTypeReflectionTest(fixtures.TestBase):
     class CustomType(object):