From 92f0017133c2d31fbd527bab7b08d4d49a582143 Mon Sep 17 00:00:00 2001 From: Akash Kumar Sen Date: Mon, 8 May 2023 23:24:46 +0530 Subject: Refs #34534 -- Reduced Add/RemoveConstraint and Add/RenameIndex operations when optimizing migrations. --- django/db/migrations/operations/models.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'django') diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 9c5bd5fbcb..b18ef55369 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -929,6 +929,9 @@ class AddIndex(IndexOperation): def reduce(self, operation, app_label): if isinstance(operation, RemoveIndex) and self.index.name == operation.name: return [] + if isinstance(operation, RenameIndex) and self.index.name == operation.old_name: + self.index.name = operation.new_name + return [AddIndex(model_name=self.model_name, index=self.index)] return super().reduce(operation, app_label) @@ -1164,6 +1167,15 @@ class AddConstraint(IndexOperation): def migration_name_fragment(self): return "%s_%s" % (self.model_name_lower, self.constraint.name.lower()) + def reduce(self, operation, app_label): + if ( + isinstance(operation, RemoveConstraint) + and self.model_name_lower == operation.model_name_lower + and self.constraint.name == operation.name + ): + return [] + return super().reduce(operation, app_label) + class RemoveConstraint(IndexOperation): option_name = "constraints" -- cgit v1.2.1