Opened 10 years ago

Closed 10 years ago

#24155 closed Cleanup/optimization (fixed)

Make migration generation more deterministic

Reported by: Tomas Dobrovolny Owned by: Markus Holtermann
Component: Migrations Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you have an application and run makemigrations command twice, generated content of migration file is different (order of generated function call parameters etc). Thus, it is very difficult to merge changes from two development branches together.

Workflow
2) run makemigrations
3) move generated migration somewhere (app/migrations/0001_initial.py to /tmp/)
4) run makemigrations again
5) compare generated migration with moved one (diff app/migrations/0001_initial.py with /tmp/0001_initial.py

You got almout all line of code is different.

Change History (8)

comment:1 by Tim Graham, 10 years ago

Summary: Rerun migration on same model generate different migratrionSorting field kwargs in migrations
Triage Stage: UnreviewedAccepted

I can reproduce on Python 3 at least.

With the tutorial, one time I get:

migrations.CreateModel(
            name='Choice',
            fields=[
                ('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),

Next time:

                ('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),

I can't think of any drawbacks of sorting the parameters, but I haven't looked into implementation feasibility.

comment:2 by Tomas Dobrovolny, 10 years ago

Not only fields kwargs in migration but also:

imports

One time:

from django.db import models, migrations
from django.conf import settings
import datetime
import django.db.models.deletion
import django.core.validators
from django.utils.timezone import utc

Next time:

from django.db import models, migrations
from django.utils.timezone import utc
import django.core.validators
import django.db.models.deletion
import datetime
from django.conf import settings

dependencies member of Migration class

One time:

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('iso3166', '0002_initialdata'),
        ('auth', '0001_initial'),
    ]

Next time:

class Migration(migrations.Migration):

    dependencies = [
        ('iso3166', '0002_initialdata'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('auth', '0001_initial'),
    ]

and

  • options
  • and models.ForeignKey, ManyToManyField and OneToOneField kwargs,

comment:3 by Tim Graham, 10 years ago

Summary: Sorting field kwargs in migrationsMake migration generation more deterministic

comment:4 by Markus Holtermann, 10 years ago

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:5 by Markus Holtermann, 10 years ago

I'm wondering how to sort the imports. The easiest would be a sorted(import_strings) but that would mean

from django.db import models, migrations
from django.utils.timezone import utc
import datetime

We could make the sorting more intelligent but I don't think that's necessary. After all, this is still auto generated code.

comment:6 by Aymeric Augustin, 10 years ago

You could use key=lambda stmt: stmt.split()[1] for slightly better results.

comment:7 by Markus Holtermann, 10 years ago

Has patch: set

PR: https://github.com/django/django/pull/3943

I don't think this change needs release notes.

comment:8 by Markus Holtermann <info@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 7f20041bca43ca33f0a9617793f2af7ab07c3fab:

Fixed #24155 -- Maintained kwargs and import order in migration writer

Thanks Tomas Dobrovolny for the report and Tim Graham for the review.

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