Skip to content

Commit

Permalink
feat: add function to protect date ranges in reporting_year table
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Nov 25, 2020
1 parent 09b5009 commit 1b5542a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
31 changes: 31 additions & 0 deletions schema/deploy/trigger_functions/protect_date_overlap.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Deploy ggircs-portal:trigger_functions/protect_date_overlap to pg
-- requires: schema_ggircs_portal_private

begin;
create or replace function ggircs_portal_private.protect_date_overlap()
returns trigger as $$
declare
temp_row record;

begin

for temp_row in
select * from ggircs_portal.reporting_year
loop
if (new.reporting_year != temp_row.year) then
if ((new.reporting_period_start between temp_row.reporting_period_start and temp_row.reporting_period_end)
or (new.reporting_period_end between temp_row.reporting_period_start and temp_row.reporting_period_end)
or (new.application_open_time between temp_row.application_open_time and temp_row.application_close_time)
or (new.application_close_time between temp_row.application_open_time and temp_row.application_close_time)) then
raise exception 'New date range entry overlaps with date range for reporting year %', temp_row.reporting_year;
end if;
end if;
end loop;

return new;
end;
$$ language plpgsql volatile;

comment on function ggircs_portal_private.protect_date_overlap is 'Trigger function excepts if a date range in reporting_year overlaps with another row';

commit;
7 changes: 7 additions & 0 deletions schema/revert/trigger_functions/protect_date_overlap.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert ggircs-portal:trigger_functions/protect_date_overlap from pg

begin;

drop function ggircs_portal_private.protect_date_overlap;

commit;
3 changes: 2 additions & 1 deletion schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,5 @@ computed_columns/application_revision_ciip_incentive [computed_columns/applicati
@v1.10.1 2020-10-15T17:41:54Z Matthieu Foucault <matthieu@button.is> # release v1.10.1
@v1.11.0 2020-10-22T00:27:09Z Dylan Leard <dylan@button.is> # release v1.11.0
function_current_timestamp [function_current_timestamp@v1.10.1] 2020-10-30T23:15:30Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # Change ggircs_portal.current_timestamp() to use the mockable now() call instead
@v1.12.0 2020-11-16T16:53:25Z Dylan Leard,,, <dleard@dleard-Aspire-V5-591G> # release v1.12.0
@v1.12.0 2020-11-16T16:53:25Z Dylan Leard <dylan@button.is> # release v1.12.0

This comment has been minimized.

Copy link
@matthieu-foucault

matthieu-foucault Dec 1, 2020

Contributor

💥

trigger_functions/protect_date_overlap [schema_ggircs_portal_private] 2020-11-25T19:17:30Z Dylan Leard <dylan@button.is> # Trigger function to deny changes if a date range overlaps another
7 changes: 7 additions & 0 deletions schema/verify/trigger_functions/protect_date_overlap.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:trigger_functions/protect_date_overlap on pg

begin;

select pg_get_functiondef('ggircs_portal_private.protect_date_overlap()'::regprocedure);

rollback;

0 comments on commit 1b5542a

Please sign in to comment.