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 , 10 years ago
Summary: | Rerun migration on same model generate different migratrion → Sorting field kwargs in migrations |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 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
andOneToOneField kwargs
,
comment:3 by , 10 years ago
Summary: | Sorting field kwargs in migrations → Make migration generation more deterministic |
---|
comment:4 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 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 , 10 years ago
You could use key=lambda stmt: stmt.split()[1]
for slightly better results.
comment:7 by , 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 , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I can reproduce on Python 3 at least.
With the tutorial, one time I get:
Next time:
I can't think of any drawbacks of sorting the parameters, but I haven't looked into implementation feasibility.