Created
August 12, 2016 16:30
-
-
Save fogonthedowns/17b9cfe9dcad83a8ab9e77785556f37a to your computer and use it in GitHub Desktop.
psql Trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE OR REPLACE FUNCTION process_inv_audit() RETURNS TRIGGER AS $inv_audit$ | |
| BEGIN | |
| IF (TG_OP = 'DELETE') THEN | |
| INSERT INTO inventory_audit (id, old_column, op, location, notes, amount, original_amount, units, original_units, lot, batch, date_opened, date_expired, plant_material_id, uid, is_active, received_date, order_date, secondary_location, price, country_code, vendor_code, legacy_id, brand, status) VALUES (nextval('inventory_audit_id_seq'), OLD.id, 'D', OLD.location, OLD.notes, OLD.amount, OLD.original_amount, OLD.units, OLD.original_units, OLD.lot, OLD.batch, OLD.date_opened, OLD.date_expired, OLD.plant_material_id, OLD.uid, OLD.is_active, OLD.received_date, OLD.order_date, OLD.secondary_location, OLD.price, OLD.country_code, OLD.vendor_code, OLD.legacy_id, OLD.brand, OLD.status); | |
| RETURN OLD; | |
| ELSIF (TG_OP = 'UPDATE') THEN | |
| INSERT INTO inventory_audit (id, old_column, op, location , notes, amount, original_amount, units, original_units, lot, batch, date_opened, date_expired, plant_material_id, uid, is_active, received_date, order_date, secondary_location, price, country_code, vendor_code, legacy_id, brand, status) VALUES (nextval('inventory_audit_id_seq'), NEW.id, 'U', NEW.location, NEW.notes, NEW.amount, NEW.original_amount, NEW.units, NEW.original_units, NEW.lot, NEW.batch, NEW.date_opened, NEW.date_expired, NEW.plant_material_id, NEW.uid, NEW.is_active, NEW.received_date, NEW.order_date, NEW.secondary_location, NEW.price, NEW.country_code, NEW.vendor_code, NEW.legacy_id, NEW.brand, NEW.status); | |
| RETURN NEW; | |
| ELSIF (TG_OP = 'INSERT') THEN | |
| INSERT INTO inventory_audit (id, old_column, op, location , notes, amount, original_amount, units, original_units, lot, batch, date_opened, date_expired, plant_material_id, uid, is_active, received_date, order_date, secondary_location, price, country_code, vendor_code, legacy_id, brand, status) VALUES (nextval('inventory_audit_id_seq'), NEW.id, 'I', NEW.location, NEW.notes, NEW.amount, NEW.original_amount, NEW.units, NEW.original_units, NEW.lot, NEW.batch, NEW.date_opened, NEW.date_expired, NEW.plant_material_id, NEW.uid, NEW.is_active, NEW.received_date, NEW.order_date, NEW.secondary_location, NEW.price, NEW.country_code, NEW.vendor_code, NEW.legacy_id, NEW.brand, NEW.status); | |
| RETURN NEW; | |
| END IF; | |
| RETURN NULL; | |
| END; | |
| $inv_audit$ LANGUAGE plpgsql; | |
| CREATE TRIGGER inv_audit | |
| AFTER INSERT OR UPDATE OR DELETE ON inventory | |
| FOR EACH ROW EXECUTE PROCEDURE process_inv_audit(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This writes to an Audit Table, on change of Inventory