Ticket #10868: 10868.settings-dict-copy.2.diff

File 10868.settings-dict-copy.2.diff, 2.7 KB (added by Julien Phalip, 8 years ago)
  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index ce1da6d..7855f26 100644
    a b from django.conf import settings 
    55
    66# The prefix to put on the default database name when creating
    77# the test database.
     8from django.db.utils import load_backend
     9
    810TEST_DATABASE_PREFIX = 'test_'
    911
    1012class BaseDatabaseCreation(object):
    class BaseDatabaseCreation(object): 
    320322            if verbosity >= 2:
    321323                test_db_repr = " ('%s')" % test_database_name
    322324            print "Destroying test database for alias '%s'%s..." % (self.connection.alias, test_db_repr)
    323         self.connection.settings_dict['NAME'] = old_database_name
    324325
     326        # Temporarily use a new thread-local connection and a thread-local
     327        # copy of the settings dict. This prevents the production database from
     328        # being exposed to potential child threads while the test database is
     329        # being destroyed. Refs #10868.
     330        original_connection = self.connection
     331        settings_dict = original_connection.settings_dict.copy()
     332        settings_dict['NAME'] = old_database_name
     333        backend = load_backend(settings_dict['ENGINE'])
     334        self.connection = backend.DatabaseWrapper(
     335                              settings_dict,
     336                              alias='__destroy_test_db__',
     337                              allow_thread_sharing=False)
    325338        self._destroy_test_db(test_database_name, verbosity)
     339        self.connection = original_connection
    326340
    327341    def _destroy_test_db(self, test_database_name, verbosity):
    328342        "Internal implementation - remove the test db tables."
  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    index 1534011..73aeb2c 100644
    a b class DjangoTestSuiteRunner(object): 
    312312        return unittest.TextTestRunner(verbosity=self.verbosity, failfast=self.failfast).run(suite)
    313313
    314314    def teardown_databases(self, old_config, **kwargs):
    315         from django.db import connections
     315        """
     316        Destroys all the non-mirror databases
     317        """
    316318        old_names, mirrors = old_config
    317         # Point all the mirrors back to the originals
    318         for alias, old_name in mirrors:
    319             connections[alias].settings_dict['NAME'] = old_name
    320         # Destroy all the non-mirror databases
    321319        for connection, old_name, destroy in old_names:
    322320            if destroy:
    323321                connection.creation.destroy_test_db(old_name, self.verbosity)
    324             else:
    325                 connection.settings_dict['NAME'] = old_name
    326322
    327323    def teardown_test_environment(self, **kwargs):
    328324        unittest.removeHandler()
Back to Top