]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add AUTOCOMMIT isolation level support for psycopg2
authorRoman Podolyaka <roman.podolyaka@gmail.com>
Sat, 15 Jun 2013 12:38:02 +0000 (15:38 +0300)
committerRoman Podolyaka <roman.podolyaka@gmail.com>
Sat, 15 Jun 2013 13:54:50 +0000 (16:54 +0300)
One can use this to emit statements, which can not be
executed within a transaction (e. g. CREATE DATABASE):

    from sqlalchemy import create_engine

    eng = create_engine('postgresql://test:test@localhost/test')

    conn = eng.connect().execution_options(isolation_level='AUTOCOMMIT')
    conn.execute('CREATE DATABASE test2;')

Fixes issue #2072.

lib/sqlalchemy/dialects/postgresql/psycopg2.py
test/dialect/test_postgresql.py

index fcc1946ff4f6ac9408483fd44e6b5716da4c505c..c7f9f0187f602289bfe7ce9c013a09ebd63cf3ec 100644 (file)
@@ -350,6 +350,7 @@ class PGDialect_psycopg2(PGDialect):
     def _isolation_lookup(self):
         extensions = __import__('psycopg2.extensions').extensions
         return {
+            'AUTOCOMMIT': extensions.ISOLATION_LEVEL_AUTOCOMMIT,
             'READ COMMITTED': extensions.ISOLATION_LEVEL_READ_COMMITTED,
             'READ UNCOMMITTED': extensions.ISOLATION_LEVEL_READ_UNCOMMITTED,
             'REPEATABLE READ': extensions.ISOLATION_LEVEL_REPEATABLE_READ,
index 00e5c07ab11c8c5976dbc40700ea7a28d76336e7..d1ba960c4588c94c3863751b6cdf7345c081696d 100644 (file)
@@ -1890,6 +1890,16 @@ class MiscTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
         c = e.connect()
         eq_(c.connection.connection.encoding, test_encoding)
 
+    @testing.only_on('postgresql+psycopg2', 'psycopg2-specific feature')
+    @engines.close_open_connections
+    def test_autocommit_isolation_level(self):
+        extensions = __import__('psycopg2.extensions').extensions
+
+        c = testing.db.connect()
+        c = c.execution_options(isolation_level='AUTOCOMMIT')
+        eq_(c.connection.connection.isolation_level,
+            extensions.ISOLATION_LEVEL_AUTOCOMMIT)
+
     @testing.fails_on('+zxjdbc',
                       "Can't infer the SQL type to use for an instance "
                       "of org.python.core.PyObjectDerived.")