add in the org setting history triggers
authorJason Etheridge <jason@esilibrary.com>
Thu, 15 Sep 2011 16:05:14 +0000 (12:05 -0400)
committerJason Etheridge <jason@esilibrary.com>
Thu, 15 Sep 2011 16:18:20 +0000 (12:18 -0400)
Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.YAOUS-log-table.sql

index 4ce1c9d..0230a76 100644 (file)
@@ -12,4 +12,39 @@ CREATE TABLE config.org_unit_setting_type_log (
     field_name      TEXT      REFERENCES config.org_unit_setting_type (name)
 );
 
+-- Log each change in oust to oustl, so admins can see what they messed up if someting stops working.
+CREATE OR REPLACE FUNCTION ous_change_log() RETURNS TRIGGER AS $ous_change_log$
+    DECLARE
+    original TEXT;
+    BEGIN
+        -- Check for which setting is being updated, and log it.
+        SELECT INTO original value FROM actor.org_unit_setting WHERE name = NEW.name AND org_unit = NEW.org_unit;
+                
+        INSERT INTO config.org_unit_setting_type_log (org,original_value,new_value,field_name) VALUES (NEW.org_unit, original, NEW.value, NEW.name);
+        
+        RETURN NEW;
+    END;
+$ous_change_log$ LANGUAGE plpgsql;    
+
+CREATE TRIGGER log_ous_change
+    BEFORE INSERT OR UPDATE ON actor.org_unit_setting
+    FOR EACH ROW EXECUTE PROCEDURE ous_change_log();
+
+CREATE OR REPLACE FUNCTION ous_delete_log() RETURNS TRIGGER AS $ous_delete_log$
+    DECLARE
+    original TEXT;
+    BEGIN
+        -- Check for which setting is being updated, and log it.
+        SELECT INTO original value FROM actor.org_unit_setting WHERE name = OLD.name AND org_unit = OLD.org_unit;
+                
+        INSERT INTO config.org_unit_setting_type_log (org,original_value,new_value,field_name) VALUES (OLD.org_unit, original, 'null', OLD.name);
+        
+        RETURN OLD;
+    END;
+$ous_delete_log$ LANGUAGE plpgsql;    
+
+CREATE TRIGGER log_ous_del
+    BEFORE DELETE ON actor.org_unit_setting
+    FOR EACH ROW EXECUTE PROCEDURE ous_delete_log();
+
 COMMIT;