Skip to content
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

feat(db): add audit log for database changes #5703

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

whitdog47
Copy link
Contributor

@whitdog47 whitdog47 commented Jan 17, 2025

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 from database.core to a new file, database.base, to resolve a circular import issue with the Audit class.

Details:

  • Audit Log Implementation:

    • A new audit log table has been created to record changes to specified tables. This is achieved through the track_changes function and the use of SQLAlchemy's event system.
    • The 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.
    • Only updates to the specified list of objects are tracked, allowing us to focus on changes that matter most.
  • Refactoring:

    • The Base class has been moved to database.base to eliminate a circular import issue caused by the Audit class, which subclasses Base.
    • All relevant imports have been updated across the codebase to reflect this change.
  • Code Requirements:

    • To enable auditing, each views.py file must include the addition of the .user attribute to the db_session. This is necessary to associate changes with the current user. For example:
      db_session.user = current_user
    • To get the current_user, the parameter list in each views.py function must be updated to include this line before any database processing:
      current_user: CurrentUser
      This ensures that the current user is passed to the function and can be used for auditing purposes. It may also be necessary add this import for CurrentUser
      from dispatch.auth.service import CurrentUser
    • Lastly, the class name needs to be added to the tracked_classes list in src/dispatch/database/core.py

Impact:

  • The introduction of the audit log enhances the system's ability to track and review changes, improving accountability and traceability.
  • The refactoring of the Base class should not affect existing functionality but will improve the code's modularity and maintainability.

Testing:

  • Existing tests have been run to ensure no functionality is broken.
  • New tests have been added to verify the auditing functionality and ensure the correct operation of the track_changes mechanism.

@whitdog47 whitdog47 added the enhancement New feature or request label Jan 17, 2025
@whitdog47 whitdog47 requested a review from snkilmartin January 17, 2025 20:33
@whitdog47 whitdog47 self-assigned this Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant