mirror of
https://github.com/pretix/pretix.git
synced 2026-05-06 15:24:02 +00:00
Add a custom context manager for rollbacks (#282)
This commit is contained in:
28
src/pretix/helpers/database.py
Normal file
28
src/pretix/helpers/database.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import contextlib
|
||||
|
||||
from django.db import transaction
|
||||
|
||||
|
||||
class DummyRollbackException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def rolledback_transaction():
|
||||
"""
|
||||
This context manager runs your code in a database transaction that will be rolled back in the end.
|
||||
This can come in handy to simulate the effects of a database operation that you do not actually
|
||||
want to perform.
|
||||
|
||||
Note that rollbacks are a very slow operation on most database backends. Also, long-running
|
||||
transactions can slow down other operations currently running and you should not use this
|
||||
in a place that is called frequently.
|
||||
"""
|
||||
try:
|
||||
with transaction.atomic():
|
||||
yield
|
||||
raise DummyRollbackException()
|
||||
except DummyRollbackException:
|
||||
pass
|
||||
else:
|
||||
raise Exception('Invalid state, should have rolled back.')
|
||||
Reference in New Issue
Block a user