Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@jezdez jezdez authored
Showing with 26 additions and 8 deletions.
  1. +16 −6 django/db/utils.py
  2. +10 −2 tests/regressiontests/backends/tests.py
View
22 django/db/utils.py
@@ -32,16 +32,26 @@ def load_backend(backend_name):
and not f.startswith('.')]
except EnvironmentError:
available_backends = []
- if backend_name.startswith('django.db.backends.'):
+ full_notation = backend_name.startswith('django.db.backends.')
+ if full_notation:
backend_name = backend_name[19:] # See #15621.
if backend_name not in available_backends:
- error_msg = ("%r isn't an available database backend. \n" +
- "Try using django.db.backends.XXX, where XXX is one of:\n %s\n" +
- "Error was: %s") % \
- (backend_name, ", ".join(map(repr, sorted(available_backends))), e_user)
+ backend_reprs = map(repr, sorted(available_backends))
+ error_msg = ("%r isn't an available database backend.\n"
+ "Try using django.db.backends.XXX, where XXX "
+ "is one of:\n %s\nError was: %s" %
+ (backend_name, ", ".join(backend_reprs), e_user))
+ raise ImproperlyConfigured(error_msg)
+ elif not full_notation:
+ # user tried to use the old notation for the database backend
+ error_msg = ("%r isn't an available database backend.\n"
+ "Try using django.db.backends.%s instead.\n"
+ "Error was: %s" %
+ (backend_name, backend_name, e_user))
raise ImproperlyConfigured(error_msg)
else:
- raise # If there's some other error, this must be an error in Django itself.
+ # If there's some other error, this must be an error in Django
+ raise
class ConnectionDoesNotExist(Exception):
View
12 tests/regressiontests/backends/tests.py
@@ -7,11 +7,12 @@
from django.conf import settings
from django.core.management.color import no_style
+from django.core.exceptions import ImproperlyConfigured
from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS,
IntegrityError, transaction)
from django.db.backends.signals import connection_created
from django.db.backends.postgresql_psycopg2 import version as pg_version
-from django.db.utils import ConnectionHandler, DatabaseError
+from django.db.utils import ConnectionHandler, DatabaseError, load_backend
from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase
from django.utils import unittest
@@ -582,4 +583,11 @@ def runner2(other_thread_connection):
t1.start()
t1.join()
# No exception was raised
- self.assertEqual(len(exceptions), 0)
+ self.assertEqual(len(exceptions), 0)
+
+
+class BackendLoadingTests(TestCase):
+ def test_old_style_backends_raise_useful_exception(self):
+ self.assertRaisesRegexp(ImproperlyConfigured,
+ "Try using django.db.backends.sqlite3 instead",
+ load_backend, 'sqlite3')
Please sign in to comment.
Something went wrong with that request. Please try again.