Skip to content

Commit

Permalink
fix transaction management for 1.6 and SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
hxu committed Mar 15, 2014
1 parent 0a4a525 commit 4470401
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
9 changes: 5 additions & 4 deletions johnny/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ def empty_iter():
def is_managed(using=None):
if django.VERSION[:2] < (1, 6):
return transaction.is_managed(using=using)
elif django.VERSION[:2] >= (1, 6):
# See https://code.djangoproject.com/ticket/21004
return not transaction.get_autocommit(using=using)
return False
# Or maybe we should run the following line? I'm not sure…
# return not transaction.get_autocommit(using=using)


def managed(flag=True, using=None):
if django.VERSION[:2] < (1, 6):
transaction.managed(flag=flag, using=using)
# Maybe we should execute the following line otherwise? I'm not sure…
# transaction.set_autocommit(autocommit=not flag, using=using)
elif django.VERSION[:2] >= (1, 6):
transaction.set_autocommit(not flag, using=using)
40 changes: 36 additions & 4 deletions johnny/tests/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from django.conf import settings
from django.core.paginator import Paginator
from django.db import connection, connections, transaction
from django.db import connection, connections, transaction, IntegrityError
from django.db.models import Q, Count, Sum
from johnny import middleware, settings as johnny_settings, cache
from johnny.cache import get_tables_for_query, invalidate
Expand Down Expand Up @@ -630,7 +630,8 @@ def miss(*args, **kwargs):

def setUp(self):
super(TransactionSupportTest, self).setUp()
managed(False)
if is_managed():
managed(False)

def tearDown(self):
if is_managed():
Expand Down Expand Up @@ -739,13 +740,12 @@ def test_transaction_rollback(self):

def test_savepoint_rollback(self):
"""Tests rollbacks of savepoints"""
if not connection.features.uses_savepoints:
if not connection.features.uses_savepoints or connection.vendor == 'sqlite':
return
self.assertFalse(is_managed())
self.assertFalse(transaction.is_dirty())
cache.local.clear()
managed()
transaction.enter_transaction_management()

g = Genre.objects.get(pk=1)
start_title = g.title
Expand All @@ -765,6 +765,38 @@ def test_savepoint_rollback(self):
g = Genre.objects.get(pk=1)
self.assertEqual(g.title, start_title)

def test_savepoint_rollback_sqlite(self):
"""SQLite savepoints don't work correctly with autocommit disabled,
so we have to use transaction.atomic().
See https://docs.djangoproject.com/en/dev/topics/db/transactions/#savepoints-in-sqlite"""
if connection.vendor != 'sqlite':
return
self.assertFalse(is_managed())
self.assertFalse(transaction.is_dirty())
cache.local.clear()

try:
with transaction.atomic():
g = Genre.objects.get(pk=1)
start_title = g.title
g.title = "Adventures in Savepoint World"
g.save()
g = Genre.objects.get(pk=1)
self.assertEqual(g.title, "Adventures in Savepoint World")
sid = transaction.savepoint()
g.title = "In the Void"
g.save()
g = Genre.objects.get(pk=1)
self.assertEqual(g.title, "In the Void")
transaction.savepoint_rollback(sid)
g = Genre.objects.get(pk=1)
self.assertEqual(g.title, "Adventures in Savepoint World")
raise IntegrityError('Exit transaction')
except IntegrityError:
pass
g = Genre.objects.get(pk=1)
self.assertEqual(g.title, start_title)

def test_savepoint_commit(self):
"""Tests a transaction commit (release)
The release actually pushes the savepoint back into the dirty stack,
Expand Down

0 comments on commit 4470401

Please sign in to comment.