django-test-migrations icon indicating copy to clipboard operation
django-test-migrations copied to clipboard

use run_syncdb in reset

Open xi opened this issue 3 years ago • 2 comments

In my project I have two databases. When testing a migration I got the following error:

Database queries to 'secondary' are not allowed in this test. Add 'secondary' to pytest_django.fixtures._django_db_helper.<locals>.PytestDjangoTestCase.databases to ensure proper test isolation and silence this failure.

This was odd because the test did not use the secondary database at all. By inspecting the stack trace I found that migrator.reset() executes all migrations, and there is actually an unrelated migration that uses the secondary database.

I think migrator.reset() should not execute all migrations. Therefore it seems like a good idea to use the --run-syncdb option. (I am not perfectly sure whether I got the syntax right)

xi avatar Jan 17 '22 19:01 xi

@xi can you please think of any simple test cases to reproduce this? 🤔

sobolevn avatar Jan 17 '22 19:01 sobolevn

I am having trouble with the test case. I was trying to add a new migration that fails for some reason and then verify that this migration is not executed when calling migrator.reset(). I was not sure how to do that without breaking a lot of other tests though. My second attempt was to patch django.db.migrations.RunPython but that I realized that the migrations are executed by a separate interpreter so that my mock is not actually used. Do you have any ideas how to do this?

xi avatar Jan 17 '22 19:01 xi