From 45db842c9bcd6f9afe274c120f43847657473839 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 24 Oct 2014 14:53:58 +0200 Subject: [PATCH] Fixed #23652 -- Restored database name after destroying test database Thanks Bjarkias for the report. --- django/db/backends/creation.py | 4 ++++ tests/test_runner/tests.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index 723027a5756..cf48b92df19 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 55e7a82a960..66d98a40c59 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 = (