Skip to content

Commit

Permalink
fix old ring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Mar 5, 2024
1 parent 76529ff commit 0983c03
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions db/migrations/20231011110157_animal_and_ring.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,37 @@ create table ring(
);

grant select on animal, ring to web_anon;
grant insert on animal, ring to writer;
grant select, insert on animal, ring to writer;

create function import_animal_and_ring(new import) returns void language plpgsql
as $$
declare
animal text;
animal_id text;
begin
if new.old_ring_number is null then
animal = new.ring_number;
animal_id = new.ring_number;
else
select animal into strict animal
from ring
where id = new.old_ring_number;
select animal into strict animal_id
from ring
where id = new.old_ring_number;
end if;
insert into animal values(
animal,
animal_id,
new.species,
new.morph,
new.subspecies
) on conflict do nothing;
insert into ring values(
new.ring_number,
animal,
animal_id,
new.euring_code,
new.colour_ring_colour,
new.colour_ring_code
) on conflict do nothing;
exception
when no_data_found then raise exception using
errcode = '22000',
message = 'Old ring number specified, but not present in the database';
end;
$$;

Expand Down

0 comments on commit 0983c03

Please sign in to comment.