web/lib/django/core/management/commands/createcachetable.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
--- a/web/lib/django/core/management/commands/createcachetable.py	Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/core/management/commands/createcachetable.py	Tue May 25 02:43:45 2010 +0200
@@ -1,14 +1,25 @@
+from optparse import make_option
+
 from django.core.management.base import LabelCommand
+from django.db import connections, transaction, models, DEFAULT_DB_ALIAS
 
 class Command(LabelCommand):
     help = "Creates the table needed to use the SQL cache backend."
     args = "<tablename>"
     label = 'tablename'
 
+    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.'),
+    )
+
     requires_model_validation = False
 
     def handle_label(self, tablename, **options):
-        from django.db import connection, transaction, models
+        alias = options.get('database', DEFAULT_DB_ALIAS)
+        connection = connections[alias]
         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),
@@ -19,7 +30,7 @@
         index_output = []
         qn = connection.ops.quote_name
         for f in fields:
-            field_output = [qn(f.name), f.db_type()]
+            field_output = [qn(f.name), f.db_type(connection=connection)]
             field_output.append("%sNULL" % (not f.null and "NOT " or ""))
             if f.primary_key:
                 field_output.append("PRIMARY KEY")
@@ -27,8 +38,8 @@
                 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),
+                index_output.append("CREATE %sINDEX %s ON %s (%s);" % \
+                    (unique, qn('%s_%s' % (tablename, f.name)), qn(tablename),
                     qn(f.name)))
             table_output.append(" ".join(field_output))
         full_statement = ["CREATE TABLE %s (" % qn(tablename)]
@@ -39,4 +50,4 @@
         curs.execute("\n".join(full_statement))
         for statement in index_output:
             curs.execute(statement)
-        transaction.commit_unless_managed()
+        transaction.commit_unless_managed(using=alias)