Skip to content

Comments

swev-id: django__django-16819 Optimize migration index reductions#527

Open
casey-brooks wants to merge 1 commit intodjango__django-16819from
noa/issue-522
Open

swev-id: django__django-16819 Optimize migration index reductions#527
casey-brooks wants to merge 1 commit intodjango__django-16819from
noa/issue-522

Conversation

@casey-brooks
Copy link

Summary

  • add migration optimizer reductions collapsing redundant AddIndex/RemoveIndex pairs
  • ensure repeated RemoveIndex operations keep the leading removal
  • extend optimizer tests to cover index cancellation, replacement, and rename boundaries

Resolves #522.

Reproduction Steps

PYTHONPATH=$PWD:$PWD/tests:$HOME/.nix-profile/lib/python3.11/site-packages \
DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py migrations --parallel=1

Observed Failure

======================================================================
FAIL: test_add_index_remove_index (migrations.test_optimizer.OptimizerTests.test_add_index_remove_index)
AssertionError: Lists differ: [] != ["migrations.AddIndex(\n    model_name='Pony',\n    index=models.Index(condition=models.Q(('pink', True)), fields=['pink'], include=('weight',), name='PONY_IDX', opclasses=['varchar_pattern_ops']),\n)"]

======================================================================
FAIL: test_add_index_replaced_by_later_add_index (migrations.test_optimizer.OptimizerTests.test_add_index_replaced_by_later_add_index)
AssertionError: Lists differ: ["migr...GinIndex(fields=['weight'], name='pony_idx'),\n)"] != ["migr...Index(fields=['weight'], name='pony_idx'),\n)"]

======================================================================
FAIL: test_remove_index_remove_index (migrations.test_optimizer.OptimizerTests.test_remove_index_remove_index)
AssertionError: Lists differ: ["migr... name='PONY_IDX',\n)"] != ["migr... name='PONY_IDX',\n)", "migr... name='pony_idx',\n)"]

Fix Summary

  • short-circuit AddIndex.reduce to drop matching RemoveIndex and defer to the latest AddIndex
  • short-circuit RemoveIndex.reduce to retain only the leading removal for identical targets
  • add optimizer coverage to ensure name-based comparisons work across index variants and renames

Testing

PYTHONPATH=$PWD:$PWD/tests:$HOME/.nix-profile/lib/python3.11/site-packages \
DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py migrations --parallel=1

@casey-brooks casey-brooks requested a review from a team December 24, 2025 17:37
Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for padding the header text to keep the sort controls accessible (and for mirroring the change in rtl.css).

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add/RemoveIndex reductions look solid and the new optimizer tests cover the regression cases.

Copy link
Collaborator

@rowan-stein rowan-stein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Reviewed optimizer changes:

  • AddIndex.reduce and RemoveIndex.reduce correctly handle index add/remove and replacement cases.
  • Tests in tests/migrations/test_optimizer.py pass locally (42 tests in module).

Approving.

@rowan-stein rowan-stein changed the base branch from django__django-16819 to main January 11, 2026 20:24
@rowan-stein rowan-stein changed the base branch from main to django__django-16819 January 12, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce Add/RemoveIndex migration operations

3 participants