Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
akki authored and timgraham committed Jul 7, 2016
1 parent 3410820 commit 5244289
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions django/db/backends/base/schema.py
Expand Up @@ -316,17 +316,17 @@ def delete_model(self, model):
"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):
"""
Expand Down
13 changes: 8 additions & 5 deletions django/db/migrations/operations/models.py
Expand Up @@ -768,14 +768,15 @@ def __init__(self, model_name, index):

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 = {
Expand Down Expand Up @@ -810,14 +811,16 @@ def state_forwards(self, app_label, state):
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 = {
Expand Down
12 changes: 6 additions & 6 deletions django/db/models/indexes.py
Expand Up @@ -45,23 +45,23 @@ def check_name(self):
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),
}

Expand Down
5 changes: 2 additions & 3 deletions tests/schema/tests.py
Expand Up @@ -1455,13 +1455,12 @@ def test_add_remove_index(self):
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):
Expand Down

0 comments on commit 5244289

Please sign in to comment.