Skip to content

Commit

Permalink
chore: adding sequential sqitchy
Browse files Browse the repository at this point in the history
  • Loading branch information
gurjmatharu authored and dleard committed Sep 12, 2023
1 parent df396e2 commit 4e1798c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
31 changes: 11 additions & 20 deletions schema/deploy/computed_columns/form_change_total_project_value.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- Deploy cif:computed_columns/form_change_total_project_value to pg

begin;

create or replace function cif.form_change_total_project_value(cif.form_change)
Expand All @@ -16,25 +18,15 @@ with additional_funding_sources as
)::jsonb
) as x(source text, amount numeric, status text)
)
select
case
when
(
($1.new_form_data ->> 'proponentCost')::numeric IS NULL
OR
($1.new_form_data ->> 'maxFundingAmount')::numeric IS NULL
OR
(select sum(amount::numeric) from additional_funding_sources where status = 'Approved') IS NULL
) THEN NULL
else
(
coalesce(($1.new_form_data ->> 'proponentCost')::numeric, 0)
+
coalesce(($1.new_form_data ->> 'maxFundingAmount')::numeric, 0)
+
coalesce((select sum(amount::numeric) from additional_funding_sources where status = 'Approved'), 0)
)
end;
select
(
(select coalesce(($1.new_form_data ->> 'proponentCost')::numeric, 0) + coalesce(($1.new_form_data ->> 'maxFundingAmount')::numeric, 0))
+
coalesce((select sum(amount::numeric)
from additional_funding_sources
where status = 'Approved'
), 0)
);
$computed_column$ language sql stable;

grant execute on function cif.form_change_total_project_value to cif_internal, cif_external, cif_admin;
Expand All @@ -44,7 +36,6 @@ $$
Computed column to return the total project value.
Calculation:
- Total Project Value = Maximum Funding Amount + Proponent Cost + approved Additional Funding Amount(s)
Only returns the total project value if all values are present. If any value is missing, returns NULL.
$$;

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
begin;

create or replace function cif.form_change_total_project_value(cif.form_change)
returns numeric
as
$computed_column$

with additional_funding_sources as
(
select * from jsonb_to_recordset(
(select (new_form_data ->> 'additionalFundingSources')
from cif.form_change fc
where fc.project_revision_id = $1.project_revision_id
and fc.form_data_table_name = 'funding_parameter'
and operation != 'archive'
)::jsonb
) as x(source text, amount numeric, status text)
)
select
case
when
(
($1.new_form_data ->> 'proponentCost')::numeric IS NULL
OR
($1.new_form_data ->> 'maxFundingAmount')::numeric IS NULL
OR
(select sum(amount::numeric) from additional_funding_sources where status = 'Approved') IS NULL
) THEN NULL
else
(
coalesce(($1.new_form_data ->> 'proponentCost')::numeric, 0)
+
coalesce(($1.new_form_data ->> 'maxFundingAmount')::numeric, 0)
+
coalesce((select sum(amount::numeric) from additional_funding_sources where status = 'Approved'), 0)
)
end;
$computed_column$ language sql stable;

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

comment on function cif.form_change_total_project_value is
$$
Computed column to return the total project value.
Calculation:
- Total Project Value = Maximum Funding Amount + Proponent Cost + approved Additional Funding Amount(s)
Only returns the total project value if all values are present. If any value is missing, returns NULL.
$$;

commit;
7 changes: 0 additions & 7 deletions schema/revert/computed_columns/[email protected]

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,4 @@ functions/handle_milestone_form_change_commit [functions/handle_milestone_form_c
@1.13.0 2023-09-05T22:09:47Z Sepehr Sobhani <[email protected]> # release v1.13.0
tables/attachment_002_delete_permission 2023-07-31T18:06:38Z Brianna Cerkiewicz <briannacerkiewicz@pop-os> # Allow hard deletion of attachments
mutations/discard_project_attachment_form_change [mutations/[email protected]] 2023-07-31T18:05:23Z Brianna Cerkiewicz <briannacerkiewicz@pop-os> # Remove loop from discard mutation
computed_columns/form_change_total_project_value_001 2023-07-21T00:06:15Z Gurjeet Matharu <[email protected]> # Modified cif.form_change_total_project_value function to return null if any input values are missing
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Verify cif:computed_columns/form_change_total_project_value on pg
-- Verify cif:computed_columns/form_change_total_project_value_001 on pg

begin;

Expand Down

0 comments on commit 4e1798c

Please sign in to comment.