-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path001_history_triggers.up.sql
More file actions
49 lines (47 loc) · 1.58 KB
/
Copy path001_history_triggers.up.sql
File metadata and controls
49 lines (47 loc) · 1.58 KB
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
49
CREATE EXTENSION btree_gist;
CREATE FUNCTION copy_inserts_and_deletes_into_history() RETURNS TRIGGER AS $$
DECLARE
history_table TEXT := quote_ident(tg_argv[0]);
id_field TEXT := quote_ident(tg_argv[1]);
BEGIN
IF (TG_OP = 'INSERT') THEN
EXECUTE 'INSERT INTO ' || history_table ||
' SELECT gen_random_uuid(), tstzrange(NOW(), null), $1.*'
USING NEW;
RETURN NEW;
ELSIF (TG_OP = 'DELETE') THEN
-- close current row
-- note: updates and then deletes for same id
-- in same tx will fail
EXECUTE 'UPDATE ' || history_table ||
' SET systime = tstzrange(lower(systime), NOW())' ||
' WHERE ' || id_field || ' = $1.' || id_field ||
' AND systime @> NOW()' USING OLD;
RETURN OLD;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION copy_updates_into_history() RETURNS TRIGGER AS $$
DECLARE
history_table TEXT := quote_ident(tg_argv[0]);
id_field TEXT := quote_ident(tg_argv[1]);
BEGIN
-- ignore changes inside the same tx
EXECUTE 'DELETE FROM ' || history_table ||
' WHERE ' || id_field || ' = $1.' || id_field ||
' AND lower(systime) = NOW()' ||
' AND upper_inf(systime)' USING NEW;
-- close current row
-- (if any, may be deleted by previous line)
EXECUTE 'UPDATE ' || history_table ||
' SET systime = tstzrange(lower(systime), NOW())'
' WHERE ' || id_field || ' = $1.' || id_field ||
' AND systime @> NOW()' USING NEW;
-- insert new row
EXECUTE 'INSERT INTO ' || history_table ||
' SELECT gen_random_uuid(), tstzrange(NOW(), null), $1.*'
USING NEW;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;