Ticket #17300: 17300-3.diff

File 17300-3.diff, 2.3 KB (added by Claude Paroz, 12 years ago)

New patch after 17513 resolution

  • django/core/management/commands/createcachetable.py

    diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py
    index 287a036..a92ccaa 100644
    a b from optparse import make_option  
    33from django.core.cache.backends.db import BaseDatabaseCache
    44from django.core.management.base import LabelCommand
    55from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
     6from django.db.utils import DatabaseError
    67
    78class Command(LabelCommand):
    89    help = "Creates the table needed to use the SQL cache backend."
    class Command(LabelCommand):  
    5152            full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or ''))
    5253        full_statement.append(');')
    5354        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 database '%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)
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index d31a5e0..c368a51 100644
    a b class DBCacheTests(BaseCacheTests, TransactionTestCase):  
    817817        self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
    818818        self.perform_cull_test(50, 18)
    819819
     820    def test_second_call_dont_crash(self):
     821        from StringIO import StringIO
     822        out = StringIO()
     823        management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False, stderr=out)
     824        self.assertTrue("Cache database 'test cache table' could not be created" in out.getvalue())
     825
    820826
    821827DBCacheWithTimeZoneTests = override_settings(USE_TZ=True)(DBCacheTests)
    822828
Back to Top