Skip to content

Commit

Permalink
chore: rebase changes made in pending form change on top of the commi…
Browse files Browse the repository at this point in the history
…tting form change
  • Loading branch information
mikevespi committed Oct 31, 2023
1 parent 6d6532e commit 0a4ba2f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
42 changes: 39 additions & 3 deletions schema/deploy/mutations/commit_form_change_internal.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
-- Deploy cif:mutations/commit_form_change to pg
begin;

create or replace function cif_private.commit_form_change_internal(fc cif.form_change)
create or replace function cif_private.commit_form_change_internal(fc cif.form_change, pending_project_revision_id int)
returns cif.form_change as $$
declare
recordId int;
pending_form_change cif.form_change;
parent_of_pending_form_change cif.form_change;
begin

if fc.validation_errors != '[]' then
Expand All @@ -26,12 +28,46 @@ begin
change_status = 'committed'
where id = fc.id;

if pending_project_revision_id is not null then
-- If the committing form change is a create, then it needs to be created in the pending revision with an update operation.
if fc.operation = 'create' then
perform cif.create_form_change(
operation => 'update'::cif.form_change_operation,
form_data_schema_name => 'cif',
form_data_table_name => fc.form_data_table_name,
form_data_record_id => fc.form_data_record_id,
project_revision_id => pending_project_revision_id,
json_schema_name => fc.json_schema_name,
new_form_data => fc.new_form_data
);
elsif fc.operation = 'update' then
-- store the other pending project revisions corresponding form_change, and its parent
select * into pending_form_change from cif.form_change
where project_revision_id = pending_project_revision_id
and form_data_table_name = fc.form_data_table_name
and form_data_record_id = fc.form_data_record_id limit 1;
select * into parent_of_pending_form_change from cif.form_change
where id = pending_form_change.previous_form_change_id limit 1;
-- set the pending form change data to be the committing form change data, plus the changes made in the
-- pending revision
update cif.form_change
set new_form_data =
(fc.new_form_data || cif.jsonb_minus(pending_form_change.new_form_data, parent_of_pending_form_change.new_form_data))
where id = pending_form_change.id;

elsif fc.operation = 'archive' then
delete from cif.form_change
where project_revision_id = pending_project_revision_id
and form_data_table_name = fc.form_data_table_name
and form_data_record_id = fc.form_data_record_id;
end if;
end if;
return (select row(form_change.*)::cif.form_change from cif.form_change where id = fc.id);
end;
$$ language plpgsql volatile;

grant execute on function cif_private.commit_form_change_internal to cif_internal, cif_external, cif_admin;
grant execute on function cif_private.commit_form_change_internal(cif.form_change, int) to cif_internal, cif_external, cif_admin;

comment on function cif_private.commit_form_change_internal(cif.form_change) is 'Commits the form change and calls the corresponding commit handler.';
comment on function cif_private.commit_form_change_internal(cif.form_change, int) is 'Commits the form change and calls the corresponding commit handler.';

commit;
11 changes: 8 additions & 3 deletions schema/revert/mutations/commit_form_change_internal.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ begin;

create or replace function cif_private.commit_form_change_internal(fc cif.form_change)
returns cif.form_change as $$
declare
recordId int;
begin

if fc.validation_errors != '[]' then
Expand All @@ -14,10 +16,13 @@ begin
end if;

-- TODO : add a conditional behaviour based on fc.form_id
execute format(
'select "cif_private".%I($1)',
(select form_change_commit_handler from cif.form where slug = fc.json_schema_name)
) using fc into recordId;

update cif.form_change set
form_data_record_id = (
select cif_private.handle_default_form_change_commit(fc)
),
form_data_record_id = recordId,
change_status = 'committed'
where id = fc.id;

Expand Down
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,4 @@ functions/handle_milestone_form_change_commit [functions/handle_milestone_form_c

functions/jsonb_minus 2023-10-30T19:55:55Z Mike Vesprini <[email protected]> # Function to provide the object difference between two jsonb objects
mutations/commit_form_change_internal [mutations/[email protected]] 2023-10-30T21:34:15Z Mike Vesprini <[email protected]> # Handle rebasing when committing with another pending revision on the same project"
mutations/commit_project_revision [mutations/[email protected]] 2023-10-31T00:07:01Z Mike Vesprini <[email protected]> # Update function to pass new parameters to commit_form_change_internal

0 comments on commit 0a4ba2f

Please sign in to comment.