Skip to content

Commit 38877bb

Browse files
start using django-linear-migrations
1 parent 336cd7f commit 38877bb

5 files changed

Lines changed: 83 additions & 0 deletions

File tree

docker-compose.override.dev.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ services:
1616
DD_ADMIN_PASSWORD: "${DD_ADMIN_PASSWORD:-admin}"
1717
DD_EMAIL_URL: "smtp://mailhog:1025"
1818
celeryworker:
19+
build:
20+
context: .
21+
dockerfile: Dockerfile.django-${DEFECT_DOJO_OS:-debian}
22+
target: development
1923
entrypoint: ['/wait-for-it.sh', '${DD_DATABASE_HOST:-postgres}:${DD_DATABASE_PORT:-5432}', '-t', '30', '--', '/entrypoint-celery-worker-dev.sh']
2024
volumes:
2125
- '.:/app:z'
@@ -24,12 +28,20 @@ services:
2428
DD_DEBUG: 'True'
2529
DD_EMAIL_URL: "smtp://mailhog:1025"
2630
celerybeat:
31+
build:
32+
context: .
33+
dockerfile: Dockerfile.django-${DEFECT_DOJO_OS:-debian}
34+
target: development
2735
volumes:
2836
- '.:/app:z'
2937
environment:
3038
PYTHONWARNINGS: error # We are strict about Warnings during development
3139
DD_DEBUG: 'True'
3240
initializer:
41+
build:
42+
context: .
43+
dockerfile: Dockerfile.django-${DEFECT_DOJO_OS:-debian}
44+
target: development
3345
volumes:
3446
- '.:/app:z'
3547
environment:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0257_pghistory_tags_backfill

dojo/settings/settings.dist.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,14 @@ def saml2_attrib_map_format(din):
20612061

20622062
MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware", *MIDDLEWARE]
20632063

2064+
# Linear migrations for development
2065+
# Helps avoid merge migration conflicts by tracking the latest migration
2066+
if DEBUG:
2067+
INSTALLED_APPS = (
2068+
"django_linear_migrations", # Must be before dojo to override makemigrations
2069+
*INSTALLED_APPS,
2070+
)
2071+
20642072
def show_toolbar(request):
20652073
return True
20662074

readme-docs/CONTRIBUTING.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,64 @@ This will result in a new file in the `dojo/db_migrations` folder that can be co
6565
When making downstream database model changes in your fork of Defect Dojo please be aware of the risks of getting out of sync with our upstream migrations.
6666
It requiers proper knowledge of [Django Migrations](https://docs.djangoproject.com/en/5.0/topics/migrations/) to reconcile the migrations before you can upgrade to a newer version of Defect Dojo.
6767

68+
### Linear Migration History
69+
70+
DefectDojo uses [django-linear-migrations](https://github.com/adamchainz/django-linear-migrations) to maintain a linear migration history and avoid merge migration conflicts.
71+
72+
**What this means for you:**
73+
- When you run `makemigrations`, a `max_migration.txt` file is automatically updated in `dojo/db_migrations/`
74+
- This file tracks the latest migration and must be committed along with your migration file
75+
- If you're working on a feature branch and migrations are added to `dev` branch, you may encounter a merge conflict in `max_migration.txt`
76+
77+
**Resolving migration conflicts:**
78+
79+
If you encounter a conflict in `dojo/db_migrations/max_migration.txt` during a rebase or merge, follow these steps:
80+
81+
1. **Abort the current rebase/merge** (you can't switch branches while in conflict):
82+
```bash
83+
git rebase --abort
84+
# OR if you were merging: git merge --abort
85+
```
86+
87+
2. **Reverse your migration** from your local database (you're still on your feature branch):
88+
```bash
89+
# First, check the conflicting migration number (look at your latest migration file)
90+
# Then migrate back to the last migration before yours
91+
docker compose exec uwsgi bash -c "python manage.py migrate dojo XXXX_last_migration_before_yours"
92+
```
93+
94+
3. **Fetch the latest changes and retry the rebase**:
95+
```bash
96+
git fetch origin
97+
git rebase origin/dev
98+
# Git will show a conflict in max_migration.txt
99+
```
100+
101+
4. **Use the `rebase_migration` command** to automatically fix the conflict:
102+
```bash
103+
docker compose exec uwsgi bash -c "python manage.py rebase_migration dojo"
104+
```
105+
This command will:
106+
- Rename your migration to follow the latest migration from `dev`
107+
- Update its dependencies
108+
- Update `max_migration.txt`
109+
110+
5. **Continue the rebase**:
111+
```bash
112+
git add dojo/db_migrations
113+
git rebase --continue
114+
```
115+
116+
6. **Apply the rebased migration** to your local database:
117+
```bash
118+
docker compose exec uwsgi bash -c "python manage.py migrate dojo"
119+
```
120+
121+
**Tips:**
122+
- If your feature branch has multiple commits modifying the same migration, squash them first before rebasing
123+
- To find the "last migration before yours", look at your migration file's dependencies
124+
- It's also possible to skip steps 1-3 and then in step 6 do `python manage.py migrate dojo XXXX_last_migration_before_yours` and `python manage.py migrate dojo --fake` to skip your migrations if they have already been applied during earlier development.
125+
68126
## Submitting Pull Requests
69127

70128
The following are things to consider before submitting a pull request to

requirements-dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ parameterized==0.9.0
1313

1414
# Development file watching (hot reload)
1515
watchdog==6.0.0
16+
17+
# Migration management - allows for easy rebasing via manage.py rebase_migration
18+
django-linear-migrations==2.19.0
19+

0 commit comments

Comments
 (0)