Skip to content

Commit

Permalink
Fixed #17300 -- Prevented createcachetable from crashing when the cac…
Browse files Browse the repository at this point in the history
…he table already exists. Thanks Claude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17363 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
aaugustin committed Jan 8, 2012
1 parent 15d10a5 commit 19c544f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
16 changes: 12 additions & 4 deletions django/core/management/commands/createcachetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.cache.backends.db import BaseDatabaseCache
from django.core.management.base import LabelCommand
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
from django.db.utils import DatabaseError

class Command(LabelCommand):
help = "Creates the table needed to use the SQL cache backend."
Expand Down Expand Up @@ -51,7 +52,14 @@ def handle_label(self, tablename, **options):
full_statement.append(' %s%s' % (line, i < len(table_output)-1 and ',' or ''))
full_statement.append(');')
curs = connection.cursor()
curs.execute("\n".join(full_statement))
for statement in index_output:
curs.execute(statement)
transaction.commit_unless_managed(using=db)
try:
curs.execute("\n".join(full_statement))
except DatabaseError, e:
self.stderr.write(
self.style.ERROR("Cache table '%s' could not be created.\nThe error was: %s.\n" %
(tablename, e)))
transaction.rollback_unless_managed(using=db)
else:
for statement in index_output:
curs.execute(statement)
transaction.commit_unless_managed(using=db)
6 changes: 6 additions & 0 deletions tests/regressiontests/cache/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hashlib
import os
import re
import StringIO
import tempfile
import time
import warnings
Expand Down Expand Up @@ -817,6 +818,11 @@ def test_old_initialization(self):
self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
self.perform_cull_test(50, 18)

def test_second_call_doesnt_crash(self):
err = StringIO.StringIO()
management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False, stderr=err)
self.assertTrue("Cache table 'test cache table' could not be created" in err.getvalue())


DBCacheWithTimeZoneTests = override_settings(USE_TZ=True)(DBCacheTests)

Expand Down

0 comments on commit 19c544f

Please sign in to comment.