← All challenges
easydjango/django · v5.0

Migration optimizer does not reduce multiple AlterField

webormbase f9fe062de5
Mode

60 minutes, the full token budget.

  • Time limit60 min
  • Token budgetup to 1M
  • Worth up to135 XP

Opens VS Code with an AI agent in a new tab. Prompts, tokens, tool calls and test runs are recorded and scored.

Problem statement

Description

Let's consider the following operations:
operations = [
migrations.AddField(
model_name="book",
name="title",
field=models.CharField(max_length=256, null=True),
),
migrations.AlterField(
model_name="book",
name="title",
field=models.CharField(max_length=128, null=True),
),
migrations.AlterField(
model_name="book",
name="title",
field=models.CharField(max_length=128, null=True, help_text="help"),
),
migrations.AlterField(
model_name="book",
name="title",
field=models.CharField(max_length=128, null=True, help_text="help", default=None),
),
]
If I run the optimizer, I get only the AddField, as we could expect. However, if the AddField model is separated from the AlterField (e.g. because of a non-elidable migration, or inside a non-squashed migration), none of the AlterField are reduced:
optimizer.optimize(operations[1:], "books")
[<AlterField model_name='book', name='title', field=<django.db.models.fields.CharField>>,
<AlterField model_name='book', name='title', field=<django.db.models.fields.CharField>>,
<AlterField model_name='book', name='title', field=<django.db.models.fields.CharField>>]
Indeed, the AlterField.reduce does not consider the the case where operation is also an AlterField.
Is this behaviour intended? If so, could it be documented?
Otherwise, would it make sense to add something like
if isinstance(operation, AlterField) and self.is_same_field_operation(
operation
):
return [operation]

The environment starts at commit f9fe062de5fc (django/django 5.0), dependencies installed and tests runnable from the first minute. You are graded by hidden tests taken from the fix that was actually merged upstream.