changeset 1307:c2df9c848e9d

Record inserts in audit table.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 08 Mar 2017 09:43:46 +0100
parents 902bc2190fec
children db46646375e3
files db_schema/audit.sql
diffstat 1 files changed, 14 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/db_schema/audit.sql	Wed Mar 08 09:41:14 2017 +0100
+++ b/db_schema/audit.sql	Wed Mar 08 09:43:46 2017 +0100
@@ -81,8 +81,8 @@
         RAISE EXCEPTION 'land.if_modified_func() may only run as an AFTER trigger';
     END IF;
 
-    -- Do nothing on insert and delete.
-    IF (TG_OP = 'INSERT' OR TG_OP = 'DELETE') THEN
+    -- Do nothing on delete.
+    IF (TG_OP = 'DELETE') THEN
         RETURN NULL;
     END IF;
 
@@ -91,7 +91,7 @@
         TG_TABLE_NAME::varchar,             -- table_name
         current_timestamp,                  -- tstamp
         substring(TG_OP,1,1),               -- action
-        OLD.id,                             -- object_id
+        NEW.id,                             -- object_id
         NULL, NULL                          -- row_data, changed_fields
         );
 
@@ -106,6 +106,9 @@
             -- All changed fields are ignored. Skip this update.
             RETURN NULL;
         END IF;
+    ELSIF (TG_OP = 'INSERT' AND TG_LEVEL = 'ROW') THEN
+        audit_row.row_data = row_to_json(NEW)::JSONB - excluded_cols;
+        audit_row.changed_fields = jsonb_strip_nulls(row_to_json(NEW)::JSONB - excluded_cols);
     ELSE
         RAISE EXCEPTION '[land.if_modified_func] - Trigger func added as trigger for unhandled case: %, %',TG_OP, TG_LEVEL;
         RETURN NULL;
@@ -186,6 +189,7 @@
 SELECT
     id,
     table_name,
+    action,
     object_id,
     tstamp,
     cast(row_data ->> 'messungs_id' AS integer) AS messungs_id,
@@ -210,12 +214,12 @@
 FROM audit_trail;
 
 
-SELECT audit_table('probe', true, false, '{tree_modified, letzte_aenderung}'::text[]);
-SELECT audit_table('messung', true, false, '{tree_modified, letzte_aenderung, status}'::text[]);
-SELECT audit_table('messwert', true, false, '{tree_modified, letzte_aenderung}'::text[]);
-SELECT audit_table('kommentar_p', true, false, '{tree_modified, letzte_aenderung}'::text[]);
-SELECT audit_table('kommentar_m', true, false, '{tree_modified, letzte_aenderung}'::text[]);
-SELECT audit_table('zusatz_wert', true, false, '{tree_modified, letzte_aenderung}'::text[]);
-SELECT audit_table('ortszuordnung', true, false, '{tree_modified, letzte_aenderung}'::text[]);
+SELECT audit_table('probe', true, false, '{id, tree_modified, letzte_aenderung}'::text[]);
+SELECT audit_table('messung', true, false, '{id,, tree_modified, letzte_aenderung, status}'::text[]);
+SELECT audit_table('messwert', true, false, '{id, tree_modified, letzte_aenderung}'::text[]);
+SELECT audit_table('kommentar_p', true, false, '{id, tree_modified, letzte_aenderung}'::text[]);
+SELECT audit_table('kommentar_m', true, false, '{id, tree_modified, letzte_aenderung}'::text[]);
+SELECT audit_table('zusatz_wert', true, false, '{id, tree_modified, letzte_aenderung}'::text[]);
+SELECT audit_table('ortszuordnung', true, false, '{id, tree_modified, letzte_aenderung}'::text[]);
 
 SET search_path TO public;
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)