8000 Adds triggers for testing · DavraYoung/postgres-meta@fe5711d · GitHub
[go: up one dir, main page]

Skip to content

Commit fe5711d

Browse files
kiwicopplew3b6x9
authored andcommitted
Adds triggers for testing
1 parent c54be39 commit fe5711d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test/postgres/mnt/00-init.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,26 @@ CREATE FUNCTION add(integer, integer) RETURNS integer
3232
LANGUAGE SQL
3333
IMMUTABLE
3434
RETURNS NULL ON NULL INPUT;
35+
36+
create table public.users_audit (
37+
id BIGINT generated by DEFAULT as identity,
38+
created_at timestamptz DEFAULT now(),
39+
user_id bigint,
40+
previous_value jsonb
41+
);
42+
43+
create function public.audit_action()
44+
returns trigger as $$
45+
begin
46+
insert into public.users_audit (user_id, previous_value)
47+
values (old.id, row_to_json(old));
48+
49+
return new;
50+
end;
51+
$$ language plpgsql;
52+
53+
54+
CREATE TRIGGER check_update
55+
BEFORE UPDATE ON users
56+
FOR EACH ROW
57+
EXECUTE FUNCTION public.audit_action();

0 commit comments

Comments
 (0)
0