diff --git a/schema/deploy/mutations/update_form_change.sql b/schema/deploy/mutations/update_form_change.sql index 10e7b48f42..554eb52bf6 100644 --- a/schema/deploy/mutations/update_form_change.sql +++ b/schema/deploy/mutations/update_form_change.sql @@ -9,8 +9,10 @@ $$ update cif.form_change set new_form_data = coalesce(form_change_patch.new_form_data, new_form_data), - operation = coalesce(form_change_patch.operation, operation), - validation_errors = coalesce(form_change_patch.validation_errors, validation_errors), + operation = case + when form_change_patch.operation is null and operation = 'archive' then 'update' + else coalesce(form_change_patch.operation, operation) + end, validation_errors = coalesce(form_change_patch.validation_errors, validation_errors), change_status = 'pending' where id=row_id returning *; diff --git a/schema/deploy/mutations/update_form_change@1.6.0.sql b/schema/deploy/mutations/update_form_change@1.6.0.sql new file mode 100644 index 0000000000..10e7b48f42 --- /dev/null +++ b/schema/deploy/mutations/update_form_change@1.6.0.sql @@ -0,0 +1,24 @@ +-- Deploy cif:mutations/update_form_change to pg + +begin; + +create or replace function cif.update_form_change(row_id int, form_change_patch cif.form_change) +returns cif.form_change +as +$$ + + update cif.form_change set + new_form_data = coalesce(form_change_patch.new_form_data, new_form_data), + operation = coalesce(form_change_patch.operation, operation), + validation_errors = coalesce(form_change_patch.validation_errors, validation_errors), + change_status = 'pending' + where id=row_id + returning *; + +$$ language sql volatile; + +grant execute on function cif.update_form_change to cif_internal, cif_external, cif_admin; + +comment on function cif.update_form_change(int, cif.form_change) is 'Custom mutation that sets the change status to pending on every form_change update'; + +commit; diff --git a/schema/revert/mutations/update_form_change.sql b/schema/revert/mutations/update_form_change.sql index ee5f9b7ba4..10e7b48f42 100644 --- a/schema/revert/mutations/update_form_change.sql +++ b/schema/revert/mutations/update_form_change.sql @@ -1,7 +1,24 @@ --- Revert cif:mutations/update_form_change from pg +-- Deploy cif:mutations/update_form_change to pg begin; -drop function cif.update_form_change; +create or replace function cif.update_form_change(row_id int, form_change_patch cif.form_change) +returns cif.form_change +as +$$ + + update cif.form_change set + new_form_data = coalesce(form_change_patch.new_form_data, new_form_data), + operation = coalesce(form_change_patch.operation, operation), + validation_errors = coalesce(form_change_patch.validation_errors, validation_errors), + change_status = 'pending' + where id=row_id + returning *; + +$$ language sql volatile; + +grant execute on function cif.update_form_change to cif_internal, cif_external, cif_admin; + +comment on function cif.update_form_change(int, cif.form_change) is 'Custom mutation that sets the change status to pending on every form_change update'; commit; diff --git a/schema/revert/mutations/update_form_change@1.6.0.sql b/schema/revert/mutations/update_form_change@1.6.0.sql new file mode 100644 index 0000000000..ee5f9b7ba4 --- /dev/null +++ b/schema/revert/mutations/update_form_change@1.6.0.sql @@ -0,0 +1,7 @@ +-- Revert cif:mutations/update_form_change from pg + +begin; + +drop function cif.update_form_change; + +commit; diff --git a/schema/sqitch.plan b/schema/sqitch.plan index 37c48803b7..72d24f7b03 100644 --- a/schema/sqitch.plan +++ b/schema/sqitch.plan @@ -297,3 +297,4 @@ tables/attachment_001_drop_project_id_project_status_id [tables/attachment] 2023 mutations/add_project_attachment_to_revision 2023-04-28T23:02:34Z Sepehr Sobhani # Creates an attachment and then adds a project_attachment form_change computed_columns/form_change_as_project_attachment [tables/attachment tables/form_change] 2023-05-04T23:00:57Z Sepehr Sobhani # Computed column returns a attachment record type for a project_attachment form_change mutations/update_milestone_form_change [mutations/update_milestone_form_change@1.7.0] 2023-05-10T23:41:13Z Sepehr Sobhani # Remove total eligible expenses from the form data when milestone type is not General +mutations/update_form_change [mutations/update_form_change@1.6.0] 2023-04-19T23:31:07Z Gurjeet Matharu # fix in update_form_change to handle a null operation for archived forms. Should now set to update. diff --git a/schema/test/unit/mutations/update_form_change_test.sql b/schema/test/unit/mutations/update_form_change_test.sql index fdd4541dd9..756d6b25a2 100644 --- a/schema/test/unit/mutations/update_form_change_test.sql +++ b/schema/test/unit/mutations/update_form_change_test.sql @@ -1,7 +1,7 @@ begin; -select plan(3); +select plan(4); insert into cif.form_change( id, @@ -102,7 +102,58 @@ select results_eq( 'The update_form_change only updates the new_form_data, validation_errors and operation fields' ); - +insert into cif.form_change( + id, + new_form_data, + operation, + form_data_schema_name, + form_data_table_name, + form_data_record_id, + project_revision_id, + change_status, + json_schema_name + ) overriding system value values ( + 54321, + '{}'::jsonb, + 'archive', + 'cif', + 'some_table', + 54321, + null, + 'staged', + 'reporting_requirement' + ); +select results_eq( + $$ + select + id, + new_form_data, + operation, + form_data_schema_name, + form_data_table_name, + form_data_record_id, + project_revision_id, + change_status, + json_schema_name + from cif.update_form_change( + 54321, (select row( 54321, '{"test":2}'::jsonb, null, 'test-schema', 'test-table', 1111, 2222, 'committed', 'some-schema-name', '[]'::jsonb, null, null, null, null, null)::cif.form_change) + ); + $$, + $$ + values ( + 54321, + '{"test":2}'::jsonb, + 'update'::cif.form_change_operation, + 'cif'::varchar, + 'some_table'::varchar, + 54321, + null::int, + 'pending'::varchar, + 'reporting_requirement'::varchar + ) + $$, + 'The update_form_change custom mutation correctly handles undefined operation when currently archived. Should be set to update' +); select finish(); rollback; diff --git a/schema/verify/mutations/update_form_change@1.6.0.sql b/schema/verify/mutations/update_form_change@1.6.0.sql new file mode 100644 index 0000000000..684e2b2800 --- /dev/null +++ b/schema/verify/mutations/update_form_change@1.6.0.sql @@ -0,0 +1,7 @@ +-- Verify cif:mutations/update_form_change on pg + +begin; + +select pg_get_functiondef('cif.update_form_change(int, cif.form_change)'::regprocedure); + +rollback;