1
0
mirror of https://github.com/django/django.git synced 2025-01-03 15:06:09 +00:00

Fixed #34528 -- Reduced Add/RemoveIndex operations when optimizing migrations.

This commit is contained in:
Mariusz Felisiak 2023-05-01 18:57:24 +02:00 committed by GitHub
parent 0b0998dc15
commit 191f6a9a45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -861,6 +861,11 @@ class AddIndex(IndexOperation):
def migration_name_fragment(self): def migration_name_fragment(self):
return "%s_%s" % (self.model_name_lower, self.index.name.lower()) return "%s_%s" % (self.model_name_lower, self.index.name.lower())
def reduce(self, operation, app_label):
if isinstance(operation, RemoveIndex) and self.index.name == operation.name:
return []
return super().reduce(operation, app_label)
class RemoveIndex(IndexOperation): class RemoveIndex(IndexOperation):
"""Remove an index from a model.""" """Remove an index from a model."""

View File

@ -1158,3 +1158,17 @@ class OptimizerTests(SimpleTestCase):
), ),
] ]
) )
def test_add_remove_index(self):
self.assertOptimizesTo(
[
migrations.AddIndex(
"Pony",
models.Index(
fields=["weight", "pink"], name="idx_pony_weight_pink"
),
),
migrations.RemoveIndex("Pony", "idx_pony_weight_pink"),
],
[],
)