Code

Ticket #15255: 15255.3.diff

File 15255.3.diff, 6.0 KB (added by aaugustin, 3 years ago)
Line 
1Index: django/db/backends/creation.py
2===================================================================
3--- django/db/backends/creation.py      (revision 16285)
4+++ django/db/backends/creation.py      (working copy)
5@@ -261,9 +261,7 @@
6         for cache_alias in settings.CACHES:
7             cache = get_cache(cache_alias)
8             if isinstance(cache, BaseDatabaseCache):
9-                from django.db import router
10-                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
11-                    call_command('createcachetable', cache._table, database=self.connection.alias)
12+                call_command('createcachetable', cache._table, database=self.connection.alias)
13 
14         # Get a cursor (even though we don't need one yet). This has
15         # the side effect of initializing the test database.
16Index: django/core/management/commands/createcachetable.py
17===================================================================
18--- django/core/management/commands/createcachetable.py (revision 16285)
19+++ django/core/management/commands/createcachetable.py (working copy)
20@@ -1,7 +1,8 @@
21 from optparse import make_option
22 
23 from django.core.management.base import LabelCommand
24-from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
25+from django.core.cache.backends.db import BaseDatabaseCache
26+from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
27 
28 class Command(LabelCommand):
29     help = "Creates the table needed to use the SQL cache backend."
30@@ -18,8 +19,11 @@
31     requires_model_validation = False
32 
33     def handle_label(self, tablename, **options):
34-        alias = options.get('database', DEFAULT_DB_ALIAS)
35-        connection = connections[alias]
36+        db = options.get('database', DEFAULT_DB_ALIAS)
37+        cache = BaseDatabaseCache(tablename, {})
38+        if not router.allow_syncdb(db, cache.cache_model_class):
39+            return
40+        connection = connections[db]
41         fields = (
42             # "key" is a reserved word in MySQL, so use "cache_key" instead.
43             models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
44@@ -50,4 +54,4 @@
45         curs.execute("\n".join(full_statement))
46         for statement in index_output:
47             curs.execute(statement)
48-        transaction.commit_unless_managed(using=alias)
49+        transaction.commit_unless_managed(using=db)
50Index: django/contrib/gis/db/backends/spatialite/creation.py
51===================================================================
52--- django/contrib/gis/db/backends/spatialite/creation.py       (revision 16285)
53+++ django/contrib/gis/db/backends/spatialite/creation.py       (working copy)
54@@ -33,9 +33,7 @@
55         for cache_alias in settings.CACHES:
56             cache = get_cache(cache_alias)
57             if isinstance(cache, BaseDatabaseCache):
58-                from django.db import router
59-                if router.allow_syncdb(self.connection.alias, cache.cache_model_class):
60-                    call_command('createcachetable', cache._table, database=self.connection.alias)
61+                call_command('createcachetable', cache._table, database=self.connection.alias)
62         # Get a cursor (even though we don't need one yet). This has
63         # the side effect of initializing the test database.
64         cursor = self.connection.cursor()
65Index: tests/regressiontests/cache/tests.py
66===================================================================
67--- tests/regressiontests/cache/tests.py        (revision 16285)
68+++ tests/regressiontests/cache/tests.py        (working copy)
69@@ -13,10 +13,11 @@
70 from django.core import management
71 from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
72 from django.core.cache.backends.base import CacheKeyWarning
73+from django.db import connections, router, DEFAULT_DB_ALIAS
74 from django.http import HttpResponse, HttpRequest, QueryDict
75 from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware
76 from django.test import RequestFactory
77-from django.test.utils import get_warnings_state, restore_warnings_state
78+from django.test.utils import get_warnings_state, restore_warnings_state, override_settings
79 from django.utils import translation
80 from django.utils import unittest
81 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key
82@@ -757,6 +758,41 @@
83         self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
84         self.perform_cull_test(50, 18)
85 
86+class DBCacheRouter(object):
87+    """A router that puts the cache table on the 'other' database."""
88+
89+    def db_for_read(self, model, **hints):
90+        if model._meta.app_label == 'django_cache':
91+            return 'other'
92+
93+    def db_for_write(self, model, **hints):
94+        if model._meta.app_label == 'django_cache':
95+            return 'other'
96+
97+    def allow_syncdb(self, db, model):
98+        if model._meta.app_label == 'django_cache':
99+            return db == 'other'
100+
101+class CreateCacheTableForDBCacheTests(unittest.TestCase):
102+
103+    @override_settings(DEBUG=True)
104+    def test_createcachetable_observes_database_router(self):
105+        old_routers = router.routers
106+        try:
107+            router.routers = [DBCacheRouter()]
108+            # cache table should not be created on 'default'
109+            management.call_command('createcachetable', 'cache_table',
110+                                    database='default',
111+                                    verbosity=0, interactive=False)
112+            self.assertEqual(len(connections['default'].queries), 0)
113+            # cache table should be created on 'other'
114+            management.call_command('createcachetable', 'cache_table',
115+                                    database='other',
116+                                    verbosity=0, interactive=False)
117+            self.assertNotEqual(len(connections['other'].queries), 0)
118+        finally:
119+            router.routers = old_routers
120+
121 class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
122     backend_name = 'django.core.cache.backends.locmem.LocMemCache'
123