From 0a4ba2ff41bfb06f24fd4499c47eb21d84eefd21 Mon Sep 17 00:00:00 2001 From: Mike Vesprini Date: Tue, 31 Oct 2023 16:41:38 -0700 Subject: [PATCH] chore: rebase changes made in pending form change on top of the committing form change --- .../mutations/commit_form_change_internal.sql | 42 +++++++++++++++++-- .../mutations/commit_form_change_internal.sql | 11 +++-- schema/sqitch.plan | 1 + 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/schema/deploy/mutations/commit_form_change_internal.sql b/schema/deploy/mutations/commit_form_change_internal.sql index bee73b915f..2eb823e4d5 100644 --- a/schema/deploy/mutations/commit_form_change_internal.sql +++ b/schema/deploy/mutations/commit_form_change_internal.sql @@ -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 @@ -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; diff --git a/schema/revert/mutations/commit_form_change_internal.sql b/schema/revert/mutations/commit_form_change_internal.sql index ae1a6bc0eb..bee73b915f 100644 --- a/schema/revert/mutations/commit_form_change_internal.sql +++ b/schema/revert/mutations/commit_form_change_internal.sql @@ -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 @@ -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; diff --git a/schema/sqitch.plan b/schema/sqitch.plan index b62717af75..858c048e5f 100644 --- a/schema/sqitch.plan +++ b/schema/sqitch.plan @@ -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 # Function to provide the object difference between two jsonb objects mutations/commit_form_change_internal [mutations/commit_form_change_internal@1.15.0] 2023-10-30T21:34:15Z Mike Vesprini # Handle rebasing when committing with another pending revision on the same project" +mutations/commit_project_revision [mutations/commit_project_revision@1.15.0] 2023-10-31T00:07:01Z Mike Vesprini # Update function to pass new parameters to commit_form_change_internal