Skip to content

Commit

Permalink
Support readonly and deferrable for non-serializable transactions (#747)
Browse files Browse the repository at this point in the history
Resolves #743
  • Loading branch information
pauldraper authored Apr 26, 2021
1 parent bc4127f commit 5cf4089
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions asyncpg/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ def __init__(self, connection, isolation, readonly, deferrable):
'isolation is expected to be either of {}, '
'got {!r}'.format(ISOLATION_LEVELS, isolation))

if isolation and isolation != 'serializable':
if readonly:
raise ValueError(
'"readonly" is only supported for '
'serializable transactions')

if deferrable and not readonly:
raise ValueError(
'"deferrable" is only supported for '
'serializable readonly transactions')

self._isolation = isolation
self._readonly = readonly
self._deferrable = deferrable
Expand Down Expand Up @@ -132,19 +121,18 @@ async def start(self):
self._id = con._get_unique_id('savepoint')
query = 'SAVEPOINT {};'.format(self._id)
else:
if self._isolation is None:
query = 'BEGIN;'
elif self._isolation == 'read_committed':
query = 'BEGIN ISOLATION LEVEL READ COMMITTED;'
query = 'BEGIN'
if self._isolation == 'read_committed':
query += ' ISOLATION LEVEL READ COMMITTED'
elif self._isolation == 'repeatable_read':
query = 'BEGIN ISOLATION LEVEL REPEATABLE READ;'
else:
query = 'BEGIN ISOLATION LEVEL SERIALIZABLE'
if self._readonly:
query += ' READ ONLY'
if self._deferrable:
query += ' DEFERRABLE'
query += ';'
query += ' ISOLATION LEVEL REPEATABLE READ'
elif self._isolation == 'serializable':
query += ' ISOLATION LEVEL SERIALIZABLE'
if self._readonly:
query += ' READ ONLY'
if self._deferrable:
query += ' DEFERRABLE'
query += ';'

try:
await self._connection.execute(query)
Expand Down

0 comments on commit 5cf4089

Please sign in to comment.