Ticket #10868: 10868.2.diff

File 10868.2.diff, 3.6 KB (added by Anssi Kääriäinen, 7 years ago)

settings-dict-copy.2 with minor cleanup

  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index ce1da6d..7892143 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
    325         self._destroy_test_db(test_database_name, verbosity)
     326        # Temporarily use a new connection and a copy of the settings dict.
     327        # This prevents the production database from being exposed to potential
     328        # child threads while (or after) the test database is destroyed. Refs
     329        # #10868.
     330        settings_dict = self.connection.settings_dict.copy()
     331        settings_dict['NAME'] = old_database_name
     332        backend = load_backend(settings_dict['ENGINE'])
     333        new_conn = backend.DatabaseWrapper(
     334                              settings_dict,
     335                              alias='__destroy_test_db__',
     336                              allow_thread_sharing=False)
     337        new_conn.creation._destroy_test_db(test_database_name, verbosity)
    326338
    327339    def _destroy_test_db(self, test_database_name, verbosity):
    328340        "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..8259f69 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()
  • docs/releases/1.4.txt

    diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt
    index 9b3c219..a9ce0f4 100644
    a b apply URL escaping again. This is wrong for URLs whose unquoted form contains 
    942942a ``%xx`` sequence, but such URLs are very unlikely to happen in the wild,
    943943since they would confuse browsers too.
    944944
     945Database connections after running the test suite
     946~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     947
     948The default test runner used to reset the django.db.connections so that after
     949the tests were ran, and test databases dropped, new connections would be made
     950to the production database. This exposed the production database to threads
     951created in the tests that were for some reason still running.
     952
     953If your code relied on connections to the production database after the tests
     954were ran, you will need to subclass DjangoTestRunner and override its
     955teardown_databases() method.
     956
    945957Features deprecated in 1.4
    946958==========================
    947959
Back to Top