|
| 1 | +### More Examples |
| 2 | + |
| 3 | +**Branch:** `main` |
| 4 | + |
| 5 | +```bash |
| 6 | + |
| 7 | +├── app |
| 8 | +│ ├── migrations |
| 9 | +│ ├── 0001_initial.py |
| 10 | +│ ├── 0002_auto_20210521_2328.py |
| 11 | + |
| 12 | +``` |
| 13 | + |
| 14 | +**Branch:** `feature/test-a` |
| 15 | + |
| 16 | +```bash |
| 17 | + |
| 18 | +├── app |
| 19 | +│ ├── migrations |
| 20 | +│ ├── 0001_initial.py |
| 21 | +│ ├── 0002_auto_20210521_2328.py |
| 22 | +│ ├── 0003_auto_20210522_1128.py |
| 23 | + |
| 24 | +``` |
| 25 | + |
| 26 | +**Branch:**`feature/test-b` |
| 27 | + |
| 28 | +```bash |
| 29 | + |
| 30 | +├── app |
| 31 | +│ ├── migrations |
| 32 | +│ ├── 0001_initial.py |
| 33 | +│ ├── 0002_auto_20210521_2328.py |
| 34 | +│ ├── 0003_auto_20210522_1228.py |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +Both `feature/test-a` and `feature/test-b` share the last migration on `main` (`0002_auto_20210521_2328.py`) |
| 39 | + |
| 40 | +Once `feature/test-a` is merged into `main` you run into the problem of resolving migrations on `feature/test-b` which was dependent on `0002_auto_20210521_2328.py` |
| 41 | + |
| 42 | +**Branch:** `main` |
| 43 | + |
| 44 | +```bash |
| 45 | + |
| 46 | +├── app |
| 47 | +│ ├── migrations |
| 48 | +│ ├── 0001_initial.py |
| 49 | +│ ├── 0002_auto_20210521_2328.py |
| 50 | +│ ├── 0003_auto_20210522_1128.py |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +**Branch:** `feature/test-b` |
| 55 | + |
| 56 | +```bash |
| 57 | + |
| 58 | +├── app |
| 59 | +│ ├── migrations |
| 60 | +│ ├── 0001_initial.py |
| 61 | +│ ├── 0002_auto_20210521_2328.py |
| 62 | +│ ├── 0003_auto_20210522_1128.py \___________________ Both dependent on 0002_auto_20210521_2328.py |
| 63 | +│ ├── 0003_auto_20210522_1228.py / |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +Running [`makemigrations`](https://docs.djangoproject.com/en/3.2/ref/django-admin/#django-admin-makemigrations) fails with the following error: |
| 68 | + |
| 69 | + CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0003_auto_20210522_1128, 0003_auto_20210522_1228 in app). |
| 70 | + To fix them run 'python manage.py makemigrations --merge' |
| 71 | + |
| 72 | +Using the [`--merge`](https://docs.djangoproject.com/en/3.2/ref/django-admin/#cmdoption-makemigrations-merge) option creates a new migration file which might not be desired. |
| 73 | + |
| 74 | +## Solution |
| 75 | + |
| 76 | +`django-migration-fixer` identifies changes between the default branch `main`, and the feature branch `feature/test-b` and maintains a consistent dependency history as shown below: |
| 77 | + |
| 78 | +**Branch:** `feature/test-b` |
| 79 | + |
| 80 | +```bash |
| 81 | + |
| 82 | +├── app |
| 83 | +│ ├── migrations |
| 84 | +│ ├── 0001_initial.py |
| 85 | +│ ├── 0002_auto_20210521_2328.py |
| 86 | +│ ├── 0003_auto_20210522_1128.py |
| 87 | +│ ├── 0004_auto_20210522_1228.py # Renames: '0003_auto_20210522_1228.py' → '0004_auto_20210522_1228.py' |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | +`0004_auto_20210522_1228.py` |
| 92 | + |
| 93 | +```py |
| 94 | +... |
| 95 | +from django.db import migrations, models |
| 96 | + |
| 97 | + |
| 98 | +class Migration(migrations.Migration): |
| 99 | + |
| 100 | + dependencies = [ |
| 101 | + ('app', '0003_auto_20210522_1128'), # Replaced '0002_auto_20210521_2328' → '0003_auto_20210522_1128' |
| 102 | + ] |
| 103 | + |
| 104 | + operations = [ |
| 105 | + ... |
| 106 | + ] |
| 107 | +``` |
| 108 | + |
| 109 | +> NOTE: :warning: |
| 110 | +> |
| 111 | +> * This also works when there are conflicts detected on the default branch. |
| 112 | +> |
| 113 | +> i.e You can run `python manage.py makemigrations --fix` on the `main` branch |
| 114 | +> which relies on primitively picking the first migration file. |
| 115 | +> |
| 116 | +> e.g `(0003_auto_20210522_1128, 0003_auto_20210522_1228 in app)` |
| 117 | +> would result in `0003_auto_20210522_1128.py` being picked as the |
| 118 | +> base migration which might not be accurate in every case and is not recommended. |
0 commit comments