From: Mike Bayer Date: Mon, 13 Feb 2017 19:20:38 +0000 (-0500) Subject: Add new DDL autocommit expressions for Postgresql X-Git-Tag: rel_1_1_6~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b6004e6ab3fef8e37fb42981c02f4dfa34fe3b7;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add new DDL autocommit expressions for Postgresql 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 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 7a462eb086..681672b500 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,16 @@ .. 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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index fd25058a25..aaaa64cf16 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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' diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index ba68faf16e..a9ef09dbd2 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -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")