Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15255 -- Ensured createcachetable honors database routers.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17114 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d0eb4693ab479ef0667cd5286ecc89c347b6b2de 1 parent c8c7105
Aymeric Augustin authored November 19, 2011
4  django/contrib/gis/db/backends/spatialite/creation.py
@@ -61,9 +61,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):
61 61
         for cache_alias in settings.CACHES:
62 62
             cache = get_cache(cache_alias)
63 63
             if isinstance(cache, BaseDatabaseCache):
64  
-                from django.db import router
65  
-                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
66  
-                    call_command('createcachetable', cache._table, database=self.connection.alias)
  64
+                call_command('createcachetable', cache._table, database=self.connection.alias)
67 65
 
68 66
         # Get a cursor (even though we don't need one yet). This has
69 67
         # the side effect of initializing the test database.
12  django/core/management/commands/createcachetable.py
... ...
@@ -1,7 +1,8 @@
1 1
 from optparse import make_option
2 2
 
  3
+from django.core.cache.backends.db import BaseDatabaseCache
3 4
 from django.core.management.base import LabelCommand
4  
-from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
  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')
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
@@ -255,9 +255,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):
255 255
         for cache_alias in settings.CACHES:
256 256
             cache = get_cache(cache_alias)
257 257
             if isinstance(cache, BaseDatabaseCache):
258  
-                from django.db import router
259  
-                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
260  
-                    call_command('createcachetable', cache._table, database=self.connection.alias)
  258
+                call_command('createcachetable', cache._table, database=self.connection.alias)
261 259
 
262 260
         # Get a cursor (even though we don't need one yet). This has
263 261
         # the side effect of initializing the test database.
39  tests/regressiontests/cache/tests.py
@@ -16,6 +16,7 @@
16 16
 from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
17 17
 from django.core.cache.backends.base import (CacheKeyWarning,
18 18
     InvalidCacheBackendError)
  19
+from django.db import router
19 20
 from django.http import HttpResponse, HttpRequest, QueryDict
20 21
 from django.middleware.cache import (FetchFromCacheMiddleware,
21 22
     UpdateCacheMiddleware, CacheMiddleware)
@@ -775,6 +776,44 @@ def test_old_initialization(self):
775 776
         self.perform_cull_test(50, 18)
776 777
 
777 778
 
  779
+class DBCacheRouter(object):
  780
+    """A router that puts the cache table on the 'other' database."""
  781
+
  782
+    def db_for_read(self, model, **hints):
  783
+        if model._meta.app_label == 'django_cache':
  784
+            return 'other'
  785
+
  786
+    def db_for_write(self, model, **hints):
  787
+        if model._meta.app_label == 'django_cache':
  788
+            return 'other'
  789
+
  790
+    def allow_syncdb(self, db, model):
  791
+        if model._meta.app_label == 'django_cache':
  792
+            return db == 'other'
  793
+
  794
+
  795
+class CreateCacheTableForDBCacheTests(TestCase):
  796
+    multi_db = True
  797
+
  798
+    def test_createcachetable_observes_database_router(self):
  799
+        old_routers = router.routers
  800
+        try:
  801
+            router.routers = [DBCacheRouter()]
  802
+            # cache table should not be created on 'default'
  803
+            with self.assertNumQueries(0, using='default'):
  804
+                management.call_command('createcachetable', 'cache_table',
  805
+                                        database='default',
  806
+                                        verbosity=0, interactive=False)
  807
+            # cache table should be created on 'other'
  808
+            # one query is used to create the table and another one the index
  809
+            with self.assertNumQueries(2, using='other'):
  810
+                management.call_command('createcachetable', 'cache_table',
  811
+                                        database='other',
  812
+                                        verbosity=0, interactive=False)
  813
+        finally:
  814
+            router.routers = old_routers
  815
+
  816
+
778 817
 class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
779 818
     backend_name = 'django.core.cache.backends.locmem.LocMemCache'
780 819
 

0 notes on commit d0eb469

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