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:
parent
3410820460
commit
52442898e7
@ -316,17 +316,17 @@ class BaseDatabaseSchemaEditor(object):
|
|||||||
"table": self.quote_name(model._meta.db_table),
|
"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.
|
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.
|
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):
|
def alter_unique_together(self, model, old_unique_together, new_unique_together):
|
||||||
"""
|
"""
|
||||||
|
@ -768,14 +768,15 @@ class AddIndex(IndexOperation):
|
|||||||
|
|
||||||
def state_forwards(self, app_label, state):
|
def state_forwards(self, app_label, state):
|
||||||
model_state = state.models[app_label, self.model_name_lower]
|
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)
|
model_state.options[self.option_name].append(self.index)
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
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):
|
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):
|
def deconstruct(self):
|
||||||
kwargs = {
|
kwargs = {
|
||||||
@ -810,14 +811,16 @@ class RemoveIndex(IndexOperation):
|
|||||||
model_state.options[self.option_name] = [idx for idx in indexes if idx.name != self.name]
|
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):
|
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]
|
from_model_state = from_state.models[app_label, self.model_name_lower]
|
||||||
index = from_model_state.get_index_by_name(self.name)
|
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):
|
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]
|
to_model_state = to_state.models[app_label, self.model_name_lower]
|
||||||
index = to_model_state.get_index_by_name(self.name)
|
index = to_model_state.get_index_by_name(self.name)
|
||||||
schema_editor.add_index(index)
|
schema_editor.add_index(model, index)
|
||||||
|
|
||||||
def deconstruct(self):
|
def deconstruct(self):
|
||||||
kwargs = {
|
kwargs = {
|
||||||
|
@ -45,23 +45,23 @@ class Index(object):
|
|||||||
self._name = 'D%s' % self._name[1:]
|
self._name = 'D%s' % self._name[1:]
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
def create_sql(self, schema_editor):
|
def create_sql(self, model, schema_editor):
|
||||||
fields = [self.model._meta.get_field(field) for field in self.fields]
|
fields = [model._meta.get_field(field) for field in self.fields]
|
||||||
tablespace_sql = schema_editor._get_index_tablespace_sql(self.model, fields)
|
tablespace_sql = schema_editor._get_index_tablespace_sql(model, fields)
|
||||||
columns = [field.column for field in fields]
|
columns = [field.column for field in fields]
|
||||||
|
|
||||||
quote_name = schema_editor.quote_name
|
quote_name = schema_editor.quote_name
|
||||||
return schema_editor.sql_create_index % {
|
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),
|
'name': quote_name(self.name),
|
||||||
'columns': ', '.join(quote_name(column) for column in columns),
|
'columns': ', '.join(quote_name(column) for column in columns),
|
||||||
'extra': tablespace_sql,
|
'extra': tablespace_sql,
|
||||||
}
|
}
|
||||||
|
|
||||||
def remove_sql(self, schema_editor):
|
def remove_sql(self, model, schema_editor):
|
||||||
quote_name = schema_editor.quote_name
|
quote_name = schema_editor.quote_name
|
||||||
return schema_editor.sql_delete_index % {
|
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),
|
'name': quote_name(self.name),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1455,13 +1455,12 @@ class SchemaTests(TransactionTestCase):
|
|||||||
self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
|
self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
|
||||||
# Add the index
|
# Add the index
|
||||||
index = Index(fields=['name'], name='author_title_idx')
|
index = Index(fields=['name'], name='author_title_idx')
|
||||||
index.model = Author
|
|
||||||
with connection.schema_editor() as editor:
|
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))
|
self.assertIn('name', self.get_indexes(Author._meta.db_table))
|
||||||
# Drop the index
|
# Drop the index
|
||||||
with connection.schema_editor() as editor:
|
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))
|
self.assertNotIn('name', self.get_indexes(Author._meta.db_table))
|
||||||
|
|
||||||
def test_indexes(self):
|
def test_indexes(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user