From 8ad1fe069b9a0cfe87c28617aed6cde457b4a94e Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 4 Mar 2021 23:08:01 -0500 Subject: [PATCH] Remove unnecessary elidable handling of operations. As long as operations are reduceable to standard django.db.migrations operations they should not require explicit elidable marking. --- syzygy/autodetector.py | 11 +------ syzygy/operations.py | 3 -- tests/test_operations.py | 62 ++++++++++++++++++---------------------- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/syzygy/autodetector.py b/syzygy/autodetector.py index 0a78f69..2c5d36f 100644 --- a/syzygy/autodetector.py +++ b/syzygy/autodetector.py @@ -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, @@ -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, diff --git a/syzygy/operations.py b/syzygy/operations.py index 98f9c5f..3a40aa6 100644 --- a/syzygy/operations.py +++ b/syzygy/operations.py @@ -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 @@ -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): diff --git a/tests/test_operations.py b/tests/test_operations.py index 9ea8d64..cef6248 100644 --- a/tests/test_operations.py +++ b/tests/test_operations.py @@ -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" @@ -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): @@ -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( @@ -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]])