Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 6de65ab76ff7704c90c6de9d5954c7736325c3fb 1 parent 94a38df
Jannis Leidel authored
4  django/contrib/gis/db/backends/spatialite/creation.py
@@ -33,9 +33,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):
33 33
         for cache_alias in settings.CACHES:
34 34
             cache = get_cache(cache_alias)
35 35
             if isinstance(cache, BaseDatabaseCache):
36  
-                from django.db import router
37  
-                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
38  
-                    call_command('createcachetable', cache._table, database=self.connection.alias)
  36
+                call_command('createcachetable', cache._table, database=self.connection.alias)
39 37
         # Get a cursor (even though we don't need one yet). This has
40 38
         # the side effect of initializing the test database.
41 39
         cursor = self.connection.cursor()
12  django/core/management/commands/createcachetable.py
... ...
@@ -1,7 +1,8 @@
1 1
 from optparse import make_option
2 2
 
3 3
 from django.core.management.base import LabelCommand
4  
-from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
  4
+from django.core.cache.backends.db import BaseDatabaseCache
  5
+from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
5 6
 
6 7
 class Command(LabelCommand):
7 8
     help = "Creates the table needed to use the SQL cache backend."
@@ -18,8 +19,11 @@ class Command(LabelCommand):
18 19
     requires_model_validation = False
19 20
 
20 21
     def handle_label(self, tablename, **options):
21  
-        alias = options.get('database', DEFAULT_DB_ALIAS)
22  
-        connection = connections[alias]
  22
+        db = options.get('database', DEFAULT_DB_ALIAS)
  23
+        cache = BaseDatabaseCache(tablename, {})
  24
+        if not router.allow_syncdb(db, cache.cache_model_class):
  25
+            return
  26
+        connection = connections[db]
23 27
         fields = (
24 28
             # "key" is a reserved word in MySQL, so use "cache_key" instead.
25 29
             models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
@@ -50,4 +54,4 @@ def handle_label(self, tablename, **options):
50 54
         curs.execute("\n".join(full_statement))
51 55
         for statement in index_output:
52 56
             curs.execute(statement)
53  
-        transaction.commit_unless_managed(using=alias)
  57
+        transaction.commit_unless_managed(using=db)
4  django/db/backends/creation.py
@@ -261,9 +261,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):
261 261
         for cache_alias in settings.CACHES:
262 262
             cache = get_cache(cache_alias)
263 263
             if isinstance(cache, BaseDatabaseCache):
264  
-                from django.db import router
265  
-                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
266  
-                    call_command('createcachetable', cache._table, database=self.connection.alias)
  264
+                call_command('createcachetable', cache._table, database=self.connection.alias)
267 265
 
268 266
         # Get a cursor (even though we don't need one yet). This has
269 267
         # the side effect of initializing the test database.
38  tests/regressiontests/cache/tests.py
@@ -13,10 +13,11 @@
13 13
 from django.core import management
14 14
 from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
15 15
 from django.core.cache.backends.base import CacheKeyWarning
  16
+from django.db import connections, router
16 17
 from django.http import HttpResponse, HttpRequest, QueryDict
17 18
 from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware
18 19
 from django.test import RequestFactory
19  
-from django.test.utils import get_warnings_state, restore_warnings_state
  20
+from django.test.utils import get_warnings_state, restore_warnings_state, override_settings
20 21
 from django.utils import translation
21 22
 from django.utils import unittest
22 23
 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
@@ -757,6 +758,41 @@ def test_old_initialization(self):
757 758
         self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
758 759
         self.perform_cull_test(50, 18)
759 760
 
  761
+class DBCacheRouter(object):
  762
+    """A router that puts the cache table on the 'other' database."""
  763
+
  764
+    def db_for_read(self, model, **hints):
  765
+        if model._meta.app_label == 'django_cache':
  766
+            return 'other'
  767
+
  768
+    def db_for_write(self, model, **hints):
  769
+        if model._meta.app_label == 'django_cache':
  770
+            return 'other'
  771
+
  772
+    def allow_syncdb(self, db, model):
  773
+        if model._meta.app_label == 'django_cache':
  774
+            return db == 'other'
  775
+
  776
+class CreateCacheTableForDBCacheTests(unittest.TestCase):
  777
+
  778
+    @override_settings(DEBUG=True)
  779
+    def test_createcachetable_observes_database_router(self):
  780
+        old_routers = router.routers
  781
+        try:
  782
+            router.routers = [DBCacheRouter()]
  783
+            # cache table should not be created on 'default'
  784
+            management.call_command('createcachetable', 'cache_table',
  785
+                                    database='default',
  786
+                                    verbosity=0, interactive=False)
  787
+            self.assertEqual(len(connections['default'].queries), 0)
  788
+            # cache table should be created on 'other'
  789
+            management.call_command('createcachetable', 'cache_table',
  790
+                                    database='other',
  791
+                                    verbosity=0, interactive=False)
  792
+            self.assertNotEqual(len(connections['other'].queries), 0)
  793
+        finally:
  794
+            router.routers = old_routers
  795
+
760 796
 class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
761 797
     backend_name = 'django.core.cache.backends.locmem.LocMemCache'
762 798
 

0 notes on commit 6de65ab

Please sign in to comment.
Something went wrong with that request. Please try again.