Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/functions/V0.0.031__get_intervention_data_as_json.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE or replace function get_intervention_data_as_json(int_id integer) returns jsonb
CREATE or replace function get_intervention_data_as_json(int_id uuid) returns jsonb
AS
$$
DECLARE
Expand Down
18 changes: 9 additions & 9 deletions db/functions/V0.0.032__update_intervention_data_table.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
CREATE or replace function update_intervention_data_table(int_id integer) returns void
CREATE or replace function update_intervention_data_table(int_id uuid) returns void

AS $$

declare
declare

update_sql text;

Expand All @@ -13,14 +13,14 @@ c cursor for
order by row_index;

begin

execute update_intervention_data_totals(int_id);

-- loop through json object updating table columns
for r in c loop

update_sql := 'UPDATE intervention_data a
SET
SET
year_0 = (b.intervention_data ->> ''' || r.row_index || '_0'')::numeric
,year_1 = (b.intervention_data ->> ''' || r.row_index || '_1'')::numeric
,year_2 = (b.intervention_data ->> ''' || r.row_index || '_2'')::numeric
Expand All @@ -32,14 +32,14 @@ begin
,year_8 = (b.intervention_data ->> ''' || r.row_index || '_8'')::numeric
,year_9 = (b.intervention_data ->> ''' || r.row_index || '_9'')::numeric
from intervention_data_json b
WHERE
WHERE
a.intervention_id = b.intervention_id
and a.intervention_id = ' || int_id || '
and a.row_index = ' || r.row_index
;

--Raise Notice'sql string: %',update_sql;

EXECUTE update_sql;

end loop;
Expand All @@ -49,4 +49,4 @@ end;
$$ LANGUAGE plpgsql;


comment on function update_intervention_data_table is 'This function updates the intervention costing data values in the table columns named as years 0 to 9 from the table which contains the equivalent json data as recalculated by a previous function.';
comment on function update_intervention_data_table is 'This function updates the intervention costing data values in the table columns named as years 0 to 9 from the table which contains the equivalent json data as recalculated by a previous function.';
14 changes: 7 additions & 7 deletions db/functions/V0.0.035__test_json_vals.sql
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
CREATE or replace function test_json_vals(int_id integer) returns void
CREATE or replace function test_json_vals(int_id uuid) returns void

AS $$

declare
declare

cells_all jsonb;

i record;
value_2 text;

begin

RAISE NOTICE '%', 'start checking...';

cells_all := get_intervention_data_as_json(int_id);

FOR i IN select * from jsonb_each_text(cells_all)
loop

select intervention_data ->> i.key into value_2
from intervention_data_json
where intervention_id = int_id;

if (round(i.value::numeric) <> round(value_2::numeric))
or (i.value is null and value_2 is not null)
or (i.value is not null and value_2 is null) then
Expand All @@ -33,9 +33,9 @@ cells_all := get_intervention_data_as_json(int_id);
END LOOP;

RAISE NOTICE '%', 'checking ended.';

end;

$$ LANGUAGE plpgsql;

comment on function test_json_vals is 'This function can be used to test the function which recalculates total values ''update_intervention_data_totals'' by comparing the original values to the recalculated values. It depends on the calculated values not being saved back to the table so run the above function but do not run ''update_intervention_data_table'' until tested.';
comment on function test_json_vals is 'This function can be used to test the function which recalculates total values ''update_intervention_data_totals'' by comparing the original values to the recalculated values. It depends on the calculated values not being saved back to the table so run the above function but do not run ''update_intervention_data_table'' until tested.';
Loading