summaryrefslogtreecommitdiffstats
path: root/webapp/django/core/management/commands/createcachetable.py
diff options
context:
space:
mode:
Diffstat (limited to 'webapp/django/core/management/commands/createcachetable.py')
-rw-r--r--webapp/django/core/management/commands/createcachetable.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/webapp/django/core/management/commands/createcachetable.py b/webapp/django/core/management/commands/createcachetable.py
new file mode 100644
index 0000000000..098bca793f
--- /dev/null
+++ b/webapp/django/core/management/commands/createcachetable.py
@@ -0,0 +1,42 @@
+from django.core.management.base import LabelCommand
+
+class Command(LabelCommand):
+ help = "Creates the table needed to use the SQL cache backend."
+ args = "<tablename>"
+ label = 'tablename'
+
+ requires_model_validation = False
+
+ def handle_label(self, tablename, **options):
+ from django.db import connection, transaction, models
+ 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 = []
+ qn = connection.ops.quote_name
+ for f in fields:
+ field_output = [qn(f.name), f.db_type()]
+ field_output.append("%sNULL" % (not f.null and "NOT " or ""))
+ if f.primary_key:
+ field_output.append("PRIMARY KEY")
+ elif f.unique:
+ field_output.append("UNIQUE")
+ if f.db_index:
+ unique = f.unique and "UNIQUE " or ""
+ index_output.append("CREATE %sINDEX %s_%s ON %s (%s);" % \
+ (unique, tablename, f.name, qn(tablename),
+ qn(f.name)))
+ table_output.append(" ".join(field_output))
+ full_statement = ["CREATE TABLE %s (" % qn(tablename)]
+ 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()
+ curs.execute("\n".join(full_statement))
+ for statement in index_output:
+ curs.execute(statement)
+ transaction.commit_unless_managed()