2009-12-22 23:18:51 +08:00
|
|
|
from optparse import make_option
|
|
|
|
|
2011-11-20 03:56:31 +08:00
|
|
|
from django.core.cache.backends.db import BaseDatabaseCache
|
2012-05-28 05:03:21 +08:00
|
|
|
from django.core.management.base import LabelCommand, 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
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
class Command(LabelCommand):
|
2007-08-16 14:06:55 +08:00
|
|
|
help = "Creates the table needed to use the SQL cache backend."
|
2007-09-10 05:57:59 +08:00
|
|
|
args = "<tablename>"
|
2007-08-16 22:34:01 +08:00
|
|
|
label = 'tablename'
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
option_list = LabelCommand.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. '
|
|
|
|
'Defaults to the "default" database.'),
|
|
|
|
)
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
requires_model_validation = False
|
|
|
|
|
2007-08-16 22:34:01 +08:00
|
|
|
def handle_label(self, tablename, **options):
|
2012-03-11 01:50:59 +08:00
|
|
|
db = options.get('database')
|
2011-11-20 03:56:31 +08:00
|
|
|
cache = BaseDatabaseCache(tablename, {})
|
|
|
|
if not router.allow_syncdb(db, cache.cache_model_class):
|
|
|
|
return
|
|
|
|
connection = connections[db]
|
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)]
|
2007-08-16 14:06:55 +08:00
|
|
|
field_output.append("%sNULL" % (not f.null and "NOT " or ""))
|
|
|
|
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:
|
|
|
|
unique = f.unique and "UNIQUE " or ""
|
2010-02-11 20:21:46 +08:00
|
|
|
index_output.append("CREATE %sINDEX %s ON %s (%s);" % \
|
|
|
|
(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):
|
|
|
|
full_statement.append(' %s%s' % (line, i < len(table_output)-1 and ',' or ''))
|
|
|
|
full_statement.append(');')
|
|
|
|
curs = connection.cursor()
|
2012-01-09 04:00:30 +08:00
|
|
|
try:
|
|
|
|
curs.execute("\n".join(full_statement))
|
2012-04-29 00:09:37 +08:00
|
|
|
except DatabaseError as e:
|
2012-05-28 05:03:21 +08:00
|
|
|
transaction.rollback_unless_managed(using=db)
|
|
|
|
raise CommandError(
|
2012-05-19 19:51:54 +08:00
|
|
|
"Cache table '%s' could not be created.\nThe error was: %s." %
|
|
|
|
(tablename, e))
|
2012-05-28 05:03:21 +08:00
|
|
|
for statement in index_output:
|
|
|
|
curs.execute(statement)
|
|
|
|
transaction.commit_unless_managed(using=db)
|