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.
This commit is contained in:
Gibheer 2021-05-21 21:07:08 +02:00
parent 25e2046e78
commit 9de4163d22
1 changed files with 7 additions and 0 deletions

View File

@ -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'),