forked from hasura/skor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·33 lines (29 loc) · 895 Bytes
/
init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /bin/bash
# script to initialize the trigger for given tables
# create function that calls pg_notify
echo \
'CREATE OR REPLACE FUNCTION notify_skor() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
cur_rec record;
BEGIN
IF (TG_OP = '"'DELETE'"') THEN
cur_rec = OLD;
ELSE
cur_rec = NEW;
END IF;
PERFORM pg_notify('"'skor'"', json_build_object(
'"'data'"', row_to_json(cur_rec),
'"'table'"', TG_TABLE_NAME,
'"'op'"', TG_OP
)::text);
RETURN cur_rec;
END;
$$;'
# drop existing trigger and add trigger to input tables
for table in "$@"
do
echo "DROP TRIGGER IF EXISTS notify_skor ON $table;"
echo "CREATE TRIGGER notify_skor AFTER INSERT OR DELETE OR UPDATE ON $table FOR EACH ROW EXECUTE PROCEDURE notify_skor();"
done