From 8a6739f6d1b755b1e5730f684dc8402237bd24c5 Mon Sep 17 00:00:00 2001 From: Benoit Clennett-Sirois Date: Tue, 30 May 2023 14:13:51 -0400 Subject: [PATCH] Add support for READ UNCOMMITTED --- asyncpg/connection.py | 6 +++--- asyncpg/transaction.py | 10 +++++++++- tests/test_transaction.py | 2 ++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/asyncpg/connection.py b/asyncpg/connection.py index 432fcef6..0b13d356 100644 --- a/asyncpg/connection.py +++ b/asyncpg/connection.py @@ -257,9 +257,9 @@ def transaction(self, *, isolation=None, readonly=False, :param isolation: Transaction isolation mode, can be one of: `'serializable'`, `'repeatable_read'`, - `'read_committed'`. If not specified, the behavior - is up to the server and session, which is usually - ``read_committed``. + `'read_uncommitted'`, `'read_committed'`. If not + specified, the behavior is up to the server and + session, which is usually ``read_committed``. :param readonly: Specifies whether or not this transaction is read-only. diff --git a/asyncpg/transaction.py b/asyncpg/transaction.py index 2d7ba49f..562811e6 100644 --- a/asyncpg/transaction.py +++ b/asyncpg/transaction.py @@ -19,9 +19,15 @@ class TransactionState(enum.Enum): FAILED = 4 -ISOLATION_LEVELS = {'read_committed', 'serializable', 'repeatable_read'} +ISOLATION_LEVELS = { + 'read_committed', + 'read_uncommitted', + 'serializable', + 'repeatable_read', +} ISOLATION_LEVELS_BY_VALUE = { 'read committed': 'read_committed', + 'read uncommitted': 'read_uncommitted', 'serializable': 'serializable', 'repeatable read': 'repeatable_read', } @@ -124,6 +130,8 @@ async def start(self): query = 'BEGIN' if self._isolation == 'read_committed': query += ' ISOLATION LEVEL READ COMMITTED' + elif self._isolation == 'read_uncommitted': + query += ' ISOLATION LEVEL READ UNCOMMITTED' elif self._isolation == 'repeatable_read': query += ' ISOLATION LEVEL REPEATABLE READ' elif self._isolation == 'serializable': diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 8b7ffd95..f84cf7c0 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -188,6 +188,7 @@ async def test_isolation_level(self): isolation_levels = { None: default_isolation, 'read_committed': 'read committed', + 'read_uncommitted': 'read uncommitted', 'repeatable_read': 'repeatable read', 'serializable': 'serializable', } @@ -214,6 +215,7 @@ async def test_nested_isolation_level(self): set_sql = 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ' isolation_levels = { 'read_committed': 'read committed', + 'read_uncommitted': 'read uncommitted', 'repeatable_read': 'repeatable read', 'serializable': 'serializable', }