Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17300 -- Prevented createcachetable from crashing when the cac…

…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...
commit 19c544ff4250720558119084a60daa6363f45919 1 parent 15d10a5
Aymeric Augustin authored January 08, 2012
16  django/core/management/commands/createcachetable.py
@@ -3,6 +3,7 @@
3 3
 from django.core.cache.backends.db import BaseDatabaseCache
4 4
 from django.core.management.base import LabelCommand
5 5
 from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
  6
+from django.db.utils import DatabaseError
6 7
 
7 8
 class Command(LabelCommand):
8 9
     help = "Creates the table needed to use the SQL cache backend."
@@ -51,7 +52,14 @@ def handle_label(self, tablename, **options):
51 52
             full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or ''))
52 53
         full_statement.append(');')
53 54
         curs = connection.cursor()
54  
-        curs.execute("\n".join(full_statement))
55  
-        for statement in index_output:
56  
-            curs.execute(statement)
57  
-        transaction.commit_unless_managed(using=db)
  55
+        try:
  56
+            curs.execute("\n".join(full_statement))
  57
+        except DatabaseError, e:
  58
+            self.stderr.write(
  59
+                self.style.ERROR("Cache table '%s' could not be created.\nThe error was: %s.\n" %
  60
+                    (tablename, e)))
  61
+            transaction.rollback_unless_managed(using=db)
  62
+        else:
  63
+            for statement in index_output:
  64
+                curs.execute(statement)
  65
+            transaction.commit_unless_managed(using=db)
6  tests/regressiontests/cache/tests.py
@@ -7,6 +7,7 @@
7 7
 import hashlib
8 8
 import os
9 9
 import re
  10
+import StringIO
10 11
 import tempfile
11 12
 import time
12 13
 import warnings
@@ -817,6 +818,11 @@ def test_old_initialization(self):
817 818
         self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
818 819
         self.perform_cull_test(50, 18)
819 820
 
  821
+    def test_second_call_doesnt_crash(self):
  822
+        err = StringIO.StringIO()
  823
+        management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False, stderr=err)
  824
+        self.assertTrue("Cache table 'test cache table' could not be created" in err.getvalue())
  825
+
820 826
 
821 827
 DBCacheWithTimeZoneTests = override_settings(USE_TZ=True)(DBCacheTests)
822 828
 

0 notes on commit 19c544f

Please sign in to comment.
Something went wrong with that request. Please try again.