From 52442898e747e9d76bf6349938fa3dada6f0e887 Mon Sep 17 00:00:00 2001 From: Akshesh Date: Wed, 6 Jul 2016 22:27:17 +0530 Subject: [PATCH] Refs #26709 -- Added 'model' argument to SchemaEditor.add/remove_index() This removes the dependency of the Index class on its model attribute when a name is passed to it. Thanks to Markush for discussions. --- django/db/backends/base/schema.py | 8 ++++---- django/db/migrations/operations/models.py | 13 ++++++++----- django/db/models/indexes.py | 12 ++++++------ tests/schema/tests.py | 5 ++--- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 44e598f92e..b11892dbeb 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -316,17 +316,17 @@ class BaseDatabaseSchemaEditor(object): "table": self.quote_name(model._meta.db_table), }) - def add_index(self, index): + def add_index(self, model, index): """ Add an index on a model. """ - self.execute(index.create_sql(self)) + self.execute(index.create_sql(model, self)) - def remove_index(self, index): + def remove_index(self, model, index): """ Remove an index from a model. """ - self.execute(index.remove_sql(self)) + self.execute(index.remove_sql(model, self)) def alter_unique_together(self, model, old_unique_together, new_unique_together): """ diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 88fccca115..ef7ecf3184 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -768,14 +768,15 @@ class AddIndex(IndexOperation): def state_forwards(self, app_label, state): model_state = state.models[app_label, self.model_name_lower] - self.index.model = state.apps.get_model(app_label, self.model_name) model_state.options[self.option_name].append(self.index) def database_forwards(self, app_label, schema_editor, from_state, to_state): - schema_editor.add_index(self.index) + model = to_state.apps.get_model(app_label, self.model_name) + schema_editor.add_index(model, self.index) def database_backwards(self, app_label, schema_editor, from_state, to_state): - schema_editor.remove_index(self.index) + model = from_state.apps.get_model(app_label, self.model_name) + schema_editor.remove_index(model, self.index) def deconstruct(self): kwargs = { @@ -810,14 +811,16 @@ class RemoveIndex(IndexOperation): model_state.options[self.option_name] = [idx for idx in indexes if idx.name != self.name] def database_forwards(self, app_label, schema_editor, from_state, to_state): + model = from_state.apps.get_model(app_label, self.model_name) from_model_state = from_state.models[app_label, self.model_name_lower] index = from_model_state.get_index_by_name(self.name) - schema_editor.remove_index(index) + schema_editor.remove_index(model, index) def database_backwards(self, app_label, schema_editor, from_state, to_state): + model = to_state.apps.get_model(app_label, self.model_name) to_model_state = to_state.models[app_label, self.model_name_lower] index = to_model_state.get_index_by_name(self.name) - schema_editor.add_index(index) + schema_editor.add_index(model, index) def deconstruct(self): kwargs = { diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py index 15d618156e..86edc5d130 100644 --- a/django/db/models/indexes.py +++ b/django/db/models/indexes.py @@ -45,23 +45,23 @@ class Index(object): self._name = 'D%s' % self._name[1:] return errors - def create_sql(self, schema_editor): - fields = [self.model._meta.get_field(field) for field in self.fields] - tablespace_sql = schema_editor._get_index_tablespace_sql(self.model, fields) + def create_sql(self, model, schema_editor): + fields = [model._meta.get_field(field) for field in self.fields] + tablespace_sql = schema_editor._get_index_tablespace_sql(model, fields) columns = [field.column for field in fields] quote_name = schema_editor.quote_name return schema_editor.sql_create_index % { - 'table': quote_name(self.model._meta.db_table), + 'table': quote_name(model._meta.db_table), 'name': quote_name(self.name), 'columns': ', '.join(quote_name(column) for column in columns), 'extra': tablespace_sql, } - def remove_sql(self, schema_editor): + def remove_sql(self, model, schema_editor): quote_name = schema_editor.quote_name return schema_editor.sql_delete_index % { - 'table': quote_name(self.model._meta.db_table), + 'table': quote_name(model._meta.db_table), 'name': quote_name(self.name), } diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 789b8b9ff2..a721abfe66 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -1455,13 +1455,12 @@ class SchemaTests(TransactionTestCase): self.assertNotIn('title', self.get_indexes(Author._meta.db_table)) # Add the index index = Index(fields=['name'], name='author_title_idx') - index.model = Author with connection.schema_editor() as editor: - editor.add_index(index) + editor.add_index(Author, index) self.assertIn('name', self.get_indexes(Author._meta.db_table)) # Drop the index with connection.schema_editor() as editor: - editor.remove_index(index) + editor.remove_index(Author, index) self.assertNotIn('name', self.get_indexes(Author._meta.db_table)) def test_indexes(self):