Skip to content

Commit

Permalink
Remove unnecessary elidable handling of operations.
Browse files Browse the repository at this point in the history
As long as operations are reduceable to standard django.db.migrations
operations they should not require explicit elidable marking.
  • Loading branch information
charettes committed Mar 5, 2021
1 parent f24eb71 commit 8ad1fe0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 47 deletions.
11 changes: 1 addition & 10 deletions syzygy/autodetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ def _generate_added_field(self, app_label, model_name, field_name):
post_add_field = PostAddField(
model_name=model_name, name=field_name, field=add_field.field
)
# Operation cannot be elided for the duration of the migration
# generation.
post_add_field.elidable = False
self.add_operation(
app_label,
post_add_field,
Expand Down Expand Up @@ -121,13 +118,7 @@ def _generate_removed_field(self, app_label, model_name, field_name):
pre_remove_field = PreRemoveField(
model_name=model_name, name=field_name, field=field
)
# Operation cannot be elided for the duration of the migration
# generation.
pre_remove_field.elidable = False
self.add_operation(
app_label,
PreRemoveField(model_name=model_name, name=field_name, field=field),
)
self.add_operation(app_label, pre_remove_field)
stage = Stage()
self.add_operation(
self.STAGE_SPLIT,
Expand Down
3 changes: 0 additions & 3 deletions syzygy/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ class PreRemoveField(migrations.AlterField):
NULL'able if it's not already.
"""

elidable = True

def state_forwards(self, app_label, state):
pass

Expand Down Expand Up @@ -182,7 +180,6 @@ class PostAddField(migrations.AlterField):
Elidable operation that drops a previously preserved database default.
"""

elidable = True
stage = Stage.POST_DEPLOY

def state_forwards(self, app_label, state):
Expand Down
62 changes: 28 additions & 34 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,6 @@ def test_database_backwards(self, preserve_default=True):
def test_database_backwards_discard_default(self):
self.test_database_backwards(preserve_default=False)

def test_elidable(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operations = [
migrations.CreateModel(model_name, [("id", models.AutoField())]),
AddField(model_name, field_name, field),
PostAddField(model_name, field_name, field),
]
self.maxDiff = None
self.assert_optimizes_to(
operations,
[
migrations.CreateModel(
model_name, [("id", models.AutoField()), (field_name, field)]
),
],
)

def test_stage(self):
model_name = "TestModel"
field_name = "foo"
Expand Down Expand Up @@ -210,6 +191,21 @@ def test_deconstruct(self):
),
)

def test_reduce(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operations = [
AddField(model_name, field_name, field),
PostAddField(model_name, field_name, field),
]
self.assert_optimizes_to(
operations,
[
migrations.AddField(model_name, field_name, field),
],
)


class PreRemoveFieldTests(OperationTestCase):
def test_database_forwards_null(self):
Expand Down Expand Up @@ -250,21 +246,6 @@ def test_database_forwards_default(self):
post_model.objects.create()
self.assertEqual(pre_model.objects.get().foo, 42)

def test_elidable(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operations = [
migrations.CreateModel(model_name, [(field_name, field)]),
PreRemoveField(
model_name,
field_name,
field,
),
migrations.RemoveField(model_name, field_name, field),
]
self.assert_optimizes_to(operations, [migrations.CreateModel(model_name, [])])

def test_migration_name_fragment(self):
self.assertEqual(
PreRemoveField(
Expand Down Expand Up @@ -309,3 +290,16 @@ def test_deconstruct(self):
),
)

def test_elidable(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operations = [
PreRemoveField(
model_name,
field_name,
field,
),
migrations.RemoveField(model_name, field_name, field),
]
self.assert_optimizes_to(operations, [operations[-1]])

0 comments on commit 8ad1fe0

Please sign in to comment.