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.
This commit is contained in:
Akshesh 2016-07-06 22:27:17 +05:30 committed by Tim Graham
parent 3410820460
commit 52442898e7
4 changed files with 20 additions and 18 deletions

View File

@ -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):
"""

View File

@ -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 = {

View File

@ -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),
}

View File

@ -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):