From 385dddef4c210bf8676fe942aee02a3fa63c2fb5 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Fri, 13 Sep 2024 14:19:12 +0200 Subject: [PATCH] fix trigger order --- .../resources/db/changelog/0-basis/020-audit-log.sql | 5 +++-- .../resources/db/changelog/0-basis/030-historization.sql | 9 +++++---- .../hsadminng/rbac/context/ContextBasedTest.java | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/resources/db/changelog/0-basis/020-audit-log.sql b/src/main/resources/db/changelog/0-basis/020-audit-log.sql index b1835fff..7ac3f790 100644 --- a/src/main/resources/db/changelog/0-basis/020-audit-log.sql +++ b/src/main/resources/db/changelog/0-basis/020-audit-log.sql @@ -25,7 +25,7 @@ create table basis.tx_context ( txId xid8 primary key not null, txTimestamp timestamp not null, - currentSubject varchar(63) not null, -- not the uuid, because users can be deleted + currentSubject varchar(63) not null, -- not the uuid, because users can be deleted assumedRoles varchar(1023) not null, -- not the uuids, because roles can be deleted currentTask varchar(127) not null, currentRequest text not null @@ -123,7 +123,8 @@ declare begin targetTable := lower(targetTable); - createTriggerSQL = 'CREATE TRIGGER tx_journal_tg' || + -- "-0-" to put the trigger execution before any alphabetically greater tx-triggers + createTriggerSQL = 'CREATE TRIGGER tx_0_journal_tg' || ' AFTER INSERT OR UPDATE OR DELETE ON ' || targetTable || ' FOR EACH ROW EXECUTE PROCEDURE basis.tx_journal_trigger()'; execute createTriggerSQL; diff --git a/src/main/resources/db/changelog/0-basis/030-historization.sql b/src/main/resources/db/changelog/0-basis/030-historization.sql index dd523006..cc200acc 100644 --- a/src/main/resources/db/changelog/0-basis/030-historization.sql +++ b/src/main/resources/db/changelog/0-basis/030-historization.sql @@ -12,14 +12,14 @@ declare historicalTxId xid8; historicalTimestamp timestamp; begin - select coalesce(current_setting('hsadminng.basis.tx_history_txid', true), '') into historicalTxIdSetting; + select coalesce(current_setting('hsadminng.tx_history_txid', true), '') into historicalTxIdSetting; select coalesce(current_setting('hsadminng.tx_history_timestamp', true), '') into historicalTimestampSetting; if historicalTxIdSetting > '' and historicalTimestampSetting > '' then - raise exception 'either hsadminng.basis.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are set: (%, %)', + raise exception 'either hsadminng.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are set: (%, %)', historicalTxIdSetting, historicalTimestampSetting; end if; if historicalTxIdSetting = '' and historicalTimestampSetting = '' then - raise exception 'either hsadminng.basis.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are unset or empty: (%, %)', + raise exception 'either hsadminng.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are unset or empty: (%, %)', historicalTxIdSetting, historicalTimestampSetting; end if; -- just for debugging / making sure the function is only called once per query @@ -150,7 +150,8 @@ begin raise notice 'sql: %', createViewSQL; execute createViewSQL; - createTriggerSQL = 'CREATE TRIGGER ' || baseTable || '_tx_historicize_tg' || + -- "-9-" to put the trigger execution after any alphabetically lesser tx-triggers + createTriggerSQL = 'CREATE TRIGGER tx_9_historicize_tg' || ' AFTER INSERT OR DELETE OR UPDATE ON ' || baseTable || ' FOR EACH ROW EXECUTE PROCEDURE tx_historicize_tf()'; raise notice 'sql: %', createTriggerSQL; diff --git a/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java b/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java index dbe873c3..69a6fae1 100644 --- a/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java +++ b/src/test/java/net/hostsharing/hsadminng/rbac/context/ContextBasedTest.java @@ -52,7 +52,7 @@ public abstract class ContextBasedTest { protected void historicalContext(final Long txId) { // set local cannot be used with query parameters em.createNativeQuery(""" - set local hsadminng.basis.tx_history_txid to ':txid'; + set local hsadminng.tx_history_txid to ':txid'; """.replace(":txid", txId.toString())).executeUpdate(); em.createNativeQuery(""" set local hsadminng.tx_history_timestamp to ''; @@ -66,7 +66,7 @@ public abstract class ContextBasedTest { set local hsadminng.tx_history_timestamp to ':timestamp'; """.replace(":timestamp", txTimestamp.toString())).executeUpdate(); em.createNativeQuery(""" - set local hsadminng.basis.tx_history_txid to ''; + set local hsadminng.tx_history_txid to ''; """).executeUpdate(); }