Move notify to registration-based triggers, store nodes in db
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DROP TRIGGER IF EXISTS nodes_notify ON nodes;
|
||||
DROP FUNCTION IF EXISTS invoke_nodes_trigger();
|
||||
@@ -0,0 +1,37 @@
|
||||
-- Your SQL goes here
|
||||
CREATE OR REPLACE FUNCTION invoke_nodes_trigger ()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
rec RECORD;
|
||||
channel TEXT;
|
||||
payload TEXT;
|
||||
BEGIN
|
||||
case TG_OP
|
||||
WHEN 'INSERT' THEN
|
||||
rec := NEW;
|
||||
channel := 'new_nodes';
|
||||
payload := NEW.listener_id;
|
||||
WHEN 'UPDATE' THEN
|
||||
rec := NEW;
|
||||
channel := 'new_nodes';
|
||||
payload := NEW.listener_id;
|
||||
WHEN 'DELETE' THEN
|
||||
rec := OLD;
|
||||
channel := 'rm_nodes';
|
||||
payload := OLD.listener_id;
|
||||
ELSE
|
||||
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
|
||||
END CASE;
|
||||
|
||||
PERFORM pg_notify(channel, payload::TEXT);
|
||||
RETURN rec;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE TRIGGER nodes_notify
|
||||
AFTER INSERT OR UPDATE OR DELETE
|
||||
ON nodes
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE invoke_nodes_trigger();
|
||||
@@ -0,0 +1,2 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
ALTER TABLE nodes DROP CONSTRAINT nodes_listener_ids_unique;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Your SQL goes here
|
||||
ALTER TABLE nodes ADD CONSTRAINT nodes_listener_ids_unique UNIQUE (listener_id);
|
||||
Reference in New Issue
Block a user