When shall we use mergemigrations --meerge/mergemigrations and when to use squashmigrations can we execute these commands via pipeline insteed of each developer in team doing it manually ?
CodePudding user response:
Let us assume that the migration folder has the following migrations.
./foo
./migrations
0001_initial.py
0002_userprofile.py
0003_article.py
0004_auto_add.py
above are the ordered till now? As we working in team.
- Developer1 working on the article features and he has created a migration 0005_add_name_in_article.py
- Developer2 working on the userprofile features and he has created the 0005_add_mobile_in_userprofile.py
- Developer3 working on the category features and he has creatd the 0005_category.py
Now that the situation has occurred when
- Developer1 will merge the code into develop branch, the migration file will be
./foo
./migrations
0001_initial.py
0002_userprofile.py
0003_article.py
0004_auto_add.py
0005_add_name_in_article.py
- Developer2 will merge the code into develop branch, the migration file will be
./foo
./migrations
0001_initial.py
0002_userprofile.py
0003_article.py
0004_auto_add.py
0005_add_name_in_article.py
0005_add_mobile_in_userprofile.py
Here, the last two migrations have the same dependence, and this is breaking the order of the migration, while Developer2 migrating the migration after merging the develop branch django will provide the hit to merge the migration. After running the command, Django will create a new migration, 0006_merge_add_name_in_article_add_mobile_in_userprofile.py, which is merge the 0005_add_name_in_article.py and 0005_add_mobile_in_userprofile.py
The file contains the
dependencies = [
('app_name', '0005_add_name_in_article'),
('app_name', '0005_add_mobile_in_userprofile'),
]
Now the Developer2 local branch has the following migrations.
./foo
./migrations
0001_initial.py
0002_userprofile.py
0003_article.py
0004_auto_add.py
0005_add_name_in_article.py
0005_add_mobile_in_userprofile.py
0006_merge_add_name_in_article_add_mobile_in_userprofile.py
Now the Developer2 will raise a PR and merge so that another develop note have and conflict.
- Same process will do Developer3 for avoiding the migration conflict.
What is "merge migration"?
The main aim of the merge migration is to reduce the conflict on the database level while working in collaborative manners. and it will generate the new migration file for managing the order of migrations.
What is "squash migration"?
May a question arise in your mind: how many unnecessary files will be created for migration?"
- In merge migration file, there has not been any operation.
- There may be the table where you have add one column then remove and then re-add it to table the different migration may created The main aim of squash migration is to optimize the migration file. but before squashing the migration, please refer to the docs as in some cases it may be cumbersome or raise a migration conflict.
Can we execute these commands via pipeline instead of each developer in our team doing it manually?
Nope, as per the above use case of merge and squash migration, We can't do it on pipeline as it developer responsibility to keep migration in ordered and as per the django community all migration should be sync in team so we can not do it in pipeline.