Database migration guide¶
This guide describes the step-by-step process for making and applying database migrations when using the containerized django-backend, and how to keep schema.prisma in sync.
Prerequisites¶
This guide assumes you can run the development compose setup described in docker/development/README.md.
Step-by-step guide to do database migrations¶
- Do desired model change in relevant
models.pyfile. - Inside
customoffice/docker/developmentrun:
docker compose run --rm django-make-migrations <DATABASE_NAME>
Or, for specific Django apps:
docker compose run --rm django-make-migrations <DATABASE_NAME> <DJANGO_APP1> <DJANGO_APP2>
- Manually verify migration file changes matches expectations.
- Apply migrations with:
docker compose run --rm django-migrate <DATABASE_NAME>
- If needed do data migrations. Remember to test data migrations in and out to avoid inconsistent databases, test all possible permutations.
- Keep Prisma in sync by verifying and updating
schema.prismafor the wanted change.
You can manually verify that you can generate validators and serializers, these are based on the model changes.
npx nx run backend-db-helpers:prepare-build
- When ready to commit, you need to commit:
models.pyschema.prisma- the generated migration file(s)
Check for migration drift by running:
docker compose run --rm django-make-migrations <DATABASE_NAME>
If it outputs new migration(s), unwanted drift has occurred. Please correct and re-run the steps above.