From f3c207528e310b9312f71e34f6bad2c48d3bee22 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 6 May 2019 18:42:11 +0200 Subject: [PATCH] general historicization table+function for Postgres --- .../historicization/historicization.xml | 46 +++++++++++++++++++ .../resources/config/liquibase/master.xml | 9 +++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/config/liquibase/historicization/historicization.xml diff --git a/src/main/resources/config/liquibase/historicization/historicization.xml b/src/main/resources/config/liquibase/historicization/historicization.xml new file mode 100644 index 00000000..9f5f515d --- /dev/null +++ b/src/main/resources/config/liquibase/historicization/historicization.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + CREATE OR REPLACE FUNCTION historicize() + RETURNS trigger + AS $$ + BEGIN + IF (TG_OP = 'INSERT') OR (TG_OP = 'UPDATE') THEN + EXECUTE 'INSERT INTO history VALUES (txid_current(), now()) ON CONFLICT DO NOTHING'; + EXECUTE format('INSERT INTO %I_history VALUES (DEFAULT, txid_current(), False, $1.*)', TG_TABLE_NAME) USING NEW; + RETURN NEW; + ELSE -- assuming TG_OP = 'DELETE' + EXECUTE 'INSERT INTO history VALUES (txid_current(), now()) ON CONFLICT DO NOTHING'; + EXECUTE format('INSERT INTO %I_history VALUES (DEFAULT, txid_current(), True, $1.*)', TG_TABLE_NAME) USING OLD; + RETURN OLD; + END IF; + END; + $$ + LANGUAGE plpgsql; + + + + + diff --git a/src/main/resources/config/liquibase/master.xml b/src/main/resources/config/liquibase/master.xml index c4daff8a..9842d7a3 100644 --- a/src/main/resources/config/liquibase/master.xml +++ b/src/main/resources/config/liquibase/master.xml @@ -3,6 +3,8 @@ xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"> + + @@ -11,6 +13,8 @@ + + @@ -18,7 +22,10 @@ - + + + +