web/lib/django/core/management/commands/createcachetable.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
       
     1 from optparse import make_option
       
     2 
     1 from django.core.management.base import LabelCommand
     3 from django.core.management.base import LabelCommand
       
     4 from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
     2 
     5 
     3 class Command(LabelCommand):
     6 class Command(LabelCommand):
     4     help = "Creates the table needed to use the SQL cache backend."
     7     help = "Creates the table needed to use the SQL cache backend."
     5     args = "<tablename>"
     8     args = "<tablename>"
     6     label = 'tablename'
     9     label = 'tablename'
     7 
    10 
       
    11     option_list = LabelCommand.option_list + (
       
    12         make_option('--database', action='store', dest='database',
       
    13             default=DEFAULT_DB_ALIAS, help='Nominates a database onto '
       
    14                 'which the cache table will be installed. '
       
    15                 'Defaults to the "default" database.'),
       
    16     )
       
    17 
     8     requires_model_validation = False
    18     requires_model_validation = False
     9 
    19 
    10     def handle_label(self, tablename, **options):
    20     def handle_label(self, tablename, **options):
    11         from django.db import connection, transaction, models
    21         alias = options.get('database', DEFAULT_DB_ALIAS)
       
    22         connection = connections[alias]
    12         fields = (
    23         fields = (
    13             # "key" is a reserved word in MySQL, so use "cache_key" instead.
    24             # "key" is a reserved word in MySQL, so use "cache_key" instead.
    14             models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
    25             models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
    15             models.TextField(name='value'),
    26             models.TextField(name='value'),
    16             models.DateTimeField(name='expires', db_index=True),
    27             models.DateTimeField(name='expires', db_index=True),
    17         )
    28         )
    18         table_output = []
    29         table_output = []
    19         index_output = []
    30         index_output = []
    20         qn = connection.ops.quote_name
    31         qn = connection.ops.quote_name
    21         for f in fields:
    32         for f in fields:
    22             field_output = [qn(f.name), f.db_type()]
    33             field_output = [qn(f.name), f.db_type(connection=connection)]
    23             field_output.append("%sNULL" % (not f.null and "NOT " or ""))
    34             field_output.append("%sNULL" % (not f.null and "NOT " or ""))
    24             if f.primary_key:
    35             if f.primary_key:
    25                 field_output.append("PRIMARY KEY")
    36                 field_output.append("PRIMARY KEY")
    26             elif f.unique:
    37             elif f.unique:
    27                 field_output.append("UNIQUE")
    38                 field_output.append("UNIQUE")
    28             if f.db_index:
    39             if f.db_index:
    29                 unique = f.unique and "UNIQUE " or ""
    40                 unique = f.unique and "UNIQUE " or ""
    30                 index_output.append("CREATE %sINDEX %s_%s ON %s (%s);" % \
    41                 index_output.append("CREATE %sINDEX %s ON %s (%s);" % \
    31                     (unique, tablename, f.name, qn(tablename),
    42                     (unique, qn('%s_%s' % (tablename, f.name)), qn(tablename),
    32                     qn(f.name)))
    43                     qn(f.name)))
    33             table_output.append(" ".join(field_output))
    44             table_output.append(" ".join(field_output))
    34         full_statement = ["CREATE TABLE %s (" % qn(tablename)]
    45         full_statement = ["CREATE TABLE %s (" % qn(tablename)]
    35         for i, line in enumerate(table_output):
    46         for i, line in enumerate(table_output):
    36             full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or ''))
    47             full_statement.append('    %s%s' % (line, i < len(table_output)-1 and ',' or ''))
    37         full_statement.append(');')
    48         full_statement.append(');')
    38         curs = connection.cursor()
    49         curs = connection.cursor()
    39         curs.execute("\n".join(full_statement))
    50         curs.execute("\n".join(full_statement))
    40         for statement in index_output:
    51         for statement in index_output:
    41             curs.execute(statement)
    52             curs.execute(statement)
    42         transaction.commit_unless_managed()
    53         transaction.commit_unless_managed(using=alias)