Opened 4 years ago

Closed 4 years ago

Last modified 12 months ago

#24828 closed Cleanup/optimization (fixed)

AlterUniqueTogether prevents squashmigrations optimizations

Reported by: Piotr Maliński Owned by: Markus Holtermann
Component: Migrations Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model that has a AlterUniqueTogether so in initial migration after CreateModel there was a AlterUniqueTogether operation. After that I got some migrations that add/alter/remove some fields to that model (but not those in AlterUniqueTogether). squashmigrations couldn't optimize those additional operations into one CreateModel operation. When I moved AlterUniqueTogether to the end of the squash operations list and resquashed then it did that. (Django 1.8.1)

Change History (7)

comment:1 Changed 4 years ago by Piotr Maliński

AlterIndexTogether also. I've also noticed that if there are two or more models the optimization is quite fragile to order of operations. When I move all model A simple add/remove/alter operations after CreateModel, then move similar operations after CreateModel B then it will resquash it nicely. If those operations are mixes and CreateModel A, B are at start then it won't merge operations.

comment:2 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

I can't confirm this can be definitely be fixed, but investigating it seems worthwhile.

comment:3 Changed 4 years ago by Markus Holtermann

Version: 1.8master

Yes, it's possible, but I think it's not a trivial thing like adding another rule to the MigrationOptimizer (django.db.migrations.optimizer). AlterFOOTogether and AlterField have nothing in common that would require a merge / reordering. Thus it's not picked up, I guess. But that's just from a brief look.

comment:4 Changed 4 years ago by Markus Holtermann

Has patch: set
Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:5 Changed 4 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:6 Changed 4 years ago by Markus Holtermann <info@…>

Resolution: fixed
Status: assignedclosed

In e470f311:

Fixed #24828 -- Allowed migration optimization across AlterFooTogether

The idea behind this change is, that AlterUniqueTogether,
AlterIndexTogether and AlterOrderWithRespectTo can always be moved after
an Add/Alter/Rename/RemoveField operation if they don't refer to the
respective field and are not empty sets / None.

Combined with the optimizations of duplicate AlterUniqueTogether,
AlterIndexTogether, and AlterOrderWithRespectTo operations from
128caa1e16ec2627737748f75c8e55600a3df97f, these operations are optimized
in a later round of the optimizer.

Thanks Tim Graham for the review.

comment:7 Changed 12 months ago by Tim Graham <timograham@…>

In ed7898e1:

Fixed #28862 -- Disabled optimization of AlterFooTogether and RemoveField.

AlterFooTogether operations cannot be swapped with RemoveField operations on
the same model as they could be removing the the same field as well.

Since AlterFooTogether operations don't track what their previous value was,
it's impossible to determine whether or not the optimization is safe so the
only way to proceed is to disable the optimization.

Thanks Ramiro Morales for the in-depth analysis of the issue.

Refs #24828

Note: See TracTickets for help on using tickets.
Back to Top