From 965866dadc4f2d6bc85cabb7e586c0409a826163 Mon Sep 17 00:00:00 2001 From: Timotheus Pokorra Date: Fri, 9 May 2025 11:29:51 +0200 Subject: [PATCH] TP-202505-postgres_notify_events (#177) Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/177 Reviewed-by: Michael Hoennig --- .../resources/db/changelog/0-base/020-audit-log.sql | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/resources/db/changelog/0-base/020-audit-log.sql b/src/main/resources/db/changelog/0-base/020-audit-log.sql index 569d8826..2603f131 100644 --- a/src/main/resources/db/changelog/0-base/020-audit-log.sql +++ b/src/main/resources/db/changelog/0-base/020-audit-log.sql @@ -66,7 +66,7 @@ select txc.*, txj.targettable, txj.targetop, txj.targetuuid, txj.targetdelta --// -- ============================================================================ ---changeset michael.hoennig:audit-TX-JOURNAL-TRIGGER endDelimiter:--// +--changeset michael.hoennig:audit-TX-JOURNAL-TRIGGER runOnChange:true validCheckSum:ANY endDelimiter:--// -- ---------------------------------------------------------------------------- /* Trigger function for transaction audit journal. @@ -78,16 +78,24 @@ declare curTask text; curTxId xid8; tableSchemaAndName text; + next_timestamp timestamp; begin curTask := base.currentTask(); curTxId := pg_current_xact_id(); tableSchemaAndName := base.combine_table_schema_and_name(tg_table_schema, tg_table_name); + next_timestamp = '1970-01-01'; insert into base.tx_context (txId, txTimestamp, currentSubject, assumedRoles, currentTask, currentRequest) values ( curTxId, now(), base.currentSubject(), base.assumedRoles(), curTask, base.currentRequest()) - on conflict do nothing; + on conflict do nothing + returning txTimestamp into next_timestamp; + + -- only if a row was inserted, a notification should be send + if next_timestamp <> '1970-01-01' then + PERFORM pg_notify ('tx_context_inserted', CONCAT(curTxId, ',', extract(epoch from next_timestamp), ',', curTask)); + end if; case tg_op when 'INSERT' then insert