Ticket #15888: 15888-code-1.diff

File 15888-code-1.diff, 8.1 KB (added by Claude Paroz, 12 years ago)

Tentative code implementation

  • django/contrib/gis/db/backends/spatialite/creation.py

    diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py
    index 27332b9..b85479e 100644
    a b class SpatiaLiteCreation(DatabaseCreation):  
    5454            interactive=False,
    5555            database=self.connection.alias)
    5656
    57         from django.core.cache import get_cache
    58         from django.core.cache.backends.db import BaseDatabaseCache
    59         for cache_alias in settings.CACHES:
    60             cache = get_cache(cache_alias)
    61             if isinstance(cache, BaseDatabaseCache):
    62                 call_command('createcachetable', cache._table, database=self.connection.alias)
     57        call_command('createcachetable', database=self.connection.alias)
    6358
    6459        # Get a cursor (even though we don't need one yet). This has
    6560        # the side effect of initializing the test database.
  • django/core/management/commands/createcachetable.py

    diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py
    index 411042e..d5d8bba 100644
    a b  
    11from optparse import make_option
    22
     3from django.conf import settings
     4from django.core.cache import get_cache
    35from django.core.cache.backends.db import BaseDatabaseCache
    4 from django.core.management.base import LabelCommand, CommandError
     6from django.core.management.base import BaseCommand, CommandError
    57from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
    68from django.db.utils import DatabaseError
    79from django.utils.encoding import force_text
    810
    911
    10 class Command(LabelCommand):
    11     help = "Creates the table needed to use the SQL cache backend."
    12     args = "<tablename>"
    13     label = 'tablename'
     12class Command(BaseCommand):
     13    help = "Creates the tables needed to use the SQL cache backend."
    1414
    15     option_list = LabelCommand.option_list + (
     15    option_list = BaseCommand.option_list + (
    1616        make_option('--database', action='store', dest='database',
    1717            default=DEFAULT_DB_ALIAS, help='Nominates a database onto '
    18                 'which the cache table will be installed. '
     18                'which the cache tables will be installed. '
    1919                'Defaults to the "default" database.'),
    2020    )
    2121
    2222    requires_model_validation = False
    2323
    24     def handle_label(self, tablename, **options):
     24    def handle(self, *tablenames, **options):
    2525        db = options.get('database')
     26        self.verbosity = int(options.get('verbosity'))
     27        if len(tablenames):
     28            # Legacy behaviour, tablename specified as argument
     29            for tablename in tablenames:
     30                self.create_table(db, tablename)
     31        else:
     32            for cache_alias in settings.CACHES:
     33                cache = get_cache(cache_alias)
     34                if isinstance(cache, BaseDatabaseCache):
     35                    self.create_table(db, cache._table)
     36
     37    def create_table(self, database, tablename):
    2638        cache = BaseDatabaseCache(tablename, {})
    27         if not router.allow_syncdb(db, cache.cache_model_class):
     39        if not router.allow_syncdb(database, cache.cache_model_class):
    2840            return
    29         connection = connections[db]
     41        connection = connections[database]
    3042        fields = (
    3143            # "key" is a reserved word in MySQL, so use "cache_key" instead.
    3244            models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
    class Command(LabelCommand):  
    5769        try:
    5870            curs.execute("\n".join(full_statement))
    5971        except DatabaseError as e:
    60             transaction.rollback_unless_managed(using=db)
     72            transaction.rollback_unless_managed(using=database)
    6173            raise CommandError(
    6274                "Cache table '%s' could not be created.\nThe error was: %s." %
    6375                    (tablename, force_text(e)))
    6476        for statement in index_output:
    6577            curs.execute(statement)
    66         transaction.commit_unless_managed(using=db)
     78        transaction.commit_unless_managed(using=database)
     79        if self.verbosity > 1:
     80            self.stdout.write("Cache table '%s' created." % tablename)
  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    index 2baed62..573ab56 100644
    a b class BaseDatabaseCreation(object):  
    303303            interactive=False,
    304304            database=self.connection.alias)
    305305
    306         from django.core.cache import get_cache
    307         from django.core.cache.backends.db import BaseDatabaseCache
    308         for cache_alias in settings.CACHES:
    309             cache = get_cache(cache_alias)
    310             if isinstance(cache, BaseDatabaseCache):
    311                 call_command('createcachetable', cache._table,
    312                              database=self.connection.alias)
     306        call_command('createcachetable', database=self.connection.alias)
    313307
    314308        # Get a cursor (even though we don't need one yet). This has
    315309        # the side effect of initializing the test database.
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index 42d0939..a4999f6 100644
    a b def custom_key_func(key, key_prefix, version):  
    796796    return 'CUSTOM-' + '-'.join([key_prefix, str(version), key])
    797797
    798798
     799@override_settings(
     800    CACHES={
     801        'default': {
     802            'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
     803            'LOCATION': 'test cache table',
     804        },
     805    },
     806)
    799807class DBCacheTests(BaseCacheTests, TransactionTestCase):
    800808    backend_name = 'django.core.cache.backends.db.DatabaseCache'
    801809
    802810    def setUp(self):
    803811        # Spaces are used in the table name to ensure quoting/escaping is working
    804812        self._table_name = 'test cache table'
    805         management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False)
     813        management.call_command('createcachetable', verbosity=0, interactive=False)
    806814        self.cache = get_cache(self.backend_name, LOCATION=self._table_name, OPTIONS={'MAX_ENTRIES': 30})
    807815        self.prefix_cache = get_cache(self.backend_name, LOCATION=self._table_name, KEY_PREFIX='cacheprefix')
    808816        self.v2_cache = get_cache(self.backend_name, LOCATION=self._table_name, VERSION=2)
    class DBCacheTests(BaseCacheTests, TransactionTestCase):  
    831839                "Cache table 'test cache table' could not be created"):
    832840            management.call_command(
    833841               'createcachetable',
    834                 self._table_name,
    835842                verbosity=0,
    836843                interactive=False
    837844            )
    class DBCacheRouter(object):  
    858865            return db == 'other'
    859866
    860867
     868@override_settings(
     869    CACHES={
     870        'default': {
     871            'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
     872            'LOCATION': 'my_cache_table',
     873        },
     874    },
     875)
    861876class CreateCacheTableForDBCacheTests(TestCase):
    862877    multi_db = True
    863878
    class CreateCacheTableForDBCacheTests(TestCase):  
    867882            router.routers = [DBCacheRouter()]
    868883            # cache table should not be created on 'default'
    869884            with self.assertNumQueries(0, using='default'):
    870                 management.call_command('createcachetable', 'cache_table',
     885                management.call_command('createcachetable',
    871886                                        database='default',
    872887                                        verbosity=0, interactive=False)
    873888            # cache table should be created on 'other'
    874889            # one query is used to create the table and another one the index
    875890            with self.assertNumQueries(2, using='other'):
    876                 management.call_command('createcachetable', 'cache_table',
     891                management.call_command('createcachetable',
    877892                                        database='other',
    878893                                        verbosity=0, interactive=False)
    879894        finally:
Back to Top