-
Notifications
You must be signed in to change notification settings - Fork 0
/
stored-signals
48 lines (41 loc) · 1.17 KB
/
stored-signals
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
DROP SEQUENCE IF EXISTS signal_ids;
CREATE SEQUENCE signal_ids START 2;
DROP SEQUENCE IF EXISTS yeslets_ids;
CREATE SEQUENCE yeslets_ids START 2;
DROP FUNCTION IF EXISTS on_other_insert() CASCADE;
CREATE FUNCTION on_other_insert() RETURNS trigger AS $$
DECLARE
id_sequence VARCHAR;
BEGIN
SELECT TG_ARGV[0] INTO id_sequence;
NEW.id := nextval(id_sequence);
NEW.created_at := now();
NEW.updated_at := now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP FUNCTION IF EXISTS on_other_update() CASCADE;
CREATE FUNCTION on_other_update() RETURNS trigger AS $$
BEGIN
NEW.id := OLD.id;
NEW.created_at := OLD.created_at;
NEW.updated_at := now();
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER signal_insert
BEFORE INSERT ON signals
FOR EACH ROW
EXECUTE PROCEDURE on_other_insert('signal_ids');
CREATE TRIGGER signals_update
BEFORE UPDATE ON signals
FOR EACH ROW
EXECUTE PROCEDURE on_other_update();
CREATE TRIGGER yesletses_insert
BEFORE INSERT ON yesletses
FOR EACH ROW
EXECUTE PROCEDURE on_other_insert('yeslets_ids');
CREATE TRIGGER yesletses_update
BEFORE UPDATE ON yesletses
FOR EACH ROW
EXECUTE PROCEDURE on_other_update();