diff options
| author | Tomasz Rybak <tomasz.rybak@gmail.com> | 2013-02-23 17:07:50 +0100 |
|---|---|---|
| committer | Tomasz Rybak <tomasz.rybak@gmail.com> | 2013-02-24 12:38:28 +0100 |
| commit | d7429defe6f8d1dce49976cf49827d18ff6be025 (patch) | |
| tree | c2801f0bdc341c99ba2fead4119fc145120b16c7 /django/db | |
| parent | 6bbf4e57c8b250d09a70d3d840531a42147705e9 (diff) | |
Add sqldropindexes to manage
Change patch from https://code.djangoproject.com/ticket/5568
to work on modern Django.
Add special case for MySQL which has different syntax for DROP INDEX.
Add unit tests for the new functionality.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/creation.py | 46 | ||||
| -rw-r--r-- | django/db/backends/mysql/creation.py | 26 |
2 files changed, 72 insertions, 0 deletions
diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index 89ff1170dc..77c9e6c9e6 100644 --- a/django/db/backends/creation.py +++ b/django/db/backends/creation.py @@ -259,6 +259,52 @@ class BaseDatabaseCreation(object): del references_to_delete[model] return output + def sql_destroy_indexes_for_model(self, model, style): + """ + Returns the DROP INDEX SQL statements for a single model. + """ + if not model._meta.managed or model._meta.proxy or model._meta.swapped: + return [] + output = [] + for f in model._meta.local_fields: + output.extend(self.sql_destroy_indexes_for_field(model, f, style)) + for fs in model._meta.index_together: + fields = [model._meta.get_field_by_name(f)[0] for f in fs] + output.extend(self.sql_destroy_indexes_for_fields(model, fields, style)) + return output + + def sql_destroy_indexes_for_field(self, model, f, style): + """ + Return the DROP INDEX SQL statements for a single model field. + """ + if f.db_index and not f.unique: + return self.sql_destroy_indexes_for_fields(model, [f], style) + else: + return [] + + def sql_destroy_indexes_for_fields(self, model, fields, style): + if len(fields) == 1 and fields[0].db_tablespace: + tablespace_sql = self.connection.ops.tablespace_sql(fields[0].db_tablespace) + elif model._meta.db_tablespace: + tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace) + else: + tablespace_sql = "" + if tablespace_sql: + tablespace_sql = " " + tablespace_sql + + field_names = [] + qn = self.connection.ops.quote_name + for f in fields: + field_names.append(style.SQL_FIELD(qn(f.column))) + + index_name = "%s_%s" % (model._meta.db_table, self._digest([f.name for f in fields])) + + return [ + style.SQL_KEYWORD("DROP INDEX") + " " + + style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + " " + + ";", + ] + def create_test_db(self, verbosity=1, autoclobber=False): """ Creates a test database, prompting the user for confirmation if the diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py index 8d72d11921..01efe8d35b 100644 --- a/django/db/backends/mysql/creation.py +++ b/django/db/backends/mysql/creation.py @@ -41,3 +41,29 @@ class DatabaseCreation(BaseDatabaseCreation): def sql_for_inline_foreign_key_references(self, model, field, known_models, style): "All inline references are pending under MySQL" return [], True + + def sql_destroy_indexes_for_fields(self, model, fields, style): + if len(fields) == 1 and fields[0].db_tablespace: + tablespace_sql = self.connection.ops.tablespace_sql(fields[0].db_tablespace) + elif model._meta.db_tablespace: + tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace) + else: + tablespace_sql = "" + if tablespace_sql: + tablespace_sql = " " + tablespace_sql + + field_names = [] + qn = self.connection.ops.quote_name + for f in fields: + field_names.append(style.SQL_FIELD(qn(f.column))) + + index_name = "%s_%s" % (model._meta.db_table, self._digest([f.name for f in fields])) + + from ..util import truncate_name + + return [ + style.SQL_KEYWORD("DROP INDEX") + " " + + style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + " " + + style.SQL_KEYWORD("ON") + " " + + style.SQL_TABLE(qn(model._meta.db_table)) + ";", + ] |
