2009-12-22 23:18:51 +08:00
|
|
|
from optparse import make_option
|
|
|
|
|
2013-01-06 06:43:01 +08:00
|
|
|
from django.conf import settings
|
2013-10-19 06:49:24 +08:00
|
|
|
from django.core.cache import caches
|
2011-11-20 03:56:31 +08:00
|
|
|
from django.core.cache.backends.db import BaseDatabaseCache
|
2013-01-06 06:43:01 +08:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2011-11-20 03:56:31 +08:00
|
|
|
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
|
2012-01-09 04:00:30 +08:00
|
|
|
from django.db.utils import DatabaseError
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text
|
2012-06-08 00:08:47 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2013-01-06 06:43:01 +08:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Creates the tables needed to use the SQL cache backend."
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2013-01-06 06:43:01 +08:00
|
|
|
option_list = BaseCommand.option_list + (
|
2009-12-22 23:18:51 +08:00
|
|
|
make_option('--database', action='store', dest='database',
|
|
|
|
default=DEFAULT_DB_ALIAS, help='Nominates a database onto '
|
2013-01-06 06:43:01 +08:00
|
|
|
'which the cache tables will be installed. '
|
2009-12-22 23:18:51 +08:00
|
|
|
'Defaults to the "default" database.'),
|
|
|
|
)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
requires_system_checks = False
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2013-01-06 06:43:01 +08:00
|
|
|
def handle(self, *tablenames, **options):
|
2012-03-11 01:50:59 +08:00
|
|
|
db = options.get('database')
|
2013-01-06 06:43:01 +08:00
|
|
|
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:
|
2013-10-19 06:49:24 +08:00
|
|
|
cache = caches[cache_alias]
|
2013-01-06 06:43:01 +08:00
|
|
|
if isinstance(cache, BaseDatabaseCache):
|
|
|
|
self.create_table(db, cache._table)
|
|
|
|
|
|
|
|
def create_table(self, database, tablename):
|
2011-11-20 03:56:31 +08:00
|
|
|
cache = BaseDatabaseCache(tablename, {})
|
2013-01-06 06:43:01 +08:00
|
|
|
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)
|
2011-11-20 03:56:31 +08:00
|
|
|
return
|
2013-01-06 06:43:01 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
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),
|
|
|
|
models.TextField(name='value'),
|
|
|
|
models.DateTimeField(name='expires', db_index=True),
|
|
|
|
)
|
|
|
|
table_output = []
|
|
|
|
index_output = []
|
2007-08-20 09:03:33 +08:00
|
|
|
qn = connection.ops.quote_name
|
2007-08-16 14:06:55 +08:00
|
|
|
for f in fields:
|
2009-12-22 23:18:51 +08:00
|
|
|
field_output = [qn(f.name), f.db_type(connection=connection)]
|
2013-05-27 10:47:50 +08:00
|
|
|
field_output.append("%sNULL" % ("NOT " if not f.null else ""))
|
2007-08-16 14:06:55 +08:00
|
|
|
if f.primary_key:
|
|
|
|
field_output.append("PRIMARY KEY")
|
2008-06-30 12:46:59 +08:00
|
|
|
elif f.unique:
|
|
|
|
field_output.append("UNIQUE")
|
2007-08-16 14:06:55 +08:00
|
|
|
if f.db_index:
|
2013-05-17 22:33:36 +08:00
|
|
|
unique = "UNIQUE " if f.unique else ""
|
2013-10-18 08:24:41 +08:00
|
|
|
index_output.append("CREATE %sINDEX %s ON %s (%s);" %
|
2010-02-11 20:21:46 +08:00
|
|
|
(unique, qn('%s_%s' % (tablename, f.name)), qn(tablename),
|
2007-08-20 09:03:33 +08:00
|
|
|
qn(f.name)))
|
2007-08-16 14:06:55 +08:00
|
|
|
table_output.append(" ".join(field_output))
|
2007-08-20 09:03:33 +08:00
|
|
|
full_statement = ["CREATE TABLE %s (" % qn(tablename)]
|
2007-08-16 14:06:55 +08:00
|
|
|
for i, line in enumerate(table_output):
|
2013-11-04 02:08:55 +08:00
|
|
|
full_statement.append(' %s%s' % (line, ',' if i < len(table_output) - 1 else ''))
|
2007-08-16 14:06:55 +08:00
|
|
|
full_statement.append(');')
|
2014-03-22 18:02:00 +08:00
|
|
|
|
|
|
|
with transaction.atomic(using=database,
|
|
|
|
savepoint=connection.features.can_rollback_ddl):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as curs:
|
|
|
|
try:
|
|
|
|
curs.execute("\n".join(full_statement))
|
|
|
|
except DatabaseError as e:
|
|
|
|
raise CommandError(
|
|
|
|
"Cache table '%s' could not be created.\nThe error was: %s." %
|
|
|
|
(tablename, force_text(e)))
|
|
|
|
for statement in index_output:
|
|
|
|
curs.execute(statement)
|
2014-03-22 18:02:00 +08:00
|
|
|
|
2013-01-06 06:43:01 +08:00
|
|
|
if self.verbosity > 1:
|
|
|
|
self.stdout.write("Cache table '%s' created." % tablename)
|