Skip to content

Commit f1cc2be

Browse files
committed
Fixed django#18575 -- Empty DATABASES should default to dummy backend
Thanks delormemarco@gmail.com for the report.
1 parent effe96b commit f1cc2be

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

django/conf/global_settings.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,8 @@
150150
# Whether to send broken-link emails.
151151
SEND_BROKEN_LINK_EMAILS = False
152152

153-
# Database connection info.
154-
DATABASES = {
155-
'default': {
156-
'ENGINE': 'django.db.backends.dummy',
157-
},
158-
}
153+
# Database connection info. If left empty, will default to the dummy backend.
154+
DATABASES = {}
159155

160156
# Classes used to implement DB routing behavior.
161157
DATABASE_ROUTERS = []

django/db/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
'IntegrityError', 'DEFAULT_DB_ALIAS')
99

1010

11-
if DEFAULT_DB_ALIAS not in settings.DATABASES:
11+
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
1212
raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
1313

1414
connections = ConnectionHandler(settings.DATABASES)

django/db/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ class ConnectionDoesNotExist(Exception):
5353

5454
class ConnectionHandler(object):
5555
def __init__(self, databases):
56-
self.databases = databases
56+
if not databases:
57+
self.databases = {
58+
DEFAULT_DB_ALIAS: {
59+
'ENGINE': 'django.db.backends.dummy',
60+
},
61+
}
62+
else:
63+
self.databases = databases
5764
self._connections = local()
5865

5966
def ensure_defaults(self, alias):

tests/regressiontests/backends/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
from . import models
2424

2525

26+
class DummyBackendTest(TestCase):
27+
def test_no_databases(self):
28+
"""
29+
Test that empty DATABASES setting default to the dummy backend.
30+
"""
31+
DATABASES = {}
32+
conns = ConnectionHandler(DATABASES)
33+
self.assertEqual(conns[DEFAULT_DB_ALIAS].settings_dict['ENGINE'],
34+
'django.db.backends.dummy')
35+
36+
2637
class OracleChecks(unittest.TestCase):
2738

2839
@unittest.skipUnless(connection.vendor == 'oracle',

0 commit comments

Comments
 (0)