Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you think of this approach for logging changes in mysql and have some kind of audit trail

I've been reading through several topics now and did some research about logging changes to a mysql table. First let me explain my situation:

I've a ticket system with a table: 'ticket'

As of now I've created triggers which will enter a duplicate entry in my table: 'ticket_history' which has "action" "user" and "timestamp" as additional columns. After some weeks and testing I'm somewhat not happy with that build since every change is creating a full copy of my row in the history table. I do understand that disk space is cheap and I should not worry about it but in order to retrieve some kind of log or nice looking history for the user is painful, at least for me. Also with the trigger I've written I get a new row in the history even if there is no change. But this is just a design flaw of my trigger!

Here my trigger:

BEFORE UPDATE ON ticket FOR EACH ROW
BEGIN
INSERT INTO ticket_history
SET
    idticket = NEW.idticket,
    time_arrival = NEW.time_arrival,
    idticket_status = NEW.idticket_status,
    tmp_user = NEW.tmp_user,
    action = 'update',
    timestamp = NOW();
END

My new approach in order to avoid having triggers

After spening some time on this topic I came up with an approach I would like to discuss and implement. But first I would have some questions about that:

My idea is to create a new table:

    id   sql_fwd        sql_bwd      keys      values    user       timestamp
    -------------------------------------------------------------------------
    1    UPDATE...      UPDATE...    status    5         14          12345678
    2    UPDATE...      UPDATE...    status    4         7           12345678

The flow would look like this in my mind:

At first I would select something or more from the DB:

SELECT keys FROM ticket;

Then I display the data in 2 input fields:

<input name="key" value="value" /> <input type="hidden" name="key" value="value" />

Hit submit and give it to my function:

I would start with a SELECT again: SELECT * FROM ticket; and make sure that the hidden input field == the value from the latest select. If so I can proceed and know that no other user has changed something in the meanwhile. If the hidden field does not match I bring the user back to the form and display a message.

Next I would build the SQL Queries for the action and also the query to undo those changes.

$sql_fwd = "UPDATE ticket 
            SET idticket_status = 1
            WHERE idticket = '".$c_get['id']."';";

$sql_bwd = "UPDATE ticket 
            SET idticket_status = 0
            WHERE idticket = '".$c_get['id']."';";

Having that I run the UPDATE on ticket and insert a new entry in my new table for logging.

With that I can try to catch possible overwrites while two users are editing the same ticket in the same time and for my history I could simply look up the keys and values and generate some kind of list. Also having the SQL_BWD I simply can undo changes.

My questions to that would be:

  • Would it be noticeable doing an additional select everytime I want to update something?
  • Do I lose some benefits I would have with triggers?
  • Are there any big disadvantages
  • Are there any functions on my mysql server or with php which already do something like that?
  • Or is there might be a much easier way to do something like that
  • Is maybe a slight change to my trigger I've now already enough?
  • If I understad this right MySQL is only performing an update if the value has changed but the trigger is executed anyways right?
  • If I'm able to change the trigger, can I still prevent somehow the overwriting of data while 2 users try to edit the ticket the same time on the mysql server or would I do this anyways with PHP?

Thank you for the help already

like image 626
floGalen Avatar asked Aug 31 '17 09:08

floGalen


People also ask

What is audit log in MySQL?

When installed, the audit plugin enables MySQL Server to produce a log file containing an audit record of server activity. The log contents include when clients connect and disconnect, and what actions they perform while connected, such as which databases and tables they access.

What is audit logs in database?

Audit records provide information about the operation that was audited, the user performing the operation, and the date and time of the operation. Audit records can be stored in either a data dictionary table, called the database audit trail, or in operating system files, called an operating system audit trail.


1 Answers

Another approach...

When a worker starts to make a change...

  1. Store the time and worker_id in the row.
  2. Proceed to do the tasks.
  3. When the worker finishes, fetch the last worker_id that touched the record; if it is himself, all is well. Clear the time and worker_id.

If, on the other hand, another worker slips in, then some resolution is needed. This gets into your concept that some things can proceed in parallel.

  • Comments could be added to a different table, hence no conflict.
  • Changing the priority may not be an issue by itself.
  • Other things may be messier.

It may be better to have another table for the time & worker_ids (& ticket_id). This would allow for flagging that multiple workers are currently touching a single record.

As for History versus Current, I (usually) like to have 2 tables:

  • History -- blow-by-blow list of what changes were made, when, and by whom. This is table is only INSERTed into.
  • Current -- the current status of the ticket. This table is mostly UPDATEd.

Also, I prefer to write the History directly from the "database layer" of the app, not via Triggers. This gives me much better control over the details of what goes into each table and when. Plus the 'transactions' are clear. This gives me confidence that I am keeping the two tables in sync:

BEGIN; INSERT INTO History...; UPDATE Current...; COMMIT;
like image 160
Rick James Avatar answered Sep 30 '22 11:09

Rick James