pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

Detect invalid migration state when using --reuse-db

Open tolomea opened this issue 5 years ago • 1 comments
trafficstars

When using --reuse-db it would be nice if pytest-django could detect that the database it's reusing has applied migrations that don't exist (due to changing branch or code edits) and rebuild it from scratch.

Our migrations take about 30 seconds to run. So per the doco here https://pytest-django.readthedocs.io/en/latest/database.html#example-work-flow-with-reuse-db-and-create-db I have --reuse-db in pytest.ini. Which is great most of the time but frequently when I switch between branches with migrations or otherwise mess with migrations I will get a complete test suite fail (but slowly and with massive amounts of error output). Then I need to run with --create-db and further that needs to be done separately for both single and multi threaded test runs and I have to remember to take it back off the args for followup runs or I will eat another 30 seconds each time.

It would be very very nice if pytest-django realised that the set of migrations in the db and the migrations on the drive are different and did a rebuild. Even just comparing the file names would be a vast improvement. The migration table also has an applied timestamp, so detecting that the mod time on the file is newer than the applied time might also be possible.

This was spun out from #422

tolomea avatar Oct 12 '20 08:10 tolomea

If anyone is looking for a simple workaround for this, I've found that just wrapping pytest with a script like this works pretty well.

#!/usr/bin/env bash
set -euo pipefail

OLD_MIGRATIONS=$(cat .migration_hash 2> /dev/null || echo "")
NEW_MIGRATIONS=$(python manage.py showmigrations | md5sum)
set -- "$@" "--reuse-db"
if [[ "$OLD_MIGRATIONS" != "$NEW_MIGRATIONS" ]]; then
  echo "Migrations have changed, recreating DBs"
  set -- "$@" "--create-db"
  echo "$NEW_MIGRATIONS" > .migration_hash
fi

pytest "$@"

dmartin avatar Feb 28 '22 22:02 dmartin