229đ
Quoting from the Django migrations documentation:
The migration files for each app live in a âmigrationsâ directory inside of that app, and are designed to be committed to, and distributed as part of, its codebase. You should be making them once on your development machine and then running the same migrations on your colleaguesâ machines, your staging machines, and eventually your production machines.
If you follow this process, you shouldnât be getting any merge conflicts in the migration files.
When merging version control branches, you still may encounter a situation where you have multiple migrations based on the same parent migration, e.g. if to different developers introduced a migration concurrently. One way of resolving this situation is to introduce a merge_migration. Often this can be done automatically with the command
./manage.py makemigrations --merge
which will introduce a new migration that depends on all current head migrations. Of course this only works when there is no conflict between the head migrations, in which case you will have to resolve the problem manually.
Given that some people here suggested that you shouldnât commit your migrations to version control, Iâd like to expand on the reasons why you actually should do so.
First, you need a record of the migrations applied to your production systems. If you deploy changes to production and want to migrate the database, you need a description of the current state. You can create a separate backup of the migrations applied to each production database, but this seems unnecessarily cumbersome.
Second, migrations often contain custom, handwritten code. Itâs not always possible to automatically generate them with ./manage.py makemigrations
.
Third, migrations should be included in code review. They are significant changes to your production system, and there are lots of things that can go wrong with them.
So in short, if you care about your production data, please check your migrations into version control.
42đ
You can follow the below process.
You can run makemigrations
locally and this creates the migration file. Commit this new migration file to repo.
In my opinion you should not run makemigrations
in production at all. You can run migrate
in production and you will see the migrations are applied from the migration file that you committed from local. This way you can avoid all conflicts.
IN LOCAL ENV, to create the migration files,
python manage.py makemigrations
python manage.py migrate
Now commit these newly created files, something like below.
git add app/migrations/...
git commit -m 'add migration files' app/migrations/...
IN PRODUCTION ENV, run only the below command.
python manage.py migrate
- [Django]-Table thumbnail_kvstore doesn't exist
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-Django models: Only permit one entry in a model?
22đ
Quote from the 2022 docs, Django 4.0. (two separate commands = makemigrations
and migrate
)
The reason that there are separate commands to make and apply
migrations is because youâll commit migrations to your version control
system and ship them with your app; they not only make your
development easier, theyâre also useable by other developers and in
production.
- [Django]-Django: Display Choice Value
- [Django]-How to resize the new uploaded images using PIL before saving?
- [Django]-Class has no objects member
18đ
TL;DR: commit migrations, resolve migration conflicts, adjust your git workflow.
Feels like youâd need to adjust your git workflow, instead of ignoring conflicts.
Ideally, every new feature is developed in a different branch, and merged back with a pull request.
PRs cannot be merged if thereâs a conflict, therefore who needs to merge his feature needs to resolve the conflict, migrations included. This might need coordination between different teams.
It is important though to commit migration files! If a conflict arises, Django might even help you solve those conflicts đ
- [Django]-Where to put business logic in django
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-Check if celery beat is up and running
5đ
I canât imagine why you would be getting conflicts, unless youâre editing the migrations somehow? That usually ends badly â if someone misses some intermediate commits then they wonât be upgrading from the correct version, and their copy of the database will be corrupted.
The process that I follow is pretty simple â whenever you change the models for an app, you also commit a migration, and then that migration doesnât change â if you need something different in the model, then you change the model and commit a new migration alongside your changes.
In greenfield projects, you can often delete the migrations and start over from scratch with a 0001_ migration when you release, but if you have production code, then you canât (though you can squash migrations down into one).
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-How do I filter ForeignKey choices in a Django ModelForm?
- [Django]-How to force Django models to be released from memory
5đ
The solution usually used, is that, before anything is merged into master, the developer must pull any remote changes. If thereâs a conflict in migration versions, he should rename his local migration (the remote one has been run by other devs, and, potentially, in production), to N+1.
During development it might be okay to just not-commit migrations (donât add an ignore though, just donât add
them). But once youâve gone into production, youâll need them in order to keep the schema in sync with model changes.
You then need to edit the file, and change the dependencies
to the latest remote version.
This works for Django migrations, as well as other similar apps (sqlalchemy+alembic, RoR, etc).
- [Django]-How to change empty_label for modelForm choice field?
- [Django]-Using django-admin on windows powershell
- [Django]-Substring in a django template?
3đ
Gitignore the migrations, if You have separate DBs for Development, Staging and Production environment. For dev. purposes You can use local sqlite DB and play with migrations locally.
I would recommend You to create four additional branches:
-
Master â Clean fresh code without migrations. Nobody is connected to this branch. Used for code reviews only
-
Development â daily development. Push/pull accepted. Each developer is working on sqlite DB
-
Cloud_DEV_env â remote cloud/server DEV environment. Pull only. Keep migrations locally on machine, which is used for the code deployment and remote migrations of Dev database
-
Cloud_STAG_env â remote cloud/server STAG environment. Pull only. Keep migrations locally on machine, which is used for the code deployment and remote migrations of Stag database
-
Cloud_PROD_env â remote cloud/server DEV environment. Pull only. Keep migrations locally on machine, which is used for the code deployment and remote migrations of Prod database
Notes:
2, 3, 4 â migrations can be kept in repos but there should be strict rules of pull requests merging, so we decided to find a person, responsible for deployments, so the only guy who has all the migration files â our deploy-er. He keeps the remote DB migrations each time we have any changes in Models.
- [Django]-How can I enable CORS on Django REST Framework
- [Django]-Do we need to upload virtual env on github too?
- [Django]-Set up a scheduled job?
1đ
You should think of migrations as a version control system for your database schema. makemigrations is responsible for packaging up your model changes into individual migration files â analogous to commits â and migrate is responsible for applying those to your database.
The migration files for each app live in a âmigrationsâ directory inside of that app, and are designed to be committed to, and distributed as part of, its codebase. You should be making them once on your development machine and then running the same migrations on your colleaguesâ machines, your staging machines, and eventually your production machines.
golden rule : Make once on dev and migrate on all
- [Django]-DRF: custom ordering on related serializers
- [Django]-Django â How to use decorator in class-based view methods?
- [Django]-Why does Django's render() function need the "request" argument?
0đ
Having a bunch of migration files in git is messy. There is only one file in migration folder that you should not ignore. That file is init.py file, If you ignore it, python will no longer look for submodules inside the directory, so any attempts to import the modules will fail. So the question should be how to ignore all migration files but init.py?
The solution is:
Add â0*.pyâ to .gitignore files and it does the job perfectly.
Hope this helps someone.
- [Django]-Adding css class to field on validation error in django
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
- [Django]-Explicitly set MySQL table storage engine using South and Django
-2đ
Committing your migrations is just a recipe for disaster. Because the migrations are somewhat or a chain that can be traced back, if you have dependences from a former migration e.g a pip module which you used at some point in your project lifecycle and then stopped using. You might find bread crumbs of such dependences in your migrations thread and you have to manually remove these imports from the migrations file.
Verdict, except you are a god tier Django dev, probably avoid adding migrations to your commits.
- [Django]-Storing an Integer Array in a Django Database
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-Error: No module named staticfiles
-3đ
Short answer
I propose excluding migrations in the repo. After code merge, just run ./manage.py makemigrations
and you are all set.
Long answer
I donât think you should put migrations files into repo. It will spoil the migration states in other personâs dev environment and other prod and stage environment. (refer to Sugar Tangâs comment for examples).
In my point of view, the purpose of Django migrations is to find gaps between previous model states and new model states, and then serialise the gap. If your model changes after code merge, you can simple do makemigrations
to find out the gap. Why do you want to manually and carefully merge other migrations when you can achieve the same automatically and bug free? Django documentation says,
They*(migrations)*âre designed to be mostly automatic
; please keep it that way. To merge migrations manually, you have to fully understand what others have changed and any dependence of the changes. Thatâs a lot of overhead and error prone. So tracking models file is sufficient.
It is a good topic on the workflow. I am open to other options.
- [Django]-How to access a dictionary element in a Django template?
- [Django]-Django datefield filter by weekday/weekend
- [Django]-Add inline model to django admin site