diff options
author | Akash Kumar Sen <Akash-Kumar-Sen@users.noreply.github.com> | 2023-05-08 23:24:46 +0530 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-05-09 12:45:09 +0200 |
commit | 92f0017133c2d31fbd527bab7b08d4d49a582143 (patch) | |
tree | 8c336ea23ba73f6f899e85e15e82d9f37ad7b97d /tests/migrations | |
parent | 59262c294d26d2aa9346284519545c0f988bf353 (diff) | |
download | django-92f0017133c2d31fbd527bab7b08d4d49a582143.tar.gz |
Refs #34534 -- Reduced Add/RemoveConstraint and Add/RenameIndex operations when optimizing migrations.
Diffstat (limited to 'tests/migrations')
-rw-r--r-- | tests/migrations/test_optimizer.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py index cb4000cb03..ec5f6b4ce9 100644 --- a/tests/migrations/test_optimizer.py +++ b/tests/migrations/test_optimizer.py @@ -2,6 +2,7 @@ from django.db import migrations, models from django.db.migrations import operations from django.db.migrations.optimizer import MigrationOptimizer from django.db.migrations.serializer import serializer_factory +from django.db.models.functions import Abs from django.test import SimpleTestCase from .models import EmptyManager, UnicodeModel @@ -1159,6 +1160,38 @@ class OptimizerTests(SimpleTestCase): ] ) + def test_add_rename_index(self): + tests = [ + models.Index(fields=["weight", "pink"], name="mid_name"), + models.Index(Abs("weight"), name="mid_name"), + models.Index( + Abs("weight"), name="mid_name", condition=models.Q(weight__gt=0) + ), + ] + for index in tests: + with self.subTest(index=index): + renamed_index = index.clone() + renamed_index.name = "new_name" + self.assertOptimizesTo( + [ + migrations.AddIndex("Pony", index), + migrations.RenameIndex( + "Pony", new_name="new_name", old_name="mid_name" + ), + ], + [ + migrations.AddIndex("Pony", renamed_index), + ], + ) + self.assertDoesNotOptimize( + [ + migrations.AddIndex("Pony", index), + migrations.RenameIndex( + "Pony", new_name="new_name", old_name="other_name" + ), + ], + ) + def test_add_remove_index(self): self.assertOptimizesTo( [ @@ -1173,6 +1206,24 @@ class OptimizerTests(SimpleTestCase): [], ) + def test_add_remove_constraint(self): + gt_constraint = models.CheckConstraint( + check=models.Q(pink__gt=2), name="constraint_pony_pink_gt_2" + ) + self.assertOptimizesTo( + [ + migrations.AddConstraint("Pony", gt_constraint), + migrations.RemoveConstraint("Pony", gt_constraint.name), + ], + [], + ) + self.assertDoesNotOptimize( + [ + migrations.AddConstraint("Pony", gt_constraint), + migrations.RemoveConstraint("Pony", "other_name"), + ], + ) + def test_create_model_add_index(self): self.assertOptimizesTo( [ |