Replies: 1 comment 1 reply
-
Your prototype is neat. I'm trying to understand the last insert id problem with postgres. You want to see the Or is it that LastInsertID isn't being correctly correlated to a query in mysql driver because it's a separate query? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Model methods like Insert(), Update(), and Delete() can be extended to save record changes that includes certain metadata like who is making the change, previous state, and current state. Similar to https://github.com/owen-it/laravel-auditing package for Laravel. From my search there is no single active library that can do this for Go ecosystem.
I have a very early prototype that works with
database/sql
which uses hooks to capture these information, plus user's IP, user agent etc.. User ID is captured simply withctx = context.WithValue(ctx, middleware.UserID, {{give user id here}})
in an Auth middleware. It works well with MySQL. Resulting record to anaudit
table looks like this:However, it cannot capture LastInsertID() for postgres because of driver limitation.
I realize that we can use
ctx
to save these audit details. We can retrieveactor_id
- taken fromAuth
middlewaretable_row_id
- taken frommodel.ID
afterQueryRowContext()
is performed.table_name
- already availableaction
- depends on whether you are doingInsert()
,Update()
, etcold_values
- Relevant forUpdate()
andDelete()
- extra SQL query need to be performed to query the record before update/delete is performednew_values
- Relevant forInsert()
,Update()
http_method
,url
,ip _address
,user_agent
- taken from a middlewarecreated_at
- A simpletime.Now()
Performance will be degraded because there are extra sql queries being made. So, audit must be optional.
Implementation seems viable. We only need to figure out when is the best time to perform those additional sql queries.
Beta Was this translation helpful? Give feedback.
All reactions