Ticket #5568: patch-drop_indexes.diff

File patch-drop_indexes.diff, 2.5 KB (added by p, 8 years ago)
  • django/core/management/commands/sqldropindexes.py

     
     1from django.core.management.base import AppCommand
     2
     3class Command(AppCommand):
     4    help = "Prints the DROP INDEX SQL statements for the given app module name(s)."
     5
     6    output_transaction = True
     7
     8    def handle_app(self, app, **options):
     9        from django.core.management.sql import sql_drop_indexes
     10        return '\n'.join(sql_drop_indexes(app, self.style))
  • django/core/management/sql.py

     
    236236        output.extend(sql_indexes_for_model(model, style))
    237237    return output
    238238
     239def sql_drop_indexes(app, style):
     240    "Returns a list of the DROP INDEX SQL statements for all models in the given app."
     241    from django.db import models
     242    output = []
     243    for model in models.get_models(app):
     244        output.extend(sql_drop_indexes_for_model(model, style))
     245    return output
     246
    239247def sql_all(app, style):
    240248    "Returns a list of CREATE TABLE SQL, initial-data inserts, and CREATE INDEX SQL for the given module."
    241249    return sql_create(app, style) + sql_custom(app) + sql_indexes(app, style)
     
    444452            )
    445453    return output
    446454
     455def sql_drop_indexes_for_model(model, style):
     456    "Returns the DROP INDEX SQL statements for a single model"
     457    from django.db import connection
     458    output = []
     459
     460    qn = connection.ops.quote_name
     461    for f in model._meta.fields:
     462        if f.db_index and not ((f.primary_key or f.unique) and connection.features.autoindexes_primary_keys):
     463            tablespace = f.db_tablespace or model._meta.db_tablespace
     464            if tablespace and connection.features.supports_tablespaces:
     465                tablespace_sql = ' ' + connection.ops.tablespace_sql(tablespace)
     466            else:
     467                tablespace_sql = ''
     468            output.append(
     469                style.SQL_KEYWORD('DROP INDEX') + ' ' + \
     470                style.SQL_TABLE(qn('%s_%s' % (model._meta.db_table, f.column))) + ';'
     471            )
     472    return output
     473
    447474def emit_post_sync_signal(created_models, verbosity, interactive):
    448475    from django.db import models
    449476    from django.dispatch import dispatcher
Back to Top