Django models and migrations ¶
Models and migrations
Cal-ITP Benefits defines a number of models in the core application, used throughout the codebase to configure different parts of the UI and logic.
The Cal-ITP Benefits database is a simple read-only Sqlite database, initialized from the data migration files.
Migrations ¶
The database is rebuilt from scratch each time the container starts. We maintain a few migration files that set up the schema and load initial data.
These files always represent the current schema and data for the database and match the current structure of the model classes.
Updating models ¶
When models are updated, the migration should be updated as well.
A simple helper script exists to regenerate the migration file based on the current state of models in the local directory:
bin/makemigrations.sh
This script:
- Copies the existing migration files to a temporary directory
- Runs the django
makemigrations
command - Copies back any migration files that are missing (data migration file)
- Formats the newly regenerated schema migration file with
black
This will result in a simple diff of changes on the schema migration file. Commit these changes (including the timestamp!) along with the model changes.