From 20d44abb41b3dde7f7edc556b784aef48c5ceddf Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 22 Aug 2024 12:03:08 +0100 Subject: [PATCH] Fixed #35700 -- Added AlterModelTable and AlterModelTableComment reductions. --- django/db/migrations/operations/models.py | 32 ++++++++++++++++++ tests/migrations/test_optimizer.py | 40 +++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 9aad9c809e..c8f7a2627a 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -184,6 +184,38 @@ class CreateModel(ModelOperation): managers=operation.managers, ), ] + elif ( + isinstance(operation, AlterModelTable) + and self.name_lower == operation.name_lower + ): + return [ + CreateModel( + self.name, + fields=self.fields, + options={ + **self.options, + "db_table": operation.table, + }, + bases=self.bases, + managers=self.managers, + ), + ] + elif ( + isinstance(operation, AlterModelTableComment) + and self.name_lower == operation.name_lower + ): + return [ + CreateModel( + self.name, + fields=self.fields, + options={ + **self.options, + "db_table_comment": operation.table_comment, + }, + bases=self.bases, + managers=self.managers, + ), + ] elif ( isinstance(operation, AlterTogetherOptionOperation) and self.name_lower == operation.name_lower diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py index 2acbc7f09f..3ed30102bf 100644 --- a/tests/migrations/test_optimizer.py +++ b/tests/migrations/test_optimizer.py @@ -154,6 +154,46 @@ class OptimizerTests(SimpleTestCase): ], ) + def test_create_alter_model_table(self): + self.assertOptimizesTo( + [ + migrations.CreateModel("Foo", fields=[]), + migrations.AlterModelTable( + name="foo", + table="foo", + ), + ], + [ + migrations.CreateModel( + "Foo", + fields=[], + options={ + "db_table": "foo", + }, + ), + ], + ) + + def test_create_alter_model_table_comment(self): + self.assertOptimizesTo( + [ + migrations.CreateModel("Foo", fields=[]), + migrations.AlterModelTableComment( + name="foo", + table_comment="A lovely table.", + ), + ], + [ + migrations.CreateModel( + "Foo", + fields=[], + options={ + "db_table_comment": "A lovely table.", + }, + ), + ], + ) + def test_create_model_and_remove_model_options(self): self.assertOptimizesTo( [