From 6de65ab76ff7704c90c6de9d5954c7736325c3fb Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 4 Jul 2011 21:53:17 +0000 Subject: [PATCH] Fixed #15255 -- Stopped database cache from ignoring database routers when creating the cache table. Thanks, aaugustin. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16510 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../gis/db/backends/spatialite/creation.py | 4 +- .../management/commands/createcachetable.py | 12 ++++-- django/db/backends/creation.py | 4 +- tests/regressiontests/cache/tests.py | 38 ++++++++++++++++++- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py index 41baf53e7f..d299208d79 100644 --- a/django/contrib/gis/db/backends/spatialite/creation.py +++ b/django/contrib/gis/db/backends/spatialite/creation.py @@ -33,9 +33,7 @@ class SpatiaLiteCreation(DatabaseCreation): for cache_alias in settings.CACHES: cache = get_cache(cache_alias) if isinstance(cache, BaseDatabaseCache): - from django.db import router - if router.allow_syncdb(self.connection.alias, cache.cache_model_class): - call_command('createcachetable', cache._table, database=self.connection.alias) + call_command('createcachetable', cache._table, database=self.connection.alias) # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database. cursor = self.connection.cursor() diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index 90172b6ca3..e2f17abdc5 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -1,7 +1,8 @@ from optparse import make_option from django.core.management.base import LabelCommand -from django.db import connections, transaction, models, DEFAULT_DB_ALIAS +from django.core.cache.backends.db import BaseDatabaseCache +from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS class Command(LabelCommand): help = "Creates the table needed to use the SQL cache backend." @@ -18,8 +19,11 @@ class Command(LabelCommand): requires_model_validation = False def handle_label(self, tablename, **options): - alias = options.get('database', DEFAULT_DB_ALIAS) - connection = connections[alias] + db = options.get('database', DEFAULT_DB_ALIAS) + cache = BaseDatabaseCache(tablename, {}) + if not router.allow_syncdb(db, cache.cache_model_class): + return + connection = connections[db] fields = ( # "key" is a reserved word in MySQL, so use "cache_key" instead. models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True), @@ -50,4 +54,4 @@ class Command(LabelCommand): curs.execute("\n".join(full_statement)) for statement in index_output: curs.execute(statement) - transaction.commit_unless_managed(using=alias) + transaction.commit_unless_managed(using=db) diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index 437a4297af..6170bf6b97 100644 --- a/django/db/backends/creation.py +++ b/django/db/backends/creation.py @@ -261,9 +261,7 @@ class BaseDatabaseCreation(object): for cache_alias in settings.CACHES: cache = get_cache(cache_alias) if isinstance(cache, BaseDatabaseCache): - from django.db import router - if router.allow_syncdb(self.connection.alias, cache.cache_model_class): - call_command('createcachetable', cache._table, database=self.connection.alias) + call_command('createcachetable', cache._table, database=self.connection.alias) # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database. diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index cf74b7458e..ad7a2f86dc 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -13,10 +13,11 @@ from django.conf import settings from django.core import management from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS from django.core.cache.backends.base import CacheKeyWarning +from django.db import connections, router from django.http import HttpResponse, HttpRequest, QueryDict from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware from django.test import RequestFactory -from django.test.utils import get_warnings_state, restore_warnings_state +from django.test.utils import get_warnings_state, restore_warnings_state, override_settings from django.utils import translation from django.utils import unittest from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key @@ -757,6 +758,41 @@ class DBCacheTests(unittest.TestCase, BaseCacheTests): self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name) self.perform_cull_test(50, 18) +class DBCacheRouter(object): + """A router that puts the cache table on the 'other' database.""" + + def db_for_read(self, model, **hints): + if model._meta.app_label == 'django_cache': + return 'other' + + def db_for_write(self, model, **hints): + if model._meta.app_label == 'django_cache': + return 'other' + + def allow_syncdb(self, db, model): + if model._meta.app_label == 'django_cache': + return db == 'other' + +class CreateCacheTableForDBCacheTests(unittest.TestCase): + + @override_settings(DEBUG=True) + def test_createcachetable_observes_database_router(self): + old_routers = router.routers + try: + router.routers = [DBCacheRouter()] + # cache table should not be created on 'default' + management.call_command('createcachetable', 'cache_table', + database='default', + verbosity=0, interactive=False) + self.assertEqual(len(connections['default'].queries), 0) + # cache table should be created on 'other' + management.call_command('createcachetable', 'cache_table', + database='other', + verbosity=0, interactive=False) + self.assertNotEqual(len(connections['other'].queries), 0) + finally: + router.routers = old_routers + class LocMemCacheTests(unittest.TestCase, BaseCacheTests): backend_name = 'django.core.cache.backends.locmem.LocMemCache'