Skip to content

Commit

Permalink
fix: update_form_change now handles the case when form data operator …
Browse files Browse the repository at this point in the history
…is null and form is currently archived. Sets it to updated.
  • Loading branch information
gurjmatharu committed May 12, 2023
1 parent e55b105 commit a2a8c95
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 6 deletions.
6 changes: 4 additions & 2 deletions schema/deploy/mutations/update_form_change.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 *;
Expand Down
24 changes: 24 additions & 0 deletions schema/deploy/mutations/update_form_change@1.6.0.sql
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 19 additions & 2 deletions schema/revert/mutations/update_form_change.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions schema/revert/mutations/update_form_change@1.6.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:mutations/update_form_change from pg

begin;

drop function cif.update_form_change;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sepehr.sobhani@gov.bc.ca> # 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 <sepehr.sobhani@gov.bc.ca> # 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 <sepehr.sobhani@gov.bc.ca> # 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 <gurjeet@matharuco.com> # fix in update_form_change to handle a null operation for archived forms. Should now set to update.
55 changes: 53 additions & 2 deletions schema/test/unit/mutations/update_form_change_test.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

begin;

select plan(3);
select plan(4);

insert into cif.form_change(
id,
Expand Down Expand Up @@ -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;
7 changes: 7 additions & 0 deletions schema/verify/mutations/update_form_change@1.6.0.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit a2a8c95

Please sign in to comment.