]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add new DDL autocommit expressions for Postgresql
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 13 Feb 2017 19:20:38 +0000 (14:20 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 13 Feb 2017 19:45:40 +0000 (14:45 -0500)
Added regular expressions for the "IMPORT FOREIGN SCHEMA",
"REFRESH MATERIALIZED VIEW" Postgresql statements so that they
autocommit when invoked via a connection or engine without
an explicit transaction.  Pull requests courtesy Frazer McLean
and Paweł Stiasny.

Fixes: #3840
Co-authored-by: Frazer McLean
Co-authored-by: Paweł Stiasny
Change-Id: I92b2b61683d29d57fa23a66a3559120cb1241c2f
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/323

doc/build/changelog/changelog_11.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_dialect.py

index 7a462eb086e84b391d6197246e2eeda0014d104b..681672b500607a650d661cc5ac341f7b8991e2e2 100644 (file)
 .. changelog::
     :version: 1.1.6
 
+    .. change:: 3804
+        :tags: bug, postgresql
+        :tickets: 3804
+
+        Added regular expressions for the "IMPORT FOREIGN SCHEMA",
+        "REFRESH MATERIALIZED VIEW" Postgresql statements so that they
+        autocommit when invoked via a connection or engine without
+        an explicit transaction.  Pull requests courtesy Frazer McLean
+        and Paweł Stiasny.
+
     .. change:: 3909
         :tags: bug, orm
         :tickets: 3909
index fd25058a255aa1df532c1aeddf8015d3f1861c23..aaaa64cf16603e1f5dbf1aa15b3907a12459b427 100644 (file)
@@ -878,6 +878,11 @@ from sqlalchemy.types import INTEGER, BIGINT, SMALLINT, VARCHAR, \
     CHAR, TEXT, FLOAT, NUMERIC, \
     DATE, BOOLEAN, REAL
 
+AUTOCOMMIT_REGEXP = re.compile(
+    r'\s*(?:UPDATE|INSERT|CREATE|DELETE|DROP|ALTER|'
+    'IMPORT FOREIGN SCHEMA|REFRESH MATERIALIZED VIEW)',
+    re.I | re.UNICODE)
+
 RESERVED_WORDS = set(
     ["all", "analyse", "analyze", "and", "any", "array", "as", "asc",
      "asymmetric", "both", "case", "cast", "check", "collate", "column",
@@ -1998,6 +2003,9 @@ class PGExecutionContext(default.DefaultExecutionContext):
 
         return super(PGExecutionContext, self).get_insert_default(column)
 
+    def should_autocommit_text(self, statement):
+        return AUTOCOMMIT_REGEXP.match(statement)
+
 
 class PGDialect(default.DefaultDialect):
     name = 'postgresql'
index ba68faf16e7856b429f3050ca9d91997ac77932e..a9ef09dbd2e21ced4ce5c3d168075e875e73a2ad 100644 (file)
@@ -18,6 +18,7 @@ from sqlalchemy.engine import engine_from_config
 from sqlalchemy.engine import url
 from sqlalchemy.testing import is_
 from sqlalchemy.testing import expect_deprecated
+from ...engine import test_execute
 
 
 class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
@@ -295,3 +296,13 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
                 ddl_compiler.get_column_specification(t.c.c),
                 "c %s NOT NULL" % expected
             )
+
+
+class AutocommitTextTest(test_execute.AutocommitTextTest):
+    __only_on__ = 'postgresql'
+
+    def test_import_foreign_schema(self):
+        self._test_keyword("IMPORT FOREIGN SCHEMA foob")
+
+    def test_refresh_view(self):
+        self._test_keyword("REFRESH MATERIALIZED VIEW fooview")