Skip to content

Commit

Permalink
feat: first pass working trigger to merge into the suggested mileston…
Browse files Browse the repository at this point in the history
…e table
  • Loading branch information
StevenWadeOddball committed Dec 5, 2024
1 parent 720d7d2 commit d05aa97
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions migrations/V195__MTO_suggested_milestone_trigger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions migrations/V196__Add_MTO_Suggested_Milestone_Trigger_To_Table.sql
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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');
1 change: 1 addition & 0 deletions pkg/storage/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d05aa97

Please sign in to comment.