forked from supabase/supa_audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
supa_audit--0.2.1--0.2.2.sql
62 lines (56 loc) · 1.48 KB
/
supa_audit--0.2.1--0.2.2.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
create or replace function audit.enable_tracking(regclass)
returns void
volatile
security definer
language plpgsql
as $$
declare
statement_row text = format('
create trigger audit_i_u_d
before 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
before 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
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;
$$;