Commit 1e8eadc9 authored by Claude Paroz's avatar Claude Paroz
Browse files

Fixed #15888 -- Made tablename argument of createcachetable optional

Thanks Aymeric Augustin for the report and the documentation and
Tim Graham for the review.
parent b600bb7e
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
import os

from django.conf import settings
from django.core.cache import get_cache
from django.core.cache.backends.db import BaseDatabaseCache
from django.core.exceptions import ImproperlyConfigured
from django.db.backends.sqlite3.creation import DatabaseCreation

@@ -55,10 +53,7 @@ class SpatiaLiteCreation(DatabaseCreation):
            interactive=False,
            database=self.connection.alias)

        for cache_alias in settings.CACHES:
            cache = get_cache(cache_alias)
            if isinstance(cache, BaseDatabaseCache):
                call_command('createcachetable', cache._table, database=self.connection.alias)
        call_command('createcachetable', database=self.connection.alias)

        # Get a cursor (even though we don't need one yet). This has
        # the side effect of initializing the test database.
+30 −10
Original line number Diff line number Diff line
from optparse import make_option

from django.conf import settings
from django.core.cache import get_cache
from django.core.cache.backends.db import BaseDatabaseCache
from django.core.management.base import LabelCommand, CommandError
from django.core.management.base import BaseCommand, CommandError
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
from django.db.utils import DatabaseError
from django.utils.encoding import force_text


class Command(LabelCommand):
    help = "Creates the table needed to use the SQL cache backend."
    args = "<tablename>"
    label = 'tablename'
class Command(BaseCommand):
    help = "Creates the tables needed to use the SQL cache backend."

    option_list = LabelCommand.option_list + (
    option_list = BaseCommand.option_list + (
        make_option('--database', action='store', dest='database',
            default=DEFAULT_DB_ALIAS, help='Nominates a database onto '
                'which the cache table will be installed. '
                'which the cache tables will be installed. '
                'Defaults to the "default" database.'),
    )

    requires_model_validation = False

    def handle_label(self, tablename, **options):
    def handle(self, *tablenames, **options):
        db = options.get('database')
        self.verbosity = int(options.get('verbosity'))
        if len(tablenames):
            # Legacy behavior, tablename specified as argument
            for tablename in tablenames:
                self.create_table(db, tablename)
        else:
            for cache_alias in settings.CACHES:
                cache = get_cache(cache_alias)
                if isinstance(cache, BaseDatabaseCache):
                    self.create_table(db, cache._table)

    def create_table(self, database, tablename):
        cache = BaseDatabaseCache(tablename, {})
        if not router.allow_migrate(db, cache.cache_model_class):
        if not router.allow_migrate(database, cache.cache_model_class):
            return
        connection = connections[database]

        if tablename in connection.introspection.table_names():
            if self.verbosity > 0:
                self.stdout.write("Cache table '%s' already exists." % tablename)
            return
        connection = connections[db]

        fields = (
            # "key" is a reserved word in MySQL, so use "cache_key" instead.
            models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
@@ -63,3 +81,5 @@ class Command(LabelCommand):
                        (tablename, force_text(e)))
            for statement in index_output:
                curs.execute(statement)
        if self.verbosity > 1:
            self.stdout.write("Cache table '%s' created." % tablename)
+1 −7
Original line number Diff line number Diff line
@@ -356,13 +356,7 @@ class BaseDatabaseCreation(object):
            interactive=False,
            database=self.connection.alias)

        from django.core.cache import get_cache
        from django.core.cache.backends.db import BaseDatabaseCache
        for cache_alias in settings.CACHES:
            cache = get_cache(cache_alias)
            if isinstance(cache, BaseDatabaseCache):
                call_command('createcachetable', cache._table,
                             database=self.connection.alias)
        call_command('createcachetable', database=self.connection.alias)

        # Get a cursor (even though we don't need one yet). This has
        # the side effect of initializing the test database.
+9 −2
Original line number Diff line number Diff line
@@ -131,12 +131,19 @@ createcachetable

.. django-admin:: createcachetable

Creates a cache table named ``tablename`` for use with the database cache
backend. See :doc:`/topics/cache` for more information.
Creates the cache tables for use with the database cache backend. See
:doc:`/topics/cache` for more information.

The :djadminopt:`--database` option can be used to specify the database
onto which the cachetable will be installed.

.. versionchanged:: 1.7

    It is no longer necessary to provide the cache table name or the
    :djadminopt:`--database` option. Django takes this information from your
    settings file. If you have configured multiple caches or multiple databases,
    all cache tables are created.

dbshell
-------

+5 −0
Original line number Diff line number Diff line
@@ -305,6 +305,11 @@ Management Commands
  ``use_natural_primary_keys`` arguments for ``serializers.serialize()``, allow
  the use of natural primary keys when serializing.

* It is no longer necessary to provide the cache table name or the
  :djadminopt:`--database` option for the :djadmin:`createcachetable` command.
  Django takes this information from your settings file. If you have configured
  multiple caches or multiple databases, all cache tables are created.

Models
^^^^^^

Loading