The title nearly says it all. I would like to write a trigger that:
- Uses a table called "audit_trail" with fields
table_name
,by
,timestamp
,operation
,contents
wherecontents
is in JSON format - The trigger listens for
update
orinsert
on each table - If the table has a column called
last_modified_by
, then:- Make up a JSON version of the record updated/inserted
- Add a record to the
audit_trail
table, with all relevant fields includingcontents
which would have the JSON representation of the record updated/inserted
Is this technically possible with MySql? I really don't want to code this into the application itself, as it would be messy.
Please note that I am fully aware about the limitation about recording this info as JSON (hard to query, etc.). The only requirement my app has is that an admin must be able to see the "history" of a record, of when/who modified it.
While this is quite trivial, there are things I just cannot work out:
Things I can't work out:
- How do you write a trigger that will get triggered on
insert
orupdate
on ANY table - How to get the JSON version of a record
- How to get the trigger to store the JSON onto the
contents
column
Ideas?