From 42009beb0cb0349fa7051dd6d9421960440693be Mon Sep 17 00:00:00 2001 From: poveyd <32199157+poveyd@users.noreply.github.com> Date: Tue, 28 Apr 2020 15:03:20 +0100 Subject: [PATCH 1/2] Update cycle_notification_history.sql --- .../cycle_notification_history.sql | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/database_maintenance/cycle_notification_history.sql b/database_maintenance/cycle_notification_history.sql index cc7698a09..8287a5011 100644 --- a/database_maintenance/cycle_notification_history.sql +++ b/database_maintenance/cycle_notification_history.sql @@ -2,8 +2,25 @@ CREATE TABLE notification_history_pivot AS SELECT * FROM notification_history WHERE 1=2; ALTER TABLE notification_history_pivot ADD PRIMARY KEY (id); --- CREATE TRIGGER update_pivot BEFORE UPDATE ON notification_history -create trigger to update notification_history_pivot when update to notification_history +-- Update values of notification_status, billable_units, updated_at, sent_by, sent_at based on new updates coming into the notification_history table. +CREATE OR REPLACE FUNCTION update_pivot_table() +RETURNS TRIGGER +LANGUAGE plpgsql AS +$$ +BEGIN + UPDATE notification_history_pivot SET notification_history_pivot.notification_status = NEW.notification_status, + notification_history_pivot.billable_units = NEW.billable_units, + notification_history_pivot.updated_at = NEW.updated_at, + notification_history_pivot.sent_by = NEW.sent_by, + notification_history_pivot.sent_at = NEW.sent_at + WHERE notification_history_pivot.id = NEW.id; +END +$$; + +DROP TRIGGER IF EXISTS update_pivot on notification_history; +CREATE TRIGGER update_pivot AFTER UPDATE OF notification_status, billable_units, updated_at, sent_by, sent_at ON notification_history +FOR EACH ROW + EXECUTE PROCEDURE update_pivot_table(); Create foreign key constraints in notification_history_pivot with dummy names - prefix with "nh_" From 3f2cc1df174e339b08c73e0fe46262a0e70048cf Mon Sep 17 00:00:00 2001 From: poveyd <32199157+poveyd@users.noreply.github.com> Date: Tue, 28 Apr 2020 15:42:38 +0100 Subject: [PATCH 2/2] Update cycle_notification_history.sql --- database_maintenance/cycle_notification_history.sql | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/database_maintenance/cycle_notification_history.sql b/database_maintenance/cycle_notification_history.sql index 8287a5011..463b2853b 100644 --- a/database_maintenance/cycle_notification_history.sql +++ b/database_maintenance/cycle_notification_history.sql @@ -8,12 +8,14 @@ RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN - UPDATE notification_history_pivot SET notification_history_pivot.notification_status = NEW.notification_status, - notification_history_pivot.billable_units = NEW.billable_units, - notification_history_pivot.updated_at = NEW.updated_at, - notification_history_pivot.sent_by = NEW.sent_by, - notification_history_pivot.sent_at = NEW.sent_at + UPDATE notification_history_pivot SET notification_status = NEW.notification_status, + billable_units = NEW.billable_units, + updated_at = NEW.updated_at, + sent_by = NEW.sent_by, + sent_at = NEW.sent_at WHERE notification_history_pivot.id = NEW.id; + + RETURN NEW; END $$;