summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2024-08-29 18:52:31 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-10-10 13:18:46 +0200
commitcee95e6172f2ea3dbf8cfd4214562cfc92f95392 (patch)
treeb1fc28212831bb587d56c10bcec7130313baea0a
parent68cee15a8fd65bd6ee2329d80331f87cadd97ff5 (diff)
Fixed #35717 -- Reduced Create/RemoveCollation operations when optimizing migrations.
-rw-r--r--django/contrib/postgres/operations.py5
-rw-r--r--tests/postgres_tests/test_operations.py20
2 files changed, 24 insertions, 1 deletions
diff --git a/django/contrib/postgres/operations.py b/django/contrib/postgres/operations.py
index 1ee5fbc2e2..84360febf9 100644
--- a/django/contrib/postgres/operations.py
+++ b/django/contrib/postgres/operations.py
@@ -237,6 +237,11 @@ class CreateCollation(CollationOperation):
def migration_name_fragment(self):
return "create_collation_%s" % self.name.lower()
+ def reduce(self, operation, app_label):
+ if isinstance(operation, RemoveCollation) and self.name == operation.name:
+ return []
+ return super().reduce(operation, app_label)
+
class RemoveCollation(CollationOperation):
"""Remove a collation."""
diff --git a/tests/postgres_tests/test_operations.py b/tests/postgres_tests/test_operations.py
index f344d4ae74..322f38148b 100644
--- a/tests/postgres_tests/test_operations.py
+++ b/tests/postgres_tests/test_operations.py
@@ -318,7 +318,7 @@ class CreateExtensionTests(PostgreSQLTestCase):
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
-class CreateCollationTests(PostgreSQLTestCase):
+class CreateCollationTests(OptimizerTestBase, PostgreSQLTestCase):
app_label = "test_allow_create_collation"
@override_settings(DATABASE_ROUTERS=[NoMigrationRouter()])
@@ -459,6 +459,24 @@ class CreateCollationTests(PostgreSQLTestCase):
"),",
)
+ def test_reduce_create_remove(self):
+ self.assertOptimizesTo(
+ [
+ CreateCollation(
+ "sample_collation",
+ "und-u-ks-level2",
+ provider="icu",
+ deterministic=False,
+ ),
+ RemoveCollation(
+ "sample_collation",
+ # Different locale
+ "de-u-ks-level1",
+ ),
+ ],
+ [],
+ )
+
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests.")
class RemoveCollationTests(PostgreSQLTestCase):