diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index 723027a575..cf48b92df1 100644 --- a/django/db/backends/creation.py +++ b/django/db/backends/creation.py @@ -524,6 +524,10 @@ class BaseDatabaseCreation(object): if not keepdb: self._destroy_test_db(test_database_name, verbosity) + # Restore the original database name + settings.DATABASES[self.connection.alias]["NAME"] = old_database_name + self.connection.settings_dict["NAME"] = old_database_name + def _destroy_test_db(self, test_database_name, verbosity): """ Internal implementation - remove the test db tables. diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index 55e7a82a96..66d98a40c5 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -6,6 +6,7 @@ from __future__ import unicode_literals import unittest from django import db +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.core.management import call_command from django.db.backends.dummy.base import DatabaseCreation @@ -336,6 +337,18 @@ class SetupDatabasesTests(unittest.TestCase): self.assertEqual(destroyed_names.count('dbname'), 1) + def test_destroy_test_db_restores_db_name(self): + db.connections = db.ConnectionHandler({ + 'default': { + 'ENGINE': settings.DATABASES[db.DEFAULT_DB_ALIAS]["ENGINE"], + 'NAME': 'xxx_test_database', + }, + }) + # Using the real current name as old_name to not mess with the test suite. + old_name = settings.DATABASES[db.DEFAULT_DB_ALIAS]["NAME"] + db.connections['default'].creation.destroy_test_db(old_name, verbosity=0, keepdb=True) + self.assertEqual(db.connections['default'].settings_dict["NAME"], old_name) + def test_serialization(self): serialize = [] DatabaseCreation.create_test_db = (