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?
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/
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.
https://gist.github.com/mhipo1364/a55da230e1ec80bfab70e9650637bb15/revisions
To merge exist migration files into one file:
django_migration
records table (manually)python manage.py migrate --fake
commandpython manage.py makemigrations
commandpython manage.py migrate --fake-initial
commandpython manage.py migrate contenttypes
commandpython manage.py migrate
commandIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With