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

Issues upgrading from 2.8.0 to 3.1.0

Open leohahn opened this issue 10 months ago • 2 comments

Hey, just decided to upgrade the dependency on our project and I'm having some issues with our tests. This is the error essentially:

django.db.utils.ProgrammingError: function _pgh_attach_context() does not exist

The issue is that the function is not created on the test database: https://github.com/Opus10/django-pghistory/blob/master/pghistory/models.py#L25.

It seems that this function is created during the migration step, however, I have disabled migrations for running tests on our database, as it makes CI tests run significantly faster:

DATABASES["default"]["TEST"] = {
    "MIGRATE": False,
}

So tests now break because the function is not present. I can make tests work again by calling the install method Context.install_pgh_attach_context_func() before each test, but that's really cumbersome. The weird part about this is that before this issue was not happening. I managed to track down that the problem happens whenever I upgrade django-pgtrigger from 4.7.0 to 4.11.0. More specifically, the issue happens in 4.8.0. Now, this maybe seems an issue the the django-pgtrigger library, but at the same time I really can't understand why our tests were working before but now they're not working. The pgtrigger dependency does not know about the _pgh_attach_context function, so I'm trying to understand why this is happening in the first place. Maybe the code before checked for the existence of the function and did nothing if it didn't exist?

Any guesses here? 🤔

leohahn avatar Apr 05 '24 19:04 leohahn

I had the same issue with 3.0.1 (same scenario - not migrating the test db for the same reason) and did exactly that Context.install_pgh_attach_context_func() in order to fix it. Instead of doing it to every test, I just applied it to the base test class where most of our tests are inherited from.

vpolimenov avatar May 15 '24 10:05 vpolimenov

Cool, thanks for the context. Yeah, I guess having the base class it's a good workaround. Care to share here you you do that in the code? Do you implement the setUp for the class and inside it you call the function?

leohahn avatar May 16 '24 19:05 leohahn

Check out the PGHISTORY_INSTALL_CONTEXT_FUNC_ON_MIGRATE setting, which was added in 3.3.

In your test settings file, do PGHISTORY_INSTALL_CONTEXT_FUNC_ON_MIGRATE=True. This will register a hook to install the functions.

Note that it's safe to have this turned on it production too. It will just result in a redundant command to try to create or replace the pghistory context tracking function after migrations are finished

wesleykendall avatar Sep 02 '24 19:09 wesleykendall