Skip to content

Commit

Permalink
chore: pass correct arguments to commit form change internal
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevespi committed Oct 31, 2023
1 parent 06f9a4c commit 6d6532e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
16 changes: 13 additions & 3 deletions schema/deploy/mutations/commit_project_revision.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@ begin;

create or replace function cif.commit_project_revision(revision_to_commit_id int)
returns cif.project_revision as $$
declare
proj_id int;
pending_project_revision_id int;
begin
-- defer FK constraints check to the end of the transaction
set constraints all deferred;

select form_data_record_id into proj_id from cif.form_change where form_data_table_name='project' and project_revision_id=$1;
select id into pending_project_revision_id from cif.project_revision
where project_id = proj_id
and change_status = 'pending'
and id != $1
limit 1;

-- Propagate the change_status to all related form_change records
-- Save the project table first to avoid foreign key violations from other potential tables.
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change, pending_project_revision_id)
from cif.form_change
where project_revision_id=$1
and form_data_table_name='project';

perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change, pending_project_revision_id)
from cif.form_change
where project_revision_id=$1
and form_data_table_name='reporting_requirement';

perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change, pending_project_revision_id)
from cif.form_change
where project_revision_id=$1
and form_data_table_name not in ('project', 'reporting_requirement');
Expand Down
42 changes: 42 additions & 0 deletions schema/deploy/mutations/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- Deploy cif:mutations/commit_project_revision to pg

begin;

create or replace function cif.commit_project_revision(revision_to_commit_id int)
returns cif.project_revision as $$
begin
-- defer FK constraints check to the end of the transaction
set constraints all deferred;

-- Propagate the change_status to all related form_change records
-- Save the project table first to avoid foreign key violations from other potential tables.
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
from cif.form_change
where project_revision_id=$1
and form_data_table_name='project';

perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
from cif.form_change
where project_revision_id=$1
and form_data_table_name='reporting_requirement';

perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
from cif.form_change
where project_revision_id=$1
and form_data_table_name not in ('project', 'reporting_requirement');

update cif.project_revision set
project_id=(select form_data_record_id from cif.form_change where form_data_table_name='project' and project_revision_id=$1),
change_status='committed',
revision_status = 'Applied'
where id=$1;

return (select row(project_revision.*)::cif.project_revision from cif.project_revision where id = $1);
end;
$$ language plpgsql;

grant execute on function cif.commit_project_revision to cif_internal, cif_external, cif_admin;

comment on function cif.commit_project_revision(int) is 'Commits a project_revision and all of its form changes';

commit;
5 changes: 0 additions & 5 deletions schema/revert/mutations/commit_project_revision.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ begin
-- defer FK constraints check to the end of the transaction
set constraints all deferred;

if ((select project_id from cif.project_revision where id = $1) is not null)
and ((select change_reason from cif.project_revision where id = $1) is null) then
raise exception 'Cannot commit revision if change_reason is null.';
end if;

-- Propagate the change_status to all related form_change records
-- Save the project table first to avoid foreign key violations from other potential tables.
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
Expand Down
47 changes: 47 additions & 0 deletions schema/revert/mutations/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- Deploy cif:mutations/commit_project_revision to pg

begin;

create or replace function cif.commit_project_revision(revision_to_commit_id int)
returns cif.project_revision as $$
begin
-- defer FK constraints check to the end of the transaction
set constraints all deferred;

if ((select project_id from cif.project_revision where id = $1) is not null)
and ((select change_reason from cif.project_revision where id = $1) is null) then
raise exception 'Cannot commit revision if change_reason is null.';
end if;

-- Propagate the change_status to all related form_change records
-- Save the project table first to avoid foreign key violations from other potential tables.
perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
from cif.form_change
where project_revision_id=$1
and form_data_table_name='project';

perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
from cif.form_change
where project_revision_id=$1
and form_data_table_name='reporting_requirement';

perform cif_private.commit_form_change_internal(row(form_change.*)::cif.form_change)
from cif.form_change
where project_revision_id=$1
and form_data_table_name not in ('project', 'reporting_requirement');

update cif.project_revision set
project_id=(select form_data_record_id from cif.form_change where form_data_table_name='project' and project_revision_id=$1),
change_status='committed',
revision_status = 'Applied'
where id=$1;

return (select row(project_revision.*)::cif.project_revision from cif.project_revision where id = $1);
end;
$$ language plpgsql;

grant execute on function cif.commit_project_revision to cif_internal, cif_external, cif_admin;

comment on function cif.commit_project_revision(int) is 'Commits a project_revision and all of its form changes';

commit;
7 changes: 7 additions & 0 deletions schema/verify/mutations/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify cif:mutations/commit_project_revision on pg

begin;

select pg_get_functiondef('cif.commit_project_revision(int)'::regprocedure);

rollback;

0 comments on commit 6d6532e

Please sign in to comment.