Anonymous avatar Anonymous committed 92290bb

Fixed #17300 -- Prevented createcachetable from crashing when the cache table already exists. Thanks Claude Paroz.

Comments (0)

Files changed (2)

django/core/management/commands/createcachetable.py

 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."
             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)

tests/regressiontests/cache/tests.py

 import hashlib
 import os
 import re
+import StringIO
 import tempfile
 import time
 import warnings
         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)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.