forked from supabase/supa_audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supa_audit--0.2.3--0.3.0.sql
197 lines (173 loc) · 4.61 KB
/
supa_audit--0.2.3--0.3.0.sql
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
create or replace function audit.primary_key_columns(entity_oid oid)
returns text[]
stable
security definer
set search_path = ''
language sql
as $$
-- Looks up the names of a table's primary key columns
select
coalesce(
array_agg(pa.attname::text order by pa.attnum),
array[]::text[]
) column_names
from
pg_index pi
join pg_attribute pa
on pi.indrelid = pa.attrelid
and pa.attnum = any(pi.indkey)
where
indrelid = $1
and indisprimary
$$;
create or replace function audit.to_record_id(entity_oid oid, pkey_cols text[], rec jsonb)
returns uuid
stable
language sql
as $$
select
case
when rec is null then null
when pkey_cols = array[]::text[] then uuid_generate_v4()
else (
select
uuid_generate_v5(
'fd62bc3d-8d6e-43c2-919c-802ba3762271',
( jsonb_build_array(to_jsonb($1)) || jsonb_agg($3 ->> key_) )::text
)
from
unnest($2) x(key_)
)
end
$$;
create or replace function audit.insert_update_delete_trigger()
returns trigger
security definer
-- can not use search_path = '' here because audit.to_record_id requires
-- uuid_generate_v4, which may be installed in a user-defined schema
language plpgsql
as $$
declare
pkey_cols text[] = audit.primary_key_columns(TG_RELID);
record_jsonb jsonb = to_jsonb(new);
record_id uuid = audit.to_record_id(TG_RELID, pkey_cols, record_jsonb);
old_record_jsonb jsonb = to_jsonb(old);
old_record_id uuid = audit.to_record_id(TG_RELID, pkey_cols, old_record_jsonb);
begin
insert into audit.record_version(
record_id,
old_record_id,
op,
table_oid,
table_schema,
table_name,
record,
old_record
)
select
record_id,
old_record_id,
TG_OP::audit.operation,
TG_RELID,
TG_TABLE_SCHEMA,
TG_TABLE_NAME,
record_jsonb,
old_record_jsonb;
return coalesce(new, old);
end;
$$;
create or replace function audit.truncate_trigger()
returns trigger
security definer
set search_path = ''
language plpgsql
as $$
begin
insert into audit.record_version(
op,
table_oid,
table_schema,
table_name
)
select
TG_OP::audit.operation,
TG_RELID,
TG_TABLE_SCHEMA,
TG_TABLE_NAME;
return coalesce(old, new);
end;
$$;
create or replace function audit.enable_tracking(regclass)
returns void
volatile
security definer
set search_path = ''
language plpgsql
as $$
declare
statement_row text = format('
create trigger audit_i_u_d
after insert or update or delete
on %s
for each row
execute procedure audit.insert_update_delete_trigger();',
$1
);
statement_stmt text = format('
create trigger audit_t
after truncate
on %s
for each statement
execute procedure audit.truncate_trigger();',
$1
);
pkey_cols text[] = audit.primary_key_columns($1);
begin
if pkey_cols = array[]::text[] then
raise exception 'Table % can not be audited because it has no primary key', $1;
end if;
if not exists(select 1 from pg_trigger where tgrelid = $1 and tgname = 'audit_i_u_d') then
execute statement_row;
end if;
if not exists(select 1 from pg_trigger where tgrelid = $1 and tgname = 'audit_t') then
execute statement_stmt;
end if;
end;
$$;
create or replace function audit.disable_tracking(regclass)
returns void
volatile
security definer
set search_path = ''
language plpgsql
as $$
declare
statement_row text = format(
'drop trigger if exists audit_i_u_d on %s;',
$1
);
statement_stmt text = format(
'drop trigger if exists audit_t on %s;',
$1
);
begin
execute statement_row;
execute statement_stmt;
end;
$$;
/*
Transition Existing audit_i_u_d and audit_t triggers from "before" to "after"
*/
do $$
declare
tab regclass;
begin
-- find all existing audit triggers
for tab in select tgrelid::regclass from pg_trigger where tgname = 'audit_i_u_d' loop
-- remove the "before" triggers"
perform audit.disable_tracking(tab);
-- create the "after" triggers
perform audit.enable_tracking(tab);
end loop;
end
$$;