Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git and django migrations: ignore the migrations files

Tags:

git

sqlite

django

Should I keep django migrations files in a git repository? In a developers team, how they manages their database changes.

For example, Tom has made changes in their models, and ran makemigrations and migrate, his database now has changed, as well his migrations files, keeping his migration story.

Meantime, Bob has made changes too. He has his migration files that are about another models, He ran makemigrations and migrate commands, and his db changed.

Tom and Bob are working in the same app, so, They share the same migrations files. And the same db too.

So, what will happen when Bob push their code to git repo, and later Tom pull or fetch it from git repo? The migrations files will be mixed and their stories will be broken. Also, what about with the db itself, if it is a sqlite file, should I keep it in git repo?

like image 748
adrian oviedo Avatar asked May 17 '17 14:05

adrian oviedo


3 Answers

You should absolutely keep migrations in your repo! With some projects, it makes sense to include initial data or other functions in the migrations by editing them, so setting up each developer's environment by automatically generating them from models.py won't work for every project.

If Tom and Bob both make changes that are independent of each other (Tom adds one field, Bob adds another), the migrations files will work when you create a merge migration. Tom and Bob may have to coordinate if they conflict:

$ python manage.py migrate
CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0002_mymodel_my_field_tom, 0002_mymodel_my_field_bob in myapp).
To fix them run 'python manage.py makemigrations --merge'

$ python manage.py makemigrations --merge
Merging myapp
  Branch 0002_mymodel_my_field_bob
    - Add field my_field_bob to mymodel
  Branch 0002_mymodel_my_field_tom
    - Add field my_field_tom to mymodel

Merging will only work if the operations printed above do not conflict
with each other (working on different fields or models)
Do you want to merge these migration branches? [y/N] y

Created new merge migration /myapp/migrations/0003_merge_20170517_1445.py

Here's a good read:

https://www.algotech.solutions/blog/python/django-migrations-and-how-to-manage-conflicts/

like image 114
whp Avatar answered Oct 09 '22 09:10

whp


You should keep migration files in git to make sure database schema of all developers are in sync. Later these same migration files are used to migrate production database.

Any developer can create their migration file and add to version control and push to remote server. Django's migrate command detects new migrations and applies them to database.

In case two developers are working on same app and both of them create migration, django will create a merge migration file just like git creates a merge commit. This merge migration will make sure there are no conflicts in database schema and their database is in sync with latest the commit.

like image 39
v1k45 Avatar answered Oct 09 '22 11:10

v1k45


https://gist.github.com/mhipo1364/a55da230e1ec80bfab70e9650637bb15/revisions

Re-Generate Migration

To merge exist migration files into one file:

  • Remove django_migration records table (manually)
  • Remove all migration files
  • run python manage.py migrate --fake command
  • run python manage.py makemigrations command
  • run python manage.py migrate --fake-initial command
  • run python manage.py migrate contenttypes command
  • and finally, for chacking if everything is just fine, run python manage.py migrate command
like image 40
Herman Arais Avatar answered Oct 09 '22 11:10

Herman Arais