Ticket #31383: 31383.diff

File 31383.diff, 3.4 KB (added by Tim Graham, 5 years ago)

draft patch

  • django/core/management/commands/createcachetable.py

    diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py
    index 7e4edfc24f..22a36255f5 100644
    a b class Command(BaseCommand):  
    5454                self.stdout.write("Cache table '%s' already exists." % tablename)
    5555            return
    5656
    57         fields = (
     57        class CacheTable(models.Model):
    5858            # "key" is a reserved word in MySQL, so use "cache_key" instead.
    59             models.CharField(name='cache_key', max_length=255, unique=True, primary_key=True),
    60             models.TextField(name='value'),
    61             models.DateTimeField(name='expires', db_index=True),
    62         )
    63         table_output = []
    64         index_output = []
    65         qn = connection.ops.quote_name
    66         for f in fields:
    67             field_output = [
    68                 qn(f.name),
    69                 f.db_type(connection=connection),
    70                 '%sNULL' % ('NOT ' if not f.null else ''),
    71             ]
    72             if f.primary_key:
    73                 field_output.append("PRIMARY KEY")
    74             elif f.unique:
    75                 field_output.append("UNIQUE")
    76             if f.db_index:
    77                 unique = "UNIQUE " if f.unique else ""
    78                 index_output.append(
    79                     "CREATE %sINDEX %s ON %s (%s);" %
    80                     (unique, qn('%s_%s' % (tablename, f.name)), qn(tablename), qn(f.name))
    81                 )
    82             table_output.append(" ".join(field_output))
    83         full_statement = ["CREATE TABLE %s (" % qn(tablename)]
    84         for i, line in enumerate(table_output):
    85             full_statement.append('    %s%s' % (line, ',' if i < len(table_output) - 1 else ''))
    86         full_statement.append(');')
     59            cache_key = models.CharField(max_length=255, unique=True, primary_key=True)
     60            value = models.TextField()
     61            expires = models.DateTimeField(db_index=True)
    8762
    88         full_statement = "\n".join(full_statement)
     63            class Meta:
     64                app_label = '<dummy>'
     65                db_table = tablename
    8966
    9067        if dry_run:
    91             self.stdout.write(full_statement)
    92             for statement in index_output:
    93                 self.stdout.write(statement)
     68            with connection.schema_editor(collect_sql=True) as editor:
     69                editor.create_model(CacheTable)
     70                for statement in editor.collected_sql + [str(s) for s in editor.deferred_sql]:
     71                    self.stdout.write(statement)
    9472            return
    9573
    9674        with transaction.atomic(using=database, savepoint=connection.features.can_rollback_ddl):
    97             with connection.cursor() as curs:
     75            with connection.schema_editor() as editor:
    9876                try:
    99                     curs.execute(full_statement)
     77                    editor.create_model(CacheTable)
    10078                except DatabaseError as e:
    10179                    raise CommandError(
    10280                        "Cache table '%s' could not be created.\nThe error was: %s." %
    103                         (tablename, e))
    104                 for statement in index_output:
    105                     curs.execute(statement)
     81                        (tablename, e)
     82                    )
    10683
    10784        if self.verbosity > 1:
    10885            self.stdout.write("Cache table '%s' created." % tablename)
Back to Top