diff --git a/migrations/V195__MTO_suggested_milestone_trigger.sql b/migrations/V195__MTO_suggested_milestone_trigger.sql index 29d1b09400..e3bbab8120 100644 --- a/migrations/V195__MTO_suggested_milestone_trigger.sql +++ b/migrations/V195__MTO_suggested_milestone_trigger.sql @@ -3,7 +3,7 @@ DECLARE h_old hstore; h_new hstore; modified_by_id UUID; - model_plan_id UUID; + plan_id UUID; h_changed HSTORE; changedKeys text[]; BEGIN @@ -20,16 +20,16 @@ BEGIN changedKeys = akeys(h_changed); --Get the keys that have changed modified_by_id = h_new -> 'modified_by'; - model_plan_id = h_new -> 'model_plan_id'; - -- RAISE NOTICE 'SET_SUGGESTED_MTO_MILESTONE called. Modified_by_id %, model_plan_id = % and hstore = %', Modified_by_id,model_plan_id, h_new; -With SuggestedMilestone AS ( - SELECT key,model_plan_id, suggested FROM DETERMINE_MTO_MILESTONE_SUGGESTIONS(TG_TABLE_NAME::text, model_plan_id, h_new, changedKeys) --need to pass the hstore of the entire row to handle composite column trigger conditions + plan_id = h_new -> 'model_plan_id'; + -- RAISE NOTICE 'SET_SUGGESTED_MTO_MILESTONE called. Modified_by_id %, model_plan_id = % and hstore = %', Modified_by_id, plan_id, h_new; +With SuggestedMilestones AS ( + SELECT key,model_plan_id, suggested FROM DETERMINE_MTO_MILESTONE_SUGGESTIONS(TG_TABLE_NAME::text, plan_id, h_new, changedKeys) --need to pass the hstore of the entire row to handle composite column trigger conditions ) -- insert unmatched suggestions. Delete old suggestions MERGE INTO mto_suggested_milestone AS target USING SuggestedMilestones AS source -ON target.mto_common_milestone_key = source.mto_common_milestone_key +ON target.mto_common_milestone_key = source.key AND target.model_plan_id = source.model_plan_id WHEN MATCHED AND COALESCE(source.suggested, FALSE) <> TRUE THEN --Delete if it is not true. We use the COALESCE to also convert NULL to false diff --git a/migrations/V196__Add_MTO_Suggested_Milestone_Trigger_To_Table.sql b/migrations/V196__Add_MTO_Suggested_Milestone_Trigger_To_Table.sql new file mode 100644 index 0000000000..62702a598e --- /dev/null +++ b/migrations/V196__Add_MTO_Suggested_Milestone_Trigger_To_Table.sql @@ -0,0 +1,14 @@ +CREATE FUNCTION public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER(schema_name TEXT, table_name TEXT) RETURNS VOID AS $body$DECLARE + _q_txt TEXT; +BEGIN + EXECUTE 'DROP TRIGGER IF EXISTS mto_suggested_milestone_trigger ON ' || schema_name || '.' || table_name; + _q_txt = 'CREATE TRIGGER mto_suggested_milestone_trigger AFTER UPDATE ON ' || + schema_name || '.' || table_name || + ' FOR EACH ROW EXECUTE PROCEDURE public.SET_SUGGESTED_MTO_MILESTONE();'; + + RAISE NOTICE '%',_q_txt; + EXECUTE _q_txt; + + +END +$body$ LANGUAGE plpgsql; diff --git a/migrations/V197__Add_MTO_Suggested_Milestone_Triggers_To_Task_List.sql b/migrations/V197__Add_MTO_Suggested_Milestone_Triggers_To_Task_List.sql new file mode 100644 index 0000000000..ef404d9eca --- /dev/null +++ b/migrations/V197__Add_MTO_Suggested_Milestone_Triggers_To_Task_List.sql @@ -0,0 +1,11 @@ +-- SELECT public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER('public', 'plan_basics'); -- no records currently are based on basics + +SELECT public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER('public', 'plan_beneficiaries'); + +SELECT public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER('public', 'plan_general_characteristics'); + +SELECT public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER('public', 'plan_ops_eval_and_learning'); + +SELECT public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER('public', 'plan_participants_and_providers'); + +SELECT public.ADD_MTO_SUGGESTED_MILESTONE_SUGGESTION_TRIGGER('public', 'plan_payments'); diff --git a/pkg/storage/truncate.go b/pkg/storage/truncate.go index c9e0edcd67..d267a026a1 100644 --- a/pkg/storage/truncate.go +++ b/pkg/storage/truncate.go @@ -33,6 +33,7 @@ func (s *Store) TruncateAllTablesDANGEROUS(logger *zap.Logger) error { analyzed_audit, existing_model_link, mto_category, + mto_suggested_milestone, mto_milestone, mto_solution, mto_milestone_solution_link,