Skip to content

Commit

Permalink
Fixed #35717 -- Reduced Create/RemoveCollation operations when optimi…
Browse files Browse the repository at this point in the history
…zing migrations.
  • Loading branch information
adamchainz authored and sarahboyce committed Oct 10, 2024
1 parent 68cee15 commit cee95e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions django/contrib/postgres/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ def describe(self):
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."""
Expand Down
20 changes: 19 additions & 1 deletion tests/postgres_tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def test_drop_nonexistent_extension(self):


@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()])
Expand Down Expand Up @@ -459,6 +459,24 @@ def test_writer(self):
"),",
)

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):
Expand Down

0 comments on commit cee95e6

Please sign in to comment.