Safe Django migration workflow with approval gates, conflict detection, and rollback awareness. Use when model changes require migrations.
Procedural workflow for Django database migrations in the All Safe Portal. This skill is auto-discovered when agents modify models or detect pending migration changes.
models.py)project-checks detects pending migration changesBefore creating any migration, always verify:
cd backend; .\venv\Scripts\Activate.ps1
# 1. Check current migration state
python manage.py showmigrations | findstr "\[ \]"
# 2. Detect pending changes (dry run, no files created)
python manage.py makemigrations --check --dry-run
If step 1 shows unapplied migrations, apply them first before creating new ones.
python manage.py makemigrations --dry-run
Review the output. Confirm which apps have pending changes. Show the user what will be created.
MANDATORY: Never run makemigrations without user confirmation.
Present to the user:
Wait for explicit "yes" or approval.
python manage.py makemigrations
Review the generated migration file. Check for:
--name)python manage.py migrate
python manage.py showmigrations | findstr "\[ \]"
Should show no unapplied migrations.
When two branches create migrations for the same app:
# Check for conflicts
python manage.py makemigrations --check
# If conflict detected, merge migrations
python manage.py makemigrations --merge
Never manually edit migration files unless resolving a merge conflict. Even then, prefer --merge first.
These require extra caution and explicit user approval:
| Operation | Risk | Mitigation |
|---|---|---|
| Remove field | Data loss | Verify field is unused, consider deprecation first |
| Remove model | Data loss | Confirm no FK references, check admin/serializers |
| Rename field | Breaks existing queries | Use RenameField operation, not remove+add |
| Change field type | Data corruption | Check compatibility, may need data migration |
| Add non-nullable field | Breaks existing rows | Provide default value or make nullable first |
For complex data transformations, create a data migration:
python manage.py makemigrations --empty {app_name} --name describe_the_change
Then edit the generated file to add RunPython operations. This is the ONE exception where editing migration files is acceptable.
project-checks skill to verify nothing broke/deploy-check command to ensure all migrations are applied (user-triggered only)backend.yml workflow runs python manage.py migrate after deployment