简体   繁体   中英

pytest-django run with migrations ignores database triggers

My Django app depends on a database with some triggers setup. I use this part of the documentation to set up the triggers in the test database for the pytest runner.

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        cur = connection.cursor()
        cur.execute([...])  # Set it up

I run my tests with --nomigrations and it works as expected. Without --nomigrations (test runs migrations first), the triggers are not working.

So trying to debug this, I've confirmed

  1. The fixture IS run, so the triggers SHOULD be setup
  2. Pausing execution in the debugger at the start of my tests, I can confirm that the triggers ARE created and present in the test database (by running psql test_<mydb> and looking in the pg_trigger table)
  3. Pausing execution inside my fixture, I can confirm that migrations are run before the fixture. So the migrations may setup the triggers for me, and they may do it incorrectly, but the fixture will drop all the triggers and recreate them
  4. Removing the fixture and running with migrations provide no new results. So there is no reason to think that the fixture is the problem. It seems to be only due to the migrations being run

Let me just stress once more that the tests pass when run without migrations and testing the functionality when running the dev server against my dev db I can also confirm that it works

So, my question is: Is there any reason that running with migrations should do things differently? Or is it likely that my migrations do something obscure which makes things fail, ie it is my own fault?

Placing trigger in django_db_setup worked for me

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        cur = connection.cursor()
        cur.execute('''CREATE TRIGGER search_vector_update BEFORE INSERT OR UPDATE
            ON xml_templates_template FOR EACH ROW EXECUTE PROCEDURE
            tsvector_update_trigger(search_vector, 'pg_catalog.english', name, description, info);
        ''')

pytest (4.4.1) is run with --nomigrations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM