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

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

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index ce1da6d..9ab7228 100644
    a b class BaseDatabaseCreation(object): 
    320320            if verbosity >= 2:
    321321                test_db_repr = " ('%s')" % test_database_name
    322322            print "Destroying test database for alias '%s'%s..." % (self.connection.alias, test_db_repr)
     323        # Use a thread-local copy of the settings dict. That way we avoid
     324        # modifying the original settings dict which would be shared between
     325        # any potential child threads. This prevents the production database
     326        # from being exposed to those potential child threads while the test
     327        # database is being destroyed. Refs #10868.
     328        self.connection.settings_dict = self.connection.settings_dict.copy()
    323329        self.connection.settings_dict['NAME'] = old_database_name
    324 
    325330        self._destroy_test_db(test_database_name, verbosity)
    326331
    327332    def _destroy_test_db(self, test_database_name, verbosity):
  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    index 1534011..47bc7e0 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        """
     316        Clean up database connections and destroy test databases.
     317
     318        Note that we use thread-local copies of the connections' settings
     319        dict references. That way we avoid modifying the original settings dict
     320        references which would be shared between any potential child threads.
     321        This prevents the production databases from being exposed to those
     322        potential child threads while the test databases are being teared down.
     323        Refs #10868.
     324        """
    315325        from django.db import connections
    316326        old_names, mirrors = old_config
    317327        # Point all the mirrors back to the originals
    318328        for alias, old_name in mirrors:
     329            connections[alias].settings_dict = connections[alias].settings_dict.copy()
    319330            connections[alias].settings_dict['NAME'] = old_name
    320331        # Destroy all the non-mirror databases
    321332        for connection, old_name, destroy in old_names:
    322333            if destroy:
    323334                connection.creation.destroy_test_db(old_name, self.verbosity)
    324335            else:
     336                connection.settings_dict = connection.settings_dict.copy()
    325337                connection.settings_dict['NAME'] = old_name
    326338
    327339    def teardown_test_environment(self, **kwargs):
Back to Top