From 9de4163d229932eef775c35610351e868d523908 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Fri, 21 May 2021 21:07:08 +0200 Subject: [PATCH] fix the modified_by and modified_at columns As all changes are already written into history, why not enforce the proper changing of modified_at and modified_by columns? This way we can be sure these are properly changed and don't have to take care of that in the SQL statements anymore. --- schema/01_initial.sql | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/schema/01_initial.sql b/schema/01_initial.sql index 7505332..d5b20c8 100644 --- a/schema/01_initial.sql +++ b/schema/01_initial.sql @@ -155,6 +155,13 @@ comment on column history.action is 'The action can be one of insert, update or create or replace function record() returns trigger as $$ begin + if TG_OP = 'UPDATE' or TG_OP = 'INSERT' then + NEW.modified_at = now(); + NEW.modified_by = current_setting('dim.username'); + end if; + if TG_OP = 'INSERT' then + NEW.created_by = current_setting('dim.username'); + end if; insert into history(transaction, created_by, type, action, old_entity, new_entity) values ( current_setting('dim.transaction'),