summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshesh <aksheshdoshi@gmail.com>2016-07-06 22:27:17 +0530
committerTim Graham <timograham@gmail.com>2016-07-07 10:06:55 -0400
commit52442898e747e9d76bf6349938fa3dada6f0e887 (patch)
tree0bafb9ebc1212b4be5ace3449ea01ebd4068b462
parent34108204603b39b3e4e58cb46012736a4986e6d8 (diff)
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.
-rw-r--r--django/db/backends/base/schema.py8
-rw-r--r--django/db/migrations/operations/models.py13
-rw-r--r--django/db/models/indexes.py12
-rw-r--r--tests/schema/tests.py5
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):