From 11310e9abbdd784aea4ba50451144fc9c239f71f Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Mon, 16 May 2022 09:57:49 +0200 Subject: [PATCH] Fixed #33710 -- Made RenameIndex operation a noop when the old and new name match. --- django/db/migrations/operations/models.py | 3 +++ tests/migrations/test_operations.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index d17232e4ec..75a3b8b030 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -960,6 +960,9 @@ class RenameIndex(IndexOperation): else: from_model_state = from_state.models[app_label, self.model_name_lower] old_index = from_model_state.get_index_by_name(self.old_name) + # Don't alter when the index name is not changed. + if old_index.name == self.new_name: + return to_model_state = to_state.models[app_label, self.model_name_lower] new_index = to_model_state.get_index_by_name(self.new_name) diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index cfd28b1b39..f3c4ea8ffe 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -2988,6 +2988,11 @@ class OperationTests(OperationTestBase): with connection.schema_editor() as editor, self.assertNumQueries(0): operation.database_backwards(app_label, editor, new_state, project_state) self.assertIndexNameExists(table_name, "new_pony_test_idx") + # Reapply, RenameIndex operation is a noop when the old and new name + # match. + with connection.schema_editor() as editor: + operation.database_forwards(app_label, editor, new_state, project_state) + self.assertIndexNameExists(table_name, "new_pony_test_idx") # Deconstruction. definition = operation.deconstruct() self.assertEqual(definition[0], "RenameIndex")