camper/revert/add_campsite.sql

37 lines
942 B
PL/PgSQL

-- Deploy camper:add_campsite to pg
-- requires: roles
-- requires: schema_camper
-- requires: campsite
-- requires: campsite_type
begin;
set search_path to camper, public;
drop function if exists add_campsite(integer, text, text, text);
create or replace function add_campsite(campsite_type integer, label text) returns integer as
$$
declare
cid integer;
begin
insert into campsite (company_id, campsite_type_id, label)
select company_id, campsite_type_id, label
from campsite_type
where campsite_type_id = add_campsite.campsite_type
returning campsite_id into cid
;
if cid is null then
raise foreign_key_violation using message = 'insert or update on table "campsite" violates foreign key constraint "campsite_campsite_type_id_fkey"';
end if;
return cid;
end
$$
language plpgsql
;
revoke execute on function add_campsite(integer, text) from public;
grant execute on function add_campsite(integer, text) to admin;
commit;