Skip to content

Commit

Permalink
db script for events
Browse files Browse the repository at this point in the history
The commit adds a new table called Events.
It also adds a trigger to watch for changes in the events table and
send notifications to the events_channel.

Signed-off-by: Gaurav Gahlot <gauravgahlot0107@gmail.com>
  • Loading branch information
gauravgahlot committed Sep 21, 2020
1 parent 94470ae commit 4782daf
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions deploy/db/tinkerbell-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,29 @@ CREATE TABLE IF NOT EXISTS workflow_data (
, metadata JSONB
, data JSONB
);

CREATE TABLE IF NOT EXISTS events (
id UUID UNIQUE NOT NULL primary key
, resource_id UUID NOT NULL
, resource_type int NOT NULL
, event_type int NOT NULL
, created_at TIMESTAMPTZ
, data JSONB
);

CREATE INDEX IF NOT EXISTS idx_eid ON events (id);
CREATE INDEX IF NOT EXISTS idx_etype ON events (event_type);
CREATE INDEX IF NOT EXISTS idx_rid ON events (resource_id);
CREATE INDEX IF NOT EXISTS idx_rtype ON events (resource_type);

CREATE OR REPLACE FUNCTION notify_event_changes()
RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('events_channel', row_to_json(NEW)::text);
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER events_channel
AFTER INSERT ON events
FOR EACH ROW EXECUTE PROCEDURE notify_event_changes()

0 comments on commit 4782daf

Please sign in to comment.