-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Leverage database triggers to generate object change records #14059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
one thing to consider. It's currently possible to have models that don't have changelog entries. I recently ended up making an internal plugin for documenting "last seen" timestamps for inventory items. we did not want changelogs created every time we wanted to update the "last seen" time so a custom field was not an option, instead we made a plugin with a relationship model to inventory items insert /update last seen timestamps there. I would hope by going down this path we can have some kind of flag on models where changelogs should or should not be created |
It sounds like currently you are ommitting the ObjectChange mixin. If I am understanding the proposal, the ObjectChange mixin will still exist but now it will instead generate the triggers for each table that needs to be change logged instead of having the middleware intercept the request and process it. |
correct
Hopefully, otherwise ill have ~15k changelogs every night when airflow inventorys our ssd's and update the last_seen objects |
After digging further into this, I'm not sure it's feasible. All the logic for serializing object representations exists in Python; migrating this to a native database function would be challenging if not impossible. It would also preclude models from overriding their own serialization logic. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide. |
We might revisit this idea in the future, but currently it does not seem feasible without undue effort. |
NetBox version
v3.6.4
Feature type
Change to existing functionality
Proposed functionality
NetBox currently records the creation, modification, and deletion of objects using application-level logic; namely the
change_logging()
context manager and receiver functions for Django'spost_save
andpost_delete
signals. This FR proposes replacing this logic with PostgreSQL triggers usingdjango-pgtrigger
. (This is similar in nature to howdjango-pghistory
works, however we'll keep our general-purpose ObjectChange model in place.)At a high level, this will entail the following:
handle_changed_object()
andhandle_deleted_object()
signal receivers. (The receivers themselves must remain to handle webhooks and metrics.)django-pghistory
's context manager to expose request details to the database.ChangeLoggingMixin
to effect the automatic creation of a new changelog record whenever an object is created, modified, or deleted.While much more exploration is needed, I believe all the necessary components have been identified.
Use case
Moving the change logging logic from the Python application layer to the database layer has several major benefits:
change_logging()
context manager when working in the Python shellDatabase changes
While this implementation would obviously effect the creation of PostgreSQL triggers, I don't predict that any substantial schema changes to existing models will be necessary.
External dependencies
django-pgtrigger
django-pghistory
(potentially)The text was updated successfully, but these errors were encountered: