diff --git a/django/db/utils.py b/django/db/utils.py index 5459fc9a33..485d908fb9 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -154,57 +154,38 @@ class ConnectionHandler(BaseConnectionHandler): ) elif databases[DEFAULT_DB_ALIAS] == {}: databases[DEFAULT_DB_ALIAS]["ENGINE"] = "django.db.backends.dummy" + + # Configure default settings. + for conn in databases.values(): + conn.setdefault("ATOMIC_REQUESTS", False) + conn.setdefault("AUTOCOMMIT", True) + conn.setdefault("ENGINE", "django.db.backends.dummy") + if conn["ENGINE"] == "django.db.backends." or not conn["ENGINE"]: + conn["ENGINE"] = "django.db.backends.dummy" + conn.setdefault("CONN_MAX_AGE", 0) + conn.setdefault("CONN_HEALTH_CHECKS", False) + conn.setdefault("OPTIONS", {}) + conn.setdefault("TIME_ZONE", None) + for setting in ["NAME", "USER", "PASSWORD", "HOST", "PORT"]: + conn.setdefault(setting, "") + + test_settings = conn.setdefault("TEST", {}) + default_test_settings = [ + ("CHARSET", None), + ("COLLATION", None), + ("MIGRATE", True), + ("MIRROR", None), + ("NAME", None), + ] + for key, value in default_test_settings: + test_settings.setdefault(key, value) return databases @property def databases(self): return self.settings - def ensure_defaults(self, alias): - """ - Put the defaults into the settings dictionary for a given connection - where no settings is provided. - """ - try: - conn = self.settings[alias] - except KeyError: - raise self.exception_class(f"The connection '{alias}' doesn't exist.") - - conn.setdefault("ATOMIC_REQUESTS", False) - conn.setdefault("AUTOCOMMIT", True) - conn.setdefault("ENGINE", "django.db.backends.dummy") - if conn["ENGINE"] == "django.db.backends." or not conn["ENGINE"]: - conn["ENGINE"] = "django.db.backends.dummy" - conn.setdefault("CONN_MAX_AGE", 0) - conn.setdefault("CONN_HEALTH_CHECKS", False) - conn.setdefault("OPTIONS", {}) - conn.setdefault("TIME_ZONE", None) - for setting in ["NAME", "USER", "PASSWORD", "HOST", "PORT"]: - conn.setdefault(setting, "") - - def prepare_test_settings(self, alias): - """ - Make sure the test settings are available in the 'TEST' sub-dictionary. - """ - try: - conn = self.settings[alias] - except KeyError: - raise self.exception_class(f"The connection '{alias}' doesn't exist.") - - test_settings = conn.setdefault("TEST", {}) - default_test_settings = [ - ("CHARSET", None), - ("COLLATION", None), - ("MIGRATE", True), - ("MIRROR", None), - ("NAME", None), - ] - for key, value in default_test_settings: - test_settings.setdefault(key, value) - def create_connection(self, alias): - self.ensure_defaults(alias) - self.prepare_test_settings(alias) db = self.settings[alias] backend = load_backend(db["ENGINE"]) return backend.DatabaseWrapper(db, alias) diff --git a/tests/db_utils/tests.py b/tests/db_utils/tests.py index 99b58d0b8a..fe1d1e2d60 100644 --- a/tests/db_utils/tests.py +++ b/tests/db_utils/tests.py @@ -50,26 +50,6 @@ class ConnectionHandlerTests(SimpleTestCase): with self.assertRaisesMessage(ConnectionDoesNotExist, msg): conns["nonexistent"] - def test_ensure_defaults_nonexistent_alias(self): - msg = "The connection 'nonexistent' doesn't exist." - conns = ConnectionHandler( - { - DEFAULT_DB_ALIAS: {"ENGINE": "django.db.backends.dummy"}, - } - ) - with self.assertRaisesMessage(ConnectionDoesNotExist, msg): - conns.ensure_defaults("nonexistent") - - def test_prepare_test_settings_nonexistent_alias(self): - msg = "The connection 'nonexistent' doesn't exist." - conns = ConnectionHandler( - { - DEFAULT_DB_ALIAS: {"ENGINE": "django.db.backends.dummy"}, - } - ) - with self.assertRaisesMessage(ConnectionDoesNotExist, msg): - conns.prepare_test_settings("nonexistent") - class DatabaseErrorWrapperTests(TestCase): @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test")