From be4a8747f116b988f48d400513ffad174035a5a4 Mon Sep 17 00:00:00 2001 From: parauliya Date: Mon, 21 Sep 2020 21:25:41 +0530 Subject: [PATCH] Added trigger for writing event in events table Signed-off-by: parauliya --- deploy/db/tinkerbell-init.sql | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/deploy/db/tinkerbell-init.sql b/deploy/db/tinkerbell-init.sql index 3201ee3c2..93874903a 100644 --- a/deploy/db/tinkerbell-init.sql +++ b/deploy/db/tinkerbell-init.sql @@ -75,8 +75,10 @@ CREATE TABLE IF NOT EXISTS workflow_data ( , data JSONB ); +CREATE EXTENSION "uuid-ossp"; + CREATE TABLE IF NOT EXISTS events ( - id UUID UNIQUE NOT NULL primary key + id UUID UNIQUE DEFAULT uuid_generate_v4() , resource_id UUID NOT NULL , resource_type int NOT NULL , event_type int NOT NULL @@ -99,4 +101,18 @@ $$ LANGUAGE plpgsql; CREATE TRIGGER events_channel AFTER INSERT ON events -FOR EACH ROW EXECUTE PROCEDURE notify_event_changes() +FOR EACH ROW EXECUTE PROCEDURE notify_event_changes(); + +CREATE OR REPLACE FUNCTION insert_event() +RETURNS trigger AS $trigger1$ +BEGIN + IF (TG_OP = 'INSERT') THEN + INSERT INTO events(resource_id, resource_type, event_type, created_at, data) VALUES (new.workflow_id, 2, 0, CURRENT_TIMESTAMP, new.action_list); + RETURN NEW; + END IF; +END; +$trigger1$ LANGUAGE plpgsql; + +CREATE TRIGGER workflow_changed +AFTER INSERT ON workflow_state +FOR EACH ROW EXECUTE PROCEDURE insert_event()