camper/deploy/unset_season_range.sql

50 lines
1.2 KiB
MySQL
Raw Normal View History

-- Deploy camper:unset_season_range to pg
-- requires: roles
-- requires: schema_camper
-- requires: season_calendar
begin;
set search_path to camper, public;
create or replace function unset_season_range(range daterange) returns void as
$$
declare
tmp_range daterange;
tmp_id integer;
begin
delete from season_calendar where range @> season_range;
for tmp_id, tmp_range in
select season_id, season_range
from season_calendar
where season_range @> range
loop
delete from season_calendar where season_id = tmp_id and season_range = tmp_range;
insert into season_calendar (season_id, season_range)
values (tmp_id, daterange(lower(tmp_range), lower(range)))
, (tmp_id, daterange(upper(range), upper(tmp_range)))
;
end loop;
update season_calendar
set season_range = season_range - range
where season_range && range
and season_range < range
;
update season_calendar
set season_range = season_range - (season_range * range)
where season_range && range
and season_range > range
;
end
$$
language plpgsql
;
revoke execute on function unset_season_range(daterange) from public;
grant execute on function unset_season_range(daterange) to admin;
commit;