Skip to content

Commit

Permalink
feat: add notification table
Browse files Browse the repository at this point in the history
* add trigger to push any changes on the teams table to the event queue
  • Loading branch information
adityathebe authored and moshloop committed Jun 6, 2023
1 parent 48b88b9 commit 2e0d482
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
35 changes: 34 additions & 1 deletion schema/incidents.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ table "comments" {
}
}


table "severities" {
schema = schema.public
column "id" {
Expand All @@ -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]
}
}
23 changes: 22 additions & 1 deletion views/007_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,25 @@ WHERE
AND attempts > 0
AND created_at >= NOW() - INTERVAL '7 days'
GROUP BY
name;
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 ();

0 comments on commit 2e0d482

Please sign in to comment.