Skip to content

Commit

Permalink
chore: using sqitch rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gurjmatharu authored and dleard committed Sep 12, 2023
1 parent 0994594 commit f9275dd
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@ create or replace function cif.form_change_holdback_amount_to_date(parameter_fc
returns numeric as
$fn$

select
round(sum(coalesce((fc.new_form_data->>'adjustedHoldbackAmount')::numeric, (fc.new_form_data->>'calculatedHoldbackAmount')::numeric, 0)), 2)
with results as (
select
(fc.new_form_data->>'adjustedHoldbackAmount')::numeric as adjustedHoldbackAmount,
(fc.new_form_data->>'calculatedHoldbackAmount')::numeric as calculatedHoldbackAmount
from cif.form_change fc
where fc.project_revision_id = $1.project_revision_id
and json_schema_name = 'milestone'
and (fc.new_form_data->>'hasExpenses')::boolean = true;
and (fc.new_form_data->>'hasExpenses')::boolean = true
and fc.operation <> 'archive'
)

select
case
when count(*) filter (where adjustedHoldbackAmount is null and calculatedHoldbackAmount is null) > 0 then null
else round(sum(coalesce(adjustedHoldbackAmount, calculatedHoldbackAmount)), 0)
end
from results;

$fn$ language sql stable;

comment on function cif.form_change_holdback_amount_to_date(cif.form_change) is 'Computed column returns sum all holdback amounts for a project. Preference for value selection is adjustedHoldbackAmount > calculuatedHoldbackAmount > amount calculated via maximum milestone amount';
comment on function cif.form_change_holdback_amount_to_date(cif.form_change) is 'Computed column returns sum all holdback amounts for a project. If any of the milestones have null values for adjustedHoldbackAmount and calculatedHoldbackAmount, then null is returned. Preference for value selection is adjustedHoldbackAmount > calculatedHoldbackAmount > amount calculated via maximum milestone amount';

commit;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-- Revert cif:computed_columns/form_change_holdback_amount_to_date_001 from pg
-- Deploy cif:computed_columns/form_change_holdback_amount_to_date to pg
-- requires: tables/form_change

begin;

Expand Down
56 changes: 32 additions & 24 deletions schema/deploy/computed_columns/form_change_total_project_value.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
-- 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)
returns numeric
as
$computed_column$

with additional_funding_sources as
(
select * from jsonb_to_recordset(
Expand All @@ -17,25 +13,37 @@ with additional_funding_sources as
and operation != 'archive'
)::jsonb
) as x(source text, amount numeric, status text)
)
select
),
additional_funding_sources_sum as
(
select sum(amount::numeric) as total from additional_funding_sources where status = 'Approved'
),
additional_funding_sources_exist as
(
(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)
);
select 1 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'
and new_form_data->'additionalFundingSources' is not null
)
select
case
when
(
($1.new_form_data ->> 'proponentCost')::numeric IS NULL
OR
($1.new_form_data ->> 'maxFundingAmount')::numeric IS NULL
OR
((select * from additional_funding_sources_exist) is not null
AND (select total from additional_funding_sources_sum) IS NULL)
) THEN NULL
else
(
coalesce(($1.new_form_data ->> 'proponentCost')::numeric, 0)
+
coalesce(($1.new_form_data ->> 'maxFundingAmount')::numeric, 0)
+
coalesce((select total from additional_funding_sources_sum), 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)
$$;

commit;
File renamed without changes.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
-- Revert cif:computed_columns/form_change_holdback_amount_to_date from pg
-- Deploy cif:computed_columns/form_change_holdback_amount_to_date to pg
-- requires: tables/form_change

begin;

drop function cif.form_change_holdback_amount_to_date(cif.form_change);
create or replace function cif.form_change_holdback_amount_to_date(parameter_fc cif.form_change)
returns numeric as
$fn$

select
round(sum(coalesce((fc.new_form_data->>'adjustedHoldbackAmount')::numeric, (fc.new_form_data->>'calculatedHoldbackAmount')::numeric, 0)), 2)
from cif.form_change fc
where fc.project_revision_id = $1.project_revision_id
and json_schema_name = 'milestone'
and (fc.new_form_data->>'hasExpenses')::boolean = true;

$fn$ language sql stable;

comment on function cif.form_change_holdback_amount_to_date(cif.form_change) is 'Computed column returns sum all holdback amounts for a project. Preference for value selection is adjustedHoldbackAmount > calculuatedHoldbackAmount > amount calculated via maximum milestone amount';

commit;
7 changes: 7 additions & 0 deletions schema/revert/computed_columns/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:computed_columns/form_change_holdback_amount_to_date from pg

begin;

drop function cif.form_change_holdback_amount_to_date(cif.form_change);

commit;
38 changes: 36 additions & 2 deletions schema/revert/computed_columns/form_change_total_project_value.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
-- Revert cif:computed_columns/form_change_total_project_value from pg
-- Deploy cif:computed_columns/form_change_total_project_value to pg

begin;

drop function cif.form_change_total_project_value;
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
(
(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;

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)
$$;

commit;
7 changes: 7 additions & 0 deletions schema/revert/computed_columns/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:computed_columns/form_change_total_project_value from pg

begin;

drop function cif.form_change_total_project_value;

commit;
2 changes: 2 additions & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,5 @@ tables/attachment_002_delete_permission 2023-07-31T18:06:38Z Brianna Cerkiewicz
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
computed_columns/form_change_holdback_amount_to_date_001 2023-07-24T20:08:21Z Gurjeet Matharu <[email protected]># Modified cif.form_change_holdback_amount_to_date function to return null if any input values are missing for any milestone
computed_columns/form_change_holdback_amount_to_date [computed_columns/[email protected]] 2023-07-27T22:18:03Z Gurjeet Matharu <[email protected]># Modified cif.form_change_holdback_amount_to_date function to return null if any input values are missing for any milestone
computed_columns/form_change_total_project_value [computed_columns/[email protected]] 2023-07-27T22:19:30Z 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_holdback_amount_to_date_001 on pg
-- Verify cif:computed_columns/form_change_holdback_amount_to_date on pg

begin;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Verify cif:computed_columns/form_change_total_project_value_001 on pg
-- Verify cif:computed_columns/form_change_total_project_value on pg

begin;

Expand Down

0 comments on commit f9275dd

Please sign in to comment.