pytest-dev / pytest-dev/pytest-django

When running pytest on a database with --reuse-db where python manage.py migrate was already run, cause failure on old runpython

Open
#756 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

I explain myself,

I have a project with a lot of migrations, with 3 important migrations for our case (in that order):

  1. a migration that add the field foo to the model Bar
  2. a migration that has a RunPython, doing some computation based on the field foo
  3. a migration that remove the field foo

now if i first do

python manage.py migrate

I finish with a database without field foo => normal and expected

if I then run pytest --reuse-db I will got SQL failure because pytest re-runs all migrations, but without actually replaying the changes on the database , but only running the RunPython, so the RunPython of 2) is run on database were the field has already been removed

is it a known behaviour , if so why ?

I would have thought that it would see migrations has been run , and will not execute anything, runpython included

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the sequence with python manage.py migrate followed by pytest --reuse-db, focusing on the migrations containing RunPython and the removed foo field. Trace how pytest-django determines which migrations to apply and verify whether the database state is expected; done means the behavior is explained or corrected with a regression test for this sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, databases, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.