Skip to content

Conversation

@carddev81
Copy link
Contributor

Description of the change

This PR implements standardized database level audit tracking across the UnlockEdv2 backend. Audit fields (create_user_id and update_user_id) are now included in all relevant models via the existing DatabaseFields struct, and are populated using GORM hooks when necessary.

Key changes

  • Added audit columns (create_user_id, update_user_id) via database migration
  • Updated DatabaseFields struct to include audit fields
  • Implemented GORM BeforeCreate and BeforeUpdate hooks for automatic field population
  • Added indexes for audit columns

NOTE: All user persisted records will be stamped with a user id (on delete, on update, on create). Please feel free to contact me when reviewing to give you tips of how to test the functionality.

Additional context

This implementation represents phase 1 of our auditing initiative. In phase 2, we can add an audit_log table with retention rules to capture more detailed event histories. This will further strengthen compliance, traceability, and reporting capabilities across the platform--we will discuss as a team about this.

@carddev81 carddev81 requested a review from a team as a code owner January 12, 2026 15:44
@carddev81 carddev81 requested review from CK-7vn and removed request for a team January 12, 2026 15:44
Copy link
Member

@CK-7vn CK-7vn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, just 2 things i was unsure of, that i could be missing something on, but I think this is a great start. Obviously when we get deeper into auditable actions I don't know if it'll be worth creating a separate audit object that we can embed into our models, but, definitely an awesome start.

@carddev81 carddev81 requested a review from corypride January 13, 2026 03:11
@carddev81 carddev81 requested review from calisio and removed request for corypride January 21, 2026 17:11
Copy link
Contributor

@calisio calisio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one small comment on helper functions-rest looks great!

Comment on lines 57 to 62
updates := map[string]any{
"deleted_at": time.Now(),
}
if userID, ok := db.Statement.Context.Value(models.UserIDKey).(uint); ok {
updates["update_user_id"] = userID
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i keep seeing that we are doing this same action repeatedly in each function that updates the database. is there a better way to do this so we don't repeat code this frequently? I understand why we can't do it in a gorm callback; could we extract this into a helper function? Just ideas as we are talking about keeping clean code. Feel free to ignore if you don't feel its necessary. (lines 60-62)

@carddev81 carddev81 merged commit fb97c24 into main Jan 22, 2026
10 checks passed
@carddev81 carddev81 deleted the carddev81/ticket_id499_dbaudit-v3 branch January 22, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants