basis.tx_history_txid(), basis.tx_operation, basis.tx_create_historicization(...)

This commit is contained in:
Michael Hoennig 2024-09-13 08:56:27 +02:00
parent 5f00a093e4
commit a7037c4306
5 changed files with 14 additions and 14 deletions

View File

@ -3,7 +3,7 @@
-- ============================================================================ -- ============================================================================
--changeset hs-global-historization-tx-history-txid:1 endDelimiter:--// --changeset hs-global-historization-tx-history-txid:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
create or replace function tx_history_txid() create or replace function basis.tx_history_txid()
returns xid8 stable returns xid8 stable
language plpgsql as $$ language plpgsql as $$
declare declare
@ -12,18 +12,18 @@ declare
historicalTxId xid8; historicalTxId xid8;
historicalTimestamp timestamp; historicalTimestamp timestamp;
begin begin
select coalesce(current_setting('hsadminng.tx_history_txid', true), '') into historicalTxIdSetting; select coalesce(current_setting('hsadminng.basis.tx_history_txid', true), '') into historicalTxIdSetting;
select coalesce(current_setting('hsadminng.tx_history_timestamp', true), '') into historicalTimestampSetting; select coalesce(current_setting('hsadminng.tx_history_timestamp', true), '') into historicalTimestampSetting;
if historicalTxIdSetting > '' and historicalTimestampSetting > '' then if historicalTxIdSetting > '' and historicalTimestampSetting > '' then
raise exception 'either hsadminng.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are set: (%, %)', raise exception 'either hsadminng.basis.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are set: (%, %)',
historicalTxIdSetting, historicalTimestampSetting; historicalTxIdSetting, historicalTimestampSetting;
end if; end if;
if historicalTxIdSetting = '' and historicalTimestampSetting = '' then if historicalTxIdSetting = '' and historicalTimestampSetting = '' then
raise exception 'either hsadminng.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are unset or empty: (%, %)', raise exception 'either hsadminng.basis.tx_history_txid or hsadminng.tx_history_timestamp must be set, but both are unset or empty: (%, %)',
historicalTxIdSetting, historicalTimestampSetting; historicalTxIdSetting, historicalTimestampSetting;
end if; end if;
-- just for debugging / making sure the function is only called once per query -- just for debugging / making sure the function is only called once per query
-- raise notice 'tx_history_txid() called with: (%, %)', historicalTxIdSetting, historicalTimestampSetting; -- raise notice 'basis.tx_history_txid() called with: (%, %)', historicalTxIdSetting, historicalTimestampSetting;
if historicalTxIdSetting is null or historicalTxIdSetting = '' then if historicalTxIdSetting is null or historicalTxIdSetting = '' then
select historicalTimestampSetting::timestamp into historicalTimestamp; select historicalTimestampSetting::timestamp into historicalTimestamp;
@ -40,7 +40,7 @@ end; $$;
--changeset hs-global-historization-tx-historicize-tf:1 endDelimiter:--// --changeset hs-global-historization-tx-historicize-tf:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
create type "tx_operation" as enum ('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE'); create type basis.tx_operation as enum ('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE');
create or replace function tx_historicize_tf() create or replace function tx_historicize_tf()
returns trigger returns trigger
@ -95,7 +95,7 @@ end; $$;
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
create or replace procedure tx_create_historicization(baseTable varchar) create or replace procedure basis.tx_create_historicization(baseTable varchar)
language plpgsql as $$ language plpgsql as $$
declare declare
createHistTableSql varchar; createHistTableSql varchar;
@ -111,7 +111,7 @@ begin
'CREATE TABLE ' || baseTable || '_ex (' || 'CREATE TABLE ' || baseTable || '_ex (' ||
' version_id serial PRIMARY KEY,' || ' version_id serial PRIMARY KEY,' ||
' txid xid8 NOT NULL REFERENCES basis.tx_context(txid),' || ' txid xid8 NOT NULL REFERENCES basis.tx_context(txid),' ||
' trigger_op tx_operation NOT NULL,' || ' trigger_op basis.tx_operation NOT NULL,' ||
' alive boolean not null,' || ' alive boolean not null,' ||
' LIKE ' || baseTable || ' LIKE ' || baseTable ||
' EXCLUDING CONSTRAINTS' || ' EXCLUDING CONSTRAINTS' ||
@ -132,7 +132,7 @@ begin
'CREATE OR REPLACE VIEW %1$s AS' || 'CREATE OR REPLACE VIEW %1$s AS' ||
'(' || '(' ||
-- make sure the function is only called once, not for every matching row in basis.tx_context -- make sure the function is only called once, not for every matching row in basis.tx_context
' WITH txh AS (SELECT tx_history_txid() AS txid) ' || ' WITH txh AS (SELECT basis.tx_history_txid() AS txid) ' ||
' SELECT %2$s' || ' SELECT %2$s' ||
' FROM %3$s' || ' FROM %3$s' ||
' WHERE alive = TRUE' || ' WHERE alive = TRUE' ||

View File

@ -25,5 +25,5 @@ call basis.create_journal('hs_booking_project');
-- ============================================================================ -- ============================================================================
--changeset hs-booking-project-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--// --changeset hs-booking-project-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
call tx_create_historicization('hs_booking_project'); call basis.tx_create_historicization('hs_booking_project');
--// --//

View File

@ -42,6 +42,6 @@ call basis.create_journal('hs_booking_item');
-- ============================================================================ -- ============================================================================
--changeset hs-booking-item-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--// --changeset hs-booking-item-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
call tx_create_historicization('hs_booking_item'); call basis.tx_create_historicization('hs_booking_item');
--// --//

View File

@ -173,7 +173,7 @@ call basis.create_journal('hs_hosting_asset');
-- ============================================================================ -- ============================================================================
--changeset hs-hosting-asset-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--// --changeset hs-hosting-asset-MAIN-TABLE-HISTORIZATION:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
call tx_create_historicization('hs_hosting_asset'); call basis.tx_create_historicization('hs_hosting_asset');
--// --//

View File

@ -52,7 +52,7 @@ public abstract class ContextBasedTest {
protected void historicalContext(final Long txId) { protected void historicalContext(final Long txId) {
// set local cannot be used with query parameters // set local cannot be used with query parameters
em.createNativeQuery(""" em.createNativeQuery("""
set local hsadminng.tx_history_txid to ':txid'; set local hsadminng.basis.tx_history_txid to ':txid';
""".replace(":txid", txId.toString())).executeUpdate(); """.replace(":txid", txId.toString())).executeUpdate();
em.createNativeQuery(""" em.createNativeQuery("""
set local hsadminng.tx_history_timestamp to ''; set local hsadminng.tx_history_timestamp to '';
@ -66,7 +66,7 @@ public abstract class ContextBasedTest {
set local hsadminng.tx_history_timestamp to ':timestamp'; set local hsadminng.tx_history_timestamp to ':timestamp';
""".replace(":timestamp", txTimestamp.toString())).executeUpdate(); """.replace(":timestamp", txTimestamp.toString())).executeUpdate();
em.createNativeQuery(""" em.createNativeQuery("""
set local hsadminng.tx_history_txid to ''; set local hsadminng.basis.tx_history_txid to '';
""").executeUpdate(); """).executeUpdate();
} }