Ticket #15255: 15255.2.diff

File 15255.2.diff, 5.7 KB (added by Aymeric Augustin, 13 years ago)
  • tests/regressiontests/cache/tests.py

     
    1313from django.core import management
    1414from django.core.cache import get_cache, DEFAULT_CACHE_ALIAS
    1515from django.core.cache.backends.base import CacheKeyWarning
     16from django.db import connections, router, DEFAULT_DB_ALIAS
    1617from django.http import HttpResponse, HttpRequest, QueryDict
    1718from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware, CacheMiddleware
    1819from django.test import RequestFactory
     
    757758        self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
    758759        self.perform_cull_test(50, 18)
    759760
     761class 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
     776class CreateCacheTableForDBCacheTests(unittest.TestCase):
     777
     778    def setUp(self):
     779        self.old_routers, router.routers = router.routers, [DBCacheRouter()]
     780        self.old_debug, settings.DEBUG = settings.DEBUG, True
     781
     782    def tearDown(self):
     783        router.routers = self.old_routers
     784        settings.DEBUG = self.old_debug
     785
     786    def test_createcachetable_observes_database_router(self):
     787        # cache table should not be created on 'default'
     788        management.call_command('createcachetable', 'cache_table',
     789                                database='default',
     790                                verbosity=0, interactive=False)
     791        self.assertEqual(len(connections['default'].queries), 0)
     792        # cache table should be created on 'other'
     793        management.call_command('createcachetable', 'cache_table',
     794                                database='other',
     795                                verbosity=0, interactive=False)
     796        self.assertNotEqual(len(connections['other'].queries), 0)
     797
    760798class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
    761799    backend_name = 'django.core.cache.backends.locmem.LocMemCache'
    762800
  • django/db/backends/creation.py

     
    261261        for cache_alias in settings.CACHES:
    262262            cache = get_cache(cache_alias)
    263263            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)
    267265
    268266        # Get a cursor (even though we don't need one yet). This has
    269267        # the side effect of initializing the test database.
  • django/core/management/commands/createcachetable.py

     
    11from optparse import make_option
    22
    33from django.core.management.base import LabelCommand
    4 from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
     4from django.core.cache.backends.db import BaseDatabaseCache
     5from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
    56
    67class Command(LabelCommand):
    78    help = "Creates the table needed to use the SQL cache backend."
     
    1819    requires_model_validation = False
    1920
    2021    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]
    2327        fields = (
    2428            # "key" is a reserved word in MySQL, so use "cache_key" instead.
    2529            models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
     
    5054        curs.execute("\n".join(full_statement))
    5155        for statement in index_output:
    5256            curs.execute(statement)
    53         transaction.commit_unless_managed(using=alias)
     57        transaction.commit_unless_managed(using=db)
  • django/contrib/gis/db/backends/spatialite/creation.py

     
    3333        for cache_alias in settings.CACHES:
    3434            cache = get_cache(cache_alias)
    3535            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)
    3937        # Get a cursor (even though we don't need one yet). This has
    4038        # the side effect of initializing the test database.
    4139        cursor = self.connection.cursor()
Back to Top