From cc4b4d9fd34d9b601de0bafaa3c1f249729fa49a Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sun, 27 May 2012 23:03:21 +0200 Subject: [PATCH] Used CommandError in createcachetable command. Raising CommandError whenever a management command meets an error condition is the standard way to handle errors in commands. --- django/core/management/commands/createcachetable.py | 13 ++++++------- tests/regressiontests/cache/tests.py | 12 ++++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index fec3aff67c..bcc47e17c8 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -1,7 +1,7 @@ from optparse import make_option from django.core.cache.backends.db import BaseDatabaseCache -from django.core.management.base import LabelCommand +from django.core.management.base import LabelCommand, CommandError from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS from django.db.utils import DatabaseError @@ -55,11 +55,10 @@ class Command(LabelCommand): try: curs.execute("\n".join(full_statement)) except DatabaseError as e: - self.stderr.write( + transaction.rollback_unless_managed(using=db) + raise CommandError( "Cache table '%s' could not be created.\nThe error was: %s." % (tablename, e)) - transaction.rollback_unless_managed(using=db) - else: - for statement in index_output: - curs.execute(statement) - transaction.commit_unless_managed(using=db) + for statement in index_output: + curs.execute(statement) + transaction.commit_unless_managed(using=db) diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index cbba7c19a3..88fe5471cf 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -7,7 +7,6 @@ from __future__ import absolute_import import hashlib import os import re -import StringIO import tempfile import time import warnings @@ -820,9 +819,14 @@ class DBCacheTests(BaseCacheTests, TransactionTestCase): 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(b"Cache table 'test cache table' could not be created" in err.getvalue()) + with self.assertRaisesRegexp(management.CommandError, + "Cache table 'test cache table' could not be created"): + management.call_command( + 'createcachetable', + self._table_name, + verbosity=0, + interactive=False + ) @override_settings(USE_TZ=True)