Fixed #23933 -- Made override_settings(DATABASE_ROUTERS) affect the master router.

This commit is contained in:
wrwrwr 2014-11-29 14:42:06 +01:00 committed by Tim Graham
parent d2202ec2d4
commit e6f19ec322
3 changed files with 20 additions and 2 deletions

View File

@ -4,7 +4,8 @@ import threading
import warnings
from django.conf import settings
from django.db import connections
from django.db import connections, router
from django.db.utils import ConnectionRouter
from django.dispatch import receiver, Signal
from django.utils import timezone
from django.utils.functional import empty
@ -77,6 +78,12 @@ def update_connections_time_zone(**kwargs):
conn.cursor().execute(tz_sql, [tz])
@receiver(setting_changed)
def clear_routers_cache(**kwargs):
if kwargs['setting'] == 'DATABASE_ROUTERS':
router.routers = ConnectionRouter().routers
@receiver(setting_changed)
def reset_default_template_engine(**kwargs):
if kwargs['setting'] in {

View File

@ -500,6 +500,9 @@ Tests
for Oracle: :setting:`DATAFILE`, :setting:`DATAFILE_TMP`,
:setting:`DATAFILE_MAXSIZE` and :setting:`DATAFILE_TMP_MAXSIZE`.
* The :func:`~django.test.override_settings` decorator can now affect the
master router in :setting:`DATABASE_ROUTERS`.
Validators
^^^^^^^^^^

View File

@ -6,7 +6,7 @@ import unittest
from django.conf.urls import url
from django.core.files.storage import default_storage
from django.core.urlresolvers import NoReverseMatch, reverse
from django.db import connection
from django.db import connection, router
from django.forms import EmailField, IntegerField
from django.http import HttpResponse
from django.template.loader import render_to_string
@ -842,3 +842,11 @@ class OverrideSettingsTests(TestCase):
self.assertIsNone(default_storage.directory_permissions_mode)
with self.settings(FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o777):
self.assertEqual(default_storage.directory_permissions_mode, 0o777)
def test_override_database_routers(self):
"""
Overriding DATABASE_ROUTERS should update the master router.
"""
test_routers = (object(),)
with self.settings(DATABASE_ROUTERS=test_routers):
self.assertSequenceEqual(router.routers, test_routers)