Refactoring Opinion Tests

upsert_opinion_choice
This commit is contained in:
2020-03-17 00:31:24 +01:00
parent 6b4a6f4075
commit 8c7e5bdf9b
9 changed files with 129 additions and 67 deletions

View File

@@ -0,0 +1,20 @@
create or replace function upsert_opinion_choice(inout resource json)
language plpgsql as
$$
declare
_id uuid = coalesce((resource->>'id')::uuid, uuid_generate_v4());
begin
insert into opinion_choice (id, name, target)
select
_id,
name,
target
from json_populate_record(null::opinion_choice, resource)
on conflict (name) do update set
target = excluded.target;
select find_opinion_choice_by_id(_id) into resource;
end;
$$;
-- drop function if exists upsert_opinion_choice(json);