summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshesh <aksheshdoshi@gmail.com>2016-07-07 12:02:00 +0530
committerTim Graham <timograham@gmail.com>2016-07-07 10:37:39 -0400
commitfc3ac6573524acc58c23ae2c721c61bc9ed8c8d5 (patch)
tree299ccf163caf78a6864aec9929f53d453e924dd7
parent3551fb537f403930763c01c85905baf3ad9b7fd8 (diff)
Refs #26709 -- Checked allow_migrate_model() in Add/RemoveIndex operations.
-rw-r--r--django/db/migrations/operations/models.py20
-rw-r--r--tests/migrations/test_operations.py18
2 files changed, 30 insertions, 8 deletions
diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py
index ef7ecf3184..b929367bce 100644
--- a/django/db/migrations/operations/models.py
+++ b/django/db/migrations/operations/models.py
@@ -772,11 +772,13 @@ class AddIndex(IndexOperation):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
model = to_state.apps.get_model(app_label, self.model_name)
- schema_editor.add_index(model, self.index)
+ if self.allow_migrate_model(schema_editor.connection.alias, model):
+ schema_editor.add_index(model, self.index)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
model = from_state.apps.get_model(app_label, self.model_name)
- schema_editor.remove_index(model, self.index)
+ if self.allow_migrate_model(schema_editor.connection.alias, model):
+ schema_editor.remove_index(model, self.index)
def deconstruct(self):
kwargs = {
@@ -812,15 +814,17 @@ class RemoveIndex(IndexOperation):
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(model, index)
+ if self.allow_migrate_model(schema_editor.connection.alias, model):
+ 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(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(model, index)
+ if self.allow_migrate_model(schema_editor.connection.alias, model):
+ 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(model, index)
def deconstruct(self):
kwargs = {
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 0d2a6ea9bd..743bcd81a4 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -2347,3 +2347,21 @@ class SwappableOperationTests(OperationTestBase):
with connection.schema_editor() as editor:
operation.database_backwards("test_adfligsw", editor, new_state, project_state)
self.assertTableNotExists("test_adfligsw_pony")
+
+ @override_settings(TEST_SWAP_MODEL='migrations.SomeFakeModel')
+ def test_indexes_ignore_swapped(self):
+ """
+ Add/RemoveIndex operations ignore swapped models.
+ """
+ operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
+ project_state, new_state = self.make_test_state('test_adinigsw', operation)
+ with connection.schema_editor() as editor:
+ # No database queries should be run for swapped models
+ operation.database_forwards('test_adinigsw', editor, project_state, new_state)
+ operation.database_backwards('test_adinigsw', editor, new_state, project_state)
+
+ operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
+ project_state, new_state = self.make_test_state("test_rminigsw", operation)
+ with connection.schema_editor() as editor:
+ operation.database_forwards('test_rminigsw', editor, project_state, new_state)
+ operation.database_backwards('test_rminigsw', editor, new_state, project_state)