Skip to content

Changing the database model

Felix Lampe edited this page Nov 18, 2024 · 6 revisions

Cheat sheet

git checkout right-before-model-change
rm db/db.sqlite3
python manage.py migrate --settings=config.settings.local
python manage.py loaddata --settings=config.settings.local e2e_tests/database/test_database.json
cp -r e2e_tests/database/test_database_uploads/. cpmonitor/images/uploads
git checkout after-model-change-including-migration
python manage.py migrate --settings=config.settings.local
python -Xutf8 manage.py dumpdata -e contenttypes -e auth.Permission -e admin.LogEntry -e sessions --indent 2 --settings=config.settings.local > e2e_tests/database/test_database.json
# if additional images were uploaded:
cp -r cpmonitor/images/uploads e2e_tests/database/test_database_uploads

Details

When the database model in models.py is changed a new migration has to be created specifying how to change to the new database format. This can be done by

python manage.py makemigrations --settings=config.settings.local

The migration might have to be improved, e.g. by using migrations.RunPython to write conversions of the data by hand.

Afterwards the test database has to be updated as well. Use the dumpdata command to generate a test database from the currently running database:

python -Xutf8 manage.py dumpdata -e contenttypes -e auth.Permission -e admin.LogEntry -e sessions --indent 2 --settings=config.settings.local > e2e_tests/database/test_database.json

Check the diff of e2e_tests/database/test_database.json for any unexpected parts and adjust as necessary.

The same procedure should be applied to some other database dumps, but not to all. Check the following directories:

  • e2e_tests/database
  • cpmonitor/fixtures

But do not update these, since they are to be used for a specific schema version:

  • cpmonitor/fixtures/complete_0006.json

There are some dumps of the production database in e2e_tests/database which should not be updated, as well. Their README file contains information about the schema version for which they are valid.

Clone this wiki locally