Safe Django migrations in CLIST
Database changes are the highest-risk edits in this repo. The DB runs in the
db container and holds real data. Be conservative.
Procedure
- Inspect first. Read the target model in
src/<app>/models.pyand look at recent migrations insrc/<app>/migrations/to match style. - Find all usages of any field/table you plan to change or remove
(
grep/search acrosssrc/andlegacy/). Removing or renaming something still referenced will break the app. - Prefer additive changes. Add new nullable fields rather than renaming or dropping. For a rename, prefer add-new → backfill → switch readers → remove old, across separate migrations.
- Generate, don't hand-write:
Review the generated migration file before applying.docker compose exec dev ./manage.py makemigrations <app> - Apply:
docker compose exec dev ./manage.py migrate <app> - Verify the app still imports/serves and relevant tests pass
(
./manage.py test <app>).
Hard rules
- Never edit an already-applied historical migration unless explicitly asked. Add a new one instead.
- Never run
DROP/DELETE/TRUNCATEor an irreversible data migration without explicit user approval. - Don't rename or drop a column/table without first proving it's unused.
- If a data migration is needed, make it reversible (
RunPythonwith a reverse function) where feasible, and call out anything that isn't. - Treat
src/clist/,src/true_coders/,src/my_oauth/,src/ranking/models as high-traffic — extra care.
When done, report
The migration created, whether it was applied, backward-compatibility risks, and any manual backfill/cleanup step a human still needs to run.