From 0025dd5eb42834af3c6566d5ef1dd1032a49f6c8 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 16 Feb 2017 12:50:43 -0500 Subject: [PATCH] Allowed RemoveField operations to be optimized through. --- django/db/migrations/operations/fields.py | 9 ++--- tests/migrations/test_autodetector.py | 40 +++++++++-------------- tests/migrations/test_operations.py | 9 +---- 3 files changed, 19 insertions(+), 39 deletions(-) diff --git a/django/db/migrations/operations/fields.py b/django/db/migrations/operations/fields.py index 9faeb81318..601f9ac43a 100644 --- a/django/db/migrations/operations/fields.py +++ b/django/db/migrations/operations/fields.py @@ -34,9 +34,7 @@ class FieldOperation(Operation): return True if self.field: return field_references_model(self.field, ModelTuple(app_label, name_lower)) - # Refuse the temptation to guess. This operation could be performed on - # a field referencing the specified model. - return True + return False def references_field(self, model_name, name, app_label=None): model_name_lower = model_name.lower() @@ -60,10 +58,7 @@ class FieldOperation(Operation): (getattr(remote_field, 'through_fields', None) is None or name in remote_field.through_fields)): return True - return False - # Refuse the temptation to guess. This operation could be performed on - # a field referencing the specified model. - return True + return False def reduce(self, operation, in_between, app_label=None): return ( diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 34c9dac989..e3ce3a129a 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -1925,11 +1925,9 @@ class AutodetectorTests(TestCase): # Remove both the through model and ManyToMany # Right number/type of migrations? self.assertNumberMigrations(changes, "otherapp", 1) - self.assertOperationTypes(changes, "otherapp", 0, ["RemoveField", "RemoveField", "RemoveField", "DeleteModel"]) - self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="author", model_name='attribution') - self.assertOperationAttributes(changes, 'otherapp', 0, 1, name="book", model_name='attribution') - self.assertOperationAttributes(changes, 'otherapp', 0, 2, name="authors", model_name='book') - self.assertOperationAttributes(changes, 'otherapp', 0, 3, name='Attribution') + self.assertOperationTypes(changes, 'otherapp', 0, ['RemoveField', 'DeleteModel']) + self.assertOperationAttributes(changes, 'otherapp', 0, 0, name='authors', model_name='book') + self.assertOperationAttributes(changes, 'otherapp', 0, 1, name='Attribution') def test_many_to_many_removed_before_through_model_2(self): """ @@ -1944,14 +1942,10 @@ class AutodetectorTests(TestCase): # Remove both the through model and ManyToMany # Right number/type of migrations? self.assertNumberMigrations(changes, "otherapp", 1) - self.assertOperationTypes(changes, "otherapp", 0, [ - "RemoveField", "RemoveField", "RemoveField", "DeleteModel", "DeleteModel" - ]) - self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="author", model_name='attribution') - self.assertOperationAttributes(changes, 'otherapp', 0, 1, name="book", model_name='attribution') - self.assertOperationAttributes(changes, 'otherapp', 0, 2, name="authors", model_name='book') - self.assertOperationAttributes(changes, 'otherapp', 0, 3, name='Attribution') - self.assertOperationAttributes(changes, 'otherapp', 0, 4, name='Book') + self.assertOperationTypes(changes, 'otherapp', 0, ['RemoveField', 'DeleteModel', 'DeleteModel']) + self.assertOperationAttributes(changes, 'otherapp', 0, 0, name='authors', model_name='book') + self.assertOperationAttributes(changes, 'otherapp', 0, 1, name='Attribution') + self.assertOperationAttributes(changes, 'otherapp', 0, 2, name='Book') def test_m2m_w_through_multistep_remove(self): """ @@ -1964,13 +1958,12 @@ class AutodetectorTests(TestCase): # Right number/type of migrations? self.assertNumberMigrations(changes, "testapp", 1) self.assertOperationTypes(changes, "testapp", 0, [ - "RemoveField", "RemoveField", "RemoveField", "DeleteModel", "DeleteModel" + "RemoveField", "RemoveField", "DeleteModel", "DeleteModel" ]) - self.assertOperationAttributes(changes, "testapp", 0, 0, name="publishers", model_name='author') - self.assertOperationAttributes(changes, "testapp", 0, 1, name="author", model_name='contract') - self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher", model_name='contract') - self.assertOperationAttributes(changes, "testapp", 0, 3, name="Author") - self.assertOperationAttributes(changes, "testapp", 0, 4, name="Contract") + self.assertOperationAttributes(changes, "testapp", 0, 0, name="author", model_name='contract') + self.assertOperationAttributes(changes, "testapp", 0, 1, name="publisher", model_name='contract') + self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author") + self.assertOperationAttributes(changes, "testapp", 0, 3, name="Contract") def test_concrete_field_changed_to_many_to_many(self): """ @@ -2007,11 +2000,10 @@ class AutodetectorTests(TestCase): changes = self.get_changes([self.author_with_publisher, self.publisher_with_author], []) # Right number/type of migrations? self.assertNumberMigrations(changes, "testapp", 1) - self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "RemoveField", "DeleteModel", "DeleteModel"]) - self.assertOperationAttributes(changes, "testapp", 0, 0, name="publisher", model_name='author') - self.assertOperationAttributes(changes, "testapp", 0, 1, name="author", model_name='publisher') - self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author") - self.assertOperationAttributes(changes, "testapp", 0, 3, name="Publisher") + self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "DeleteModel", "DeleteModel"]) + self.assertOperationAttributes(changes, "testapp", 0, 0, name="author", model_name='publisher') + self.assertOperationAttributes(changes, "testapp", 0, 1, name="Author") + self.assertOperationAttributes(changes, "testapp", 0, 2, name="Publisher") def test_alter_model_options(self): """Changing a model's options should make a change.""" diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 5dfe4acb4d..142330127b 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -2758,10 +2758,7 @@ class TestCreateModel(SimpleTestCase): class FieldOperationTests(SimpleTestCase): def test_references_model(self): - operation = FieldOperation('MoDel', 'field') - # When missing a field declaration always assume it's referencing. - self.assertIs(operation.references_model('Whatever'), True) - operation.field = models.ForeignKey('Other', models.CASCADE) + operation = FieldOperation('MoDel', 'field', models.ForeignKey('Other', models.CASCADE)) # Model name match. self.assertIs(operation.references_model('mOdEl'), True) # Referenced field. @@ -2769,10 +2766,6 @@ class FieldOperationTests(SimpleTestCase): # Doesn't reference. self.assertIs(operation.references_model('Whatever'), False) - def test_references_field_missing_field(self): - operation = FieldOperation('MoDel', 'field') - self.assertIs(operation.references_field('Whatever', 'missing'), True) - def test_references_field_by_name(self): operation = FieldOperation('MoDel', 'field', models.BooleanField(default=False)) self.assertIs(operation.references_field('model', 'field'), True)