From f1cc2be0c53858b673afc3b26347d3bb25e424f6 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sun, 28 Oct 2012 23:02:41 +0100 Subject: [PATCH] Fixed #18575 -- Empty DATABASES should default to dummy backend Thanks delormemarco@gmail.com for the report. --- django/conf/global_settings.py | 8 ++------ django/db/__init__.py | 2 +- django/db/utils.py | 9 ++++++++- tests/regressiontests/backends/tests.py | 11 +++++++++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index f1cbb22880..84296c7493 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -150,12 +150,8 @@ SERVER_EMAIL = 'root@localhost' # Whether to send broken-link emails. SEND_BROKEN_LINK_EMAILS = False -# Database connection info. -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.dummy', - }, -} +# Database connection info. If left empty, will default to the dummy backend. +DATABASES = {} # Classes used to implement DB routing behavior. DATABASE_ROUTERS = [] diff --git a/django/db/__init__.py b/django/db/__init__.py index 26c7add0af..b1980488df 100644 --- a/django/db/__init__.py +++ b/django/db/__init__.py @@ -8,7 +8,7 @@ __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError', 'IntegrityError', 'DEFAULT_DB_ALIAS') -if DEFAULT_DB_ALIAS not in settings.DATABASES: +if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES: raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS) connections = ConnectionHandler(settings.DATABASES) diff --git a/django/db/utils.py b/django/db/utils.py index 5fa78fe350..a91298626b 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -53,7 +53,14 @@ class ConnectionDoesNotExist(Exception): class ConnectionHandler(object): def __init__(self, databases): - self.databases = databases + if not databases: + self.databases = { + DEFAULT_DB_ALIAS: { + 'ENGINE': 'django.db.backends.dummy', + }, + } + else: + self.databases = databases self._connections = local() def ensure_defaults(self, alias): diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index 4766dcf44e..14b3e0053c 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -23,6 +23,17 @@ from django.utils import unittest from . import models +class DummyBackendTest(TestCase): + def test_no_databases(self): + """ + Test that empty DATABASES setting default to the dummy backend. + """ + DATABASES = {} + conns = ConnectionHandler(DATABASES) + self.assertEqual(conns[DEFAULT_DB_ALIAS].settings_dict['ENGINE'], + 'django.db.backends.dummy') + + class OracleChecks(unittest.TestCase): @unittest.skipUnless(connection.vendor == 'oracle',