Skip to content

Record transaction time for CRUD events that occur together as part of a transaction #331

@lccc-andy-buchanan

Description

@lccc-andy-buchanan

I have a Django app where a user action triggers several updates to happen together within a transaction. These get written to separate rows in the easyaudit crudevent table.

When inspecting the crudevents, I had hoped to be able to determine that these updates happened together in the same transaction. The timestamps are obviously very close together, but this is not reliable enough for a generic solution. The timestamp for the crudevent is being determined by calling timezone.now(), which is different for each event. https://github.com/soynatan/django-easy-audit/blob/master/easyaudit/signals/crud_flows.py#L44

My suggestion is to use django.contrib.postgres.functions.TransactionNow, which uses the database server's view of the current transaction time: https://docs.djangoproject.com/en/5.2/ref/contrib/postgres/functions/#django.contrib.postgres.functions.TransactionNow

My first thought was just to replace the datetime field with this, but given that this is specific to the postgres backend, and it might still be useful to have the separate event/statement times, I propose adding a new column transaction_datetime to the crudevent model and populating it using TransactionNow() if a certain config parameter is set.

Does this sound worth implementing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions