feat(db): add audit log for database changes #5703
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces an audit log table to track changes within the database. Note: as part of this implementation, we have refactored the
Base
class by moving it fromdatabase.core
to a new file,database.base
, to resolve a circular import issue with theAudit
class.Details:
Audit Log Implementation:
track_changes
function and the use of SQLAlchemy's event system.track_changes
function is registered as an event listener for SQLAlchemy's "before_flush" event, ensuring that changes are logged before they are committed to the database.Refactoring:
Base
class has been moved todatabase.base
to eliminate a circular import issue caused by theAudit
class, which subclassesBase
.Code Requirements:
views.py
file must include the addition of the.user
attribute to thedb_session
. This is necessary to associate changes with the current user. For example:current_user
, the parameter list in eachviews.py
function must be updated to include this line before any database processing:CurrentUser
tracked_classes
list insrc/dispatch/database/core.py
Impact:
Base
class should not affect existing functionality but will improve the code's modularity and maintainability.Testing:
track_changes
mechanism.