From 2e0d482b3387d063bd48f2470c8b9d15052a99d8 Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 5 Jun 2023 16:13:48 +0545 Subject: [PATCH] feat: add notification table * add trigger to push any changes on the teams table to the event queue --- schema/incidents.hcl | 35 ++++++++++++++++++++++++++++++++++- views/007_events.sql | 23 ++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/schema/incidents.hcl b/schema/incidents.hcl index de40fbf5..162aa3f3 100644 --- a/schema/incidents.hcl +++ b/schema/incidents.hcl @@ -714,7 +714,6 @@ table "comments" { } } - table "severities" { schema = schema.public column "id" { @@ -734,3 +733,37 @@ table "severities" { type = text } } + +table "notifications" { + schema = schema.public + column "id" { + null = false + type = uuid + default = sql("generate_ulid()") + } + column "team_id" { + null = false + type = uuid + } + column "created_at" { + null = false + type = timestamptz + default = sql("now()") + } + column "deleted_at" { + null = true + type = timestamptz + } + column "config" { + null = false + type = jsonb + } + foreign_key "notifications_team_id_fkey" { + columns = [column.team_id] + ref_columns = [table.teams.column.id] + } + index "notifications_team_id_key" { + unique = false + columns = [column.team_id] + } +} diff --git a/views/007_events.sql b/views/007_events.sql index a19737b4..d2ba90a7 100644 --- a/views/007_events.sql +++ b/views/007_events.sql @@ -57,4 +57,25 @@ WHERE AND attempts > 0 AND created_at >= NOW() - INTERVAL '7 days' GROUP BY - name; \ No newline at end of file + name; + +-- Insert team updates in event_queue +CREATE +OR REPLACE FUNCTION insert_team_in_event_queue () RETURNS TRIGGER AS $$ +BEGIN + IF TG_OP = 'DELETE' THEN + INSERT INTO event_queue(name, properties) VALUES ('team.delete', jsonb_build_object('team_id', OLD.id)); + NOTIFY event_queue_updates, 'update'; + RETURN OLD; + ELSE + INSERT INTO event_queue(name, properties) VALUES ('team.update', jsonb_build_object('team_id', NEW.id)); + NOTIFY event_queue_updates, 'update'; + RETURN NEW; + END IF; +END +$$ LANGUAGE plpgsql; + +CREATE +OR REPLACE TRIGGER team_enqueue +AFTER INSERT OR UPDATE OR DELETE ON teams FOR EACH ROW +EXECUTE PROCEDURE insert_team_in_event_queue ();