Skip to content

Commit

Permalink
feat: migration function to move the existing milestone form_changes …
Browse files Browse the repository at this point in the history
…to the new schema
  • Loading branch information
pbastia committed Oct 28, 2022
1 parent 11919c7 commit 39a26df
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-- Deploy cif:functions/migration_milestone_form_changes_to_single_form_change to pg

begin;

create or replace function cif_private.migration_milestone_form_changes_to_single_form_change()
returns void as
$migration$



with milestone_form_changes as (
select
reporting_requirement_form_change.id,
coalesce(reporting_requirement_form_change.new_form_data, '{}'::jsonb) as reporting_requirement_data,
payment_form_change.id,
coalesce(payment_form_change.new_form_data, '{}'::jsonb) as payment_data,
milestone_form_change.id,
coalesce(milestone_form_change.new_form_data, '{}'::jsonb) as milestone_data
from cif.form_change as reporting_requirement_form_change
where form_data_table_name = 'reporting_requirement'
and json_schema_name = 'reporting_requirement'
and new_form_data->>'reportType' in ('General Milestone', 'Advanced Milestone', 'Reporting Milestone')
left join cif.form_change as payment_form_change
on payment_form_change.new_form_data->>'reportingRequirementId'::numeric = reporting_requirement_form_change.form_data_record_id
and payment_form_change.new_form_data->>'reportingRequirementId'::numeric is not null
left join cif.form_change as milestone_form_change
on milestone_form_change.new_form_data->>'reportingRequirementId'::numeric = reporting_requirement_form_change.form_data_record_id
and milestone_form_change.new_form_data->>'reportingRequirementId'::numeric is not null
),
new_data as (
select
reporting_requirement_form_change.id as id,
json_strips_null(
jsonb_build_object(
'reportingRequirementIndex', reporting_requirement_data->>'reportingRequirementIndex',
'description', reporting_requirement_data->>'description',
'reportType', reporting_requirement_data->>'reportType',
'hasExpenses', (select has_expenses from cif.report_type where name=milestone_data->>'reportType'),
'reportDueDate', reporting_requirement_data->>'reportDueDate',
'submittedDate', reporting_requirement_data->>'submittedDate',
'substantialCompletionDate', milestone_data->>'substantialCompletionDate',
'certifiedBy', milestone_data->>'certifiedBy',
'certifierProfessionalDesignation', milestone_data->>'certifierProfessionalDesignation',
'maximumAmount', milestone_data->>'maximumAmount',
'totalEligibleExpenses', milestone_data->>'totalEligibleExpenses',
'calculatedGrossAmount', null
'calculatedNetAmount', null
'adjustedGrossAmount', payment_data->>'adjustedGrossAmount',
'adjustedNetAmount', payment_data->>'adjustedNetAmount',
'dateSentToCsnr', payment_data->>'dateSentToCsnr'
)
) as new_form_data
from milestone_form_changes
),
archived_form_changes as (
update cif.form_change set archived_at = now() where id in (
select payment_form_change.id as id from milestone_form_changes
union
select milestone_form_change.id as id from milestone_form_changes
) returning *
)
update cif.form_change
set
new_form_data = new_data.new_form_data,
json_schema_name = 'milestone'
from new_data
where id=new_data.id





$migration$ language sql volatile;

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Deploy cif:migrations/001_milestone_form_changes_to_single_form_change to pg

begin;

alter table cif.form_change disable trigger _100_committed_changes_are_immutable, disable trigger _100_timestamps;

select cif_private.migration_milestone_form_changes_to_single_form_change();

alter table cif.form_change enable trigger _100_committed_changes_are_immutable, enable trigger _100_timestamps;

end;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:functions/migration_milestone_form_changes_to_single_form_change from pg

begin;

drop function cif_private.migration_milestone_form_changes_to_single_form_change;

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:migrations/001_milestone_form_change_to_single_form_change from pg

begin;

-- nothing to revert, this is a one-way migration only.

commit;
2 changes: 2 additions & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,5 @@ functions/handle_milestone_form_change_commit [tables/form_change] 2022-08-30T21
tables/form_change_001 [tables/form_change tables/form] 2022-09-01T22:26:46Z Dylan Leard <[email protected]> # Migration: json_schema_name is now a foreign key to the form table
mutations/add_milestone_to_revision [mutations/[email protected]] 2022-09-12T17:26:22Z Pierre Bastianelli <[email protected]> # Removing unused custom mutation
mutations/discard_milestone_form_change [mutations/[email protected]] 2022-09-12T17:38:11Z Pierre Bastianelli <[email protected]> # Removing unnecessary discard custom mutation
migrations/001_milestone_form_changes_to_single_form_change 2022-10-05T21:24:11Z Pierre Bastianelli <[email protected]> # Migration function and call to move all truples(reporting_requirement, milestone, payment) to a single milestone form_change
functions/migration_milestone_form_changes_to_single_form_change 2022-10-05T21:33:26Z Pierre Bastianelli <[email protected]> # idempotent function called by the migration with the same name. Aggregates the multiple form_changes for milestones(reporting_requirement, payment, milestone) into a new, single milestone form_change
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


begin;


select plan(99);


-- it takes the milestone data from existing revisions and puts them into the new json schema

-- it removes null values from the form data

-- it archives both form_changes for milestone_report and payment tables

-- it transforms the existing form_change for the reporting_requirement into the new format

-- it doesn't touch the form changes not in a revision

-- it is




select finish();

rollback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify cif:functions/migration_milestone_form_changes_to_single_form_change on pg

begin;

select pg_get_functiondef('cif_private.migration_milestone_form_changes_to_single_form_change()'::regprocedure);

rollback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Verify cif:migrations/001_milestone_form_changes_to_single_form_change on pg

begin;

-- If the migration has been applied, there are no form_changes for milestone_report or payment tables that are not archived.


do $$
begin
assert (
select count(*) = 0
from cif.form_change
where
(form_data_table_name='milestone_report' or form_data_table_name='payment')
and archived_at is null
), 'there are no form_changes for milestone_report or payment tables that are not archived';
end;
$$;


rollback;

0 comments on commit 39a26df

Please sign in to comment.