jazzband / django-auditlog

A Django app that keeps a log of changes made to an object.
MIT License
1.12k stars 409 forks source link

Errors with Celery and Django-Tenants #439

Open stonesoft-tech opened 1 year ago

stonesoft-tech commented 1 year ago

I am using this with Celery and Django-tenants, however changes made to a tracked model inside a celery task fail with the following error (from Celery):

ProgrammingError('relation "auditlog_logentry" does not exist\nLINE 1: INSERT INTO "auditlog_logentry"

I suspect this is because it is trying to update the auditlog table in the 'public' schema.

For other database related tasks performed in a Celery task, I have to use method supplied with django-tenants:

with schema_context(schema_name):
    # All commands here are ran under the schema `schema_name`

Is there a way that I can have the auditlog database updates respect the correct schema when invoked from a Celery task? similar to the: with set_actor(actor): there coud be

with set_schema(schema_name):

Thanks!

hramezani commented 1 year ago

I am not very familiar with Django-tenants. Do you have multiple auditlog_logentry tables? one for each tenant? or one auditlog_logentry for the whole application?

stonesoft-tech commented 1 year ago

Thanks for the quick response! The database consists of a postgres schema for each tenant, and each schema contains all of the tables specific to that tenant. So one auditlog_logentry table for each tenant. The schema is usually derived from the hostname, eg. tenant1.webapp.com, tenant2.webapp.com. But in the case of celery tasks, which do not come from a 'request', the schema must be specified.

stonesoft-tech commented 1 year ago

I should add, there is also a 'public' schema, which is one that can be accessed by all tenants. If no schema is specified it is this schema that is used. And this one does not have the auditlog_entrytable.

hramezani commented 1 year ago

Thanks for your update. There is no schema change support in django-auditlog and you have to use set_schema context manager.

aleh-rymasheuski commented 1 year ago

Given that (a) django-auditlog doesn't handle the celery tasks itself and (b) it doesn't imply using django-tenants or other multi-tenant / multi-schema tools, the theoretical set_schema context manager shouldn't live in django-auditlog code base.