Skip to content

Support readonly and deferrable for non-serializable transactions #747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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