Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17047 -- Improved django.db.utils.load_backend to raise a bett…

…er exception when using a unqualified database backend name. Thanks, Jonas Obrist.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17274 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit db2b1458b1fc83c8485687634934b7c4909cde78 1 parent ff1f423
Jannis Leidel authored December 25, 2011
22  django/db/utils.py
@@ -32,16 +32,26 @@ def load_backend(backend_name):
32 32
                     and not f.startswith('.')]
33 33
         except EnvironmentError:
34 34
             available_backends = []
35  
-        if backend_name.startswith('django.db.backends.'):
  35
+        full_notation = backend_name.startswith('django.db.backends.')
  36
+        if full_notation:
36 37
             backend_name = backend_name[19:] # See #15621.
37 38
         if backend_name not in available_backends:
38  
-            error_msg = ("%r isn't an available database backend. \n" +
39  
-                "Try using django.db.backends.XXX, where XXX is one of:\n    %s\n" +
40  
-                "Error was: %s") % \
41  
-                (backend_name, ", ".join(map(repr, sorted(available_backends))), e_user)
  39
+            backend_reprs = map(repr, sorted(available_backends))
  40
+            error_msg = ("%r isn't an available database backend.\n"
  41
+                         "Try using django.db.backends.XXX, where XXX "
  42
+                         "is one of:\n    %s\nError was: %s" %
  43
+                         (backend_name, ", ".join(backend_reprs), e_user))
  44
+            raise ImproperlyConfigured(error_msg)
  45
+        elif not full_notation:
  46
+            # user tried to use the old notation for the database backend
  47
+            error_msg = ("%r isn't an available database backend.\n"
  48
+                         "Try using django.db.backends.%s instead.\n"
  49
+                         "Error was: %s" %
  50
+                         (backend_name, backend_name, e_user))
42 51
             raise ImproperlyConfigured(error_msg)
43 52
         else:
44  
-            raise # If there's some other error, this must be an error in Django itself.
  53
+            # If there's some other error, this must be an error in Django
  54
+            raise
45 55
 
46 56
 
47 57
 class ConnectionDoesNotExist(Exception):
12  tests/regressiontests/backends/tests.py
@@ -7,11 +7,12 @@
7 7
 
8 8
 from django.conf import settings
9 9
 from django.core.management.color import no_style
  10
+from django.core.exceptions import ImproperlyConfigured
10 11
 from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS,
11 12
     IntegrityError, transaction)
12 13
 from django.db.backends.signals import connection_created
13 14
 from django.db.backends.postgresql_psycopg2 import version as pg_version
14  
-from django.db.utils import ConnectionHandler, DatabaseError
  15
+from django.db.utils import ConnectionHandler, DatabaseError, load_backend
15 16
 from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase
16 17
 from django.utils import unittest
17 18
 
@@ -582,4 +583,11 @@ def runner2(other_thread_connection):
582 583
         t1.start()
583 584
         t1.join()
584 585
         # No exception was raised
585  
-        self.assertEqual(len(exceptions), 0)
  586
+        self.assertEqual(len(exceptions), 0)
  587
+
  588
+
  589
+class BackendLoadingTests(TestCase):
  590
+    def test_old_style_backends_raise_useful_exception(self):
  591
+        self.assertRaisesRegexp(ImproperlyConfigured,
  592
+            "Try using django.db.backends.sqlite3 instead",
  593
+            load_backend, 'sqlite3')

0 notes on commit db2b145

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