Skip to content

Commit

Permalink
[1.7.x] Fixed #23089 -- Fixed transaction handling in two management …
Browse files Browse the repository at this point in the history
…commands.

Previously, when createcachetable and flush operated on non-default
databases, they weren't atomic.

Backport of 753a22a from master
  • Loading branch information
aaugustin authored and timgraham committed Jul 24, 2014
1 parent b918bc9 commit 5ca82ff
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion django/core/management/commands/createcachetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create_table(self, database, tablename):
for i, line in enumerate(table_output):
full_statement.append(' %s%s' % (line, ',' if i < len(table_output) - 1 else ''))
full_statement.append(');')
with transaction.commit_on_success_unless_managed():
with transaction.atomic(using=database):
with connection.cursor() as curs:
try:
curs.execute("\n".join(full_statement))
Expand Down
2 changes: 1 addition & 1 deletion django/core/management/commands/flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def handle_noargs(self, **options):

if confirm == 'yes':
try:
with transaction.commit_on_success_unless_managed():
with transaction.atomic(using=db):
with connection.cursor() as cursor:
for sql in sql_list:
cursor.execute(sql)
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.6.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Bugfixes

* Restored ``pre_delete`` signals for ``GenericRelation`` cascade deletion
(`#22998 <https://code.djangoproject.com/ticket/22998>`_).

* Fixed transaction handling when specifying non-default database in
``createcachetable`` and ``flush``
(`#23089 <https://code.djangoproject.com/ticket/23089>`_).
8 changes: 5 additions & 3 deletions tests/cache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,9 +989,11 @@ def test_createcachetable_observes_database_router(self):
# cache table should be created on 'other'
# Queries:
# 1: check table doesn't already exist
# 2: create the table
# 3: create the index
with self.assertNumQueries(3, using='other'):
# 2: create savepoint
# 3: create the table
# 4: create the index
# 5: release savepoint
with self.assertNumQueries(5, using='other'):
management.call_command('createcachetable',
database='other',
verbosity=0, interactive=False)
Expand Down

0 comments on commit 5ca82ff

Please sign in to comment.