begin; do $$ declare owner uuid:=gen_random_uuid(); friend uuid:=gen_random_uuid(); map uuid; manifest jsonb; invite uuid; begin insert into auth.users(id) values(owner),(friend); insert into public.profiles(id) values(owner),(friend) on conflict(id) do nothing; manifest:='{"format":"minegeo-dataset/v1","source":"plugin","mediaFormat":"webp","name":"Admin fixture","description":"","serverName":"Admin fixture server","bounds":{"minX":0,"minZ":0,"maxX":64,"maxZ":64},"viewpoints":[{"id":"p0","x":0,"y":70,"z":0},{"id":"p1","x":1,"y":70,"z":1},{"id":"p2","x":2,"y":70,"z":2},{"id":"p3","x":3,"y":70,"z":3},{"id":"p4","x":4,"y":70,"z":4}],"assets":[]}'; map:=public.create_upload(owner,manifest); insert into public.map_admins(dataset_id,user_id,invited_by) values(map,friend,owner) returning id into invite; if (select state from public.map_admins where id=invite)<>'pending' then raise exception 'Invite did not start pending'; end if; begin insert into public.map_admins(dataset_id,user_id,invited_by) values(map,friend,owner); raise exception 'Duplicate invite allowed'; exception when unique_violation then null; end; begin insert into public.map_admins(dataset_id,user_id,invited_by) values(map,owner,owner); raise exception 'Self invite allowed'; exception when check_violation then null; end; begin update public.map_admins set responded_at=now() where id=invite; raise exception 'Pending invite stamped'; exception when check_violation then null; end; update public.map_admins set state='accepted',responded_at=now() where id=invite; if not exists(select 1 from public.map_admins where id=invite and state='accepted' and responded_at is not null) then raise exception 'Accept failed'; end if; begin update public.map_admins set responded_at=null where id=invite; raise exception 'Response timestamp dropped'; exception when check_violation then null; end; delete from public.datasets where id=map; if exists(select 1 from public.map_admins where id=invite) then raise exception 'Invite survived map deletion'; end if; end; $$; rollback;