Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you cleanly maintain django migrations on non-master branch?

  1. Imagine that I have a branch that has a migration. It's an experimental branch that keeps up with master, but will not be merged into it for a while, if ever.
  2. Master migrations change over time.
  3. The result is that when I merge in master, I end up with multiple migration leaves, meaning that manage.py migrate errors out. If I just do manage.py makemigrations --merge, then it creates a new leaf that will later also be out of date.

Is there a nice way to handle this? I'm tempted just to avoid having any migrations in long-lived dependent branches.

like image 251
Doug Bradshaw Avatar asked Apr 03 '18 15:04

Doug Bradshaw


People also ask

How does Django keep track of migration?

Django keeps track of applied migrations in the Django migrations table. Django migrations consist of plain Python files containing a Migration class. Django knows which changes to perform from the operations list in the Migration classes. Django compares your models to a project state it builds from the migrations.

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

Should I commit Django migration files?

It is important though to commit migration files! If a conflict arises, Django might even help you solve those conflicts ;) That's the right answer. An operational versioning system workflow seems to be implicit in the django documentation but it's fundamental.


1 Answers

  1. Rebase an experimental branch on top of master before merging it.
  2. Unapply all migrations from the experimental branch:

    manage.py migrate <appname> <previousMigration> 
    
  3. Delete all migrations from the experimental branch.

  4. Create a brand new migration:

    manage.py makemigrations
    
  5. Commit changes to the experimental branch

  6. Merge the experimental branch.

It's perfectly safe to delete migrations from a repository if no one else has applied them.

like image 174
Max Malysh Avatar answered Oct 08 '22 22:10

Max Malysh