-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscratch.sql
54 lines (41 loc) · 1.28 KB
/
scratch.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
insert into events (type, uuid, body)
values ('create_user', '11111111-1111-1111-1111-111111111111', '{"name": "blah"}');
insert into events (type, uuid, body)
values ('update_user', '11111111-1111-1111-1111-111111111111', '{"name": "vtha"}');
insert into events (type, uuid, body)
values ('update_user', '11111111-1111-1111-1111-111111111111', '{"name": "toby"}');
SELECT *
FROM events e
WHERE inserted_at = (
SELECT MAX(e2.inserted_at)
FROM events e2
WHERE e2.uuid = e.uuid
)
create or replace function fn_event_insert() returns trigger
security definer
language plpgsql
as $$
begin
if new.body ?& array['blah'] then
insert into users(id, name, inserted_at, updated_at)
values(new.id, new.body->>'blah', NOW(), NOW())
on conflict (id) do
update SET name = new.body->>'blah', updated_at = NOW()
where users.id = new.id;
end if;
return new;
end;
$$;
create or replace function fn_event_insert_action(event jsonb) returns integer
security definer
language plpgsql
as $$
declare return_id int;
begin
insert into users(id, name, inserted_at, updated_at)
values((event->>'id')::int, event->>'name', NOW(), NOW())
RETURNING id into return_id;
return return_id;
end;
$$;
SELECT fn_event_insert_action(body) FROM events LIMIT 1;