2020-04-28 10:20:13 +01:00
-- Create new notification_history table and primary key.
CREATE TABLE notification_history_pivot AS SELECT * FROM notification_history WHERE 1 = 2 ;
ALTER TABLE notification_history_pivot ADD PRIMARY KEY ( id ) ;
2020-04-28 09:45:09 +01:00
2020-04-28 15:03:20 +01:00
-- 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
2020-04-28 15:42:38 +01:00
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
2020-04-28 15:03:20 +01:00
WHERE notification_history_pivot . id = NEW . id ;
2020-04-28 15:42:38 +01:00
RETURN NEW ;
2020-04-28 15:03:20 +01:00
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 ( ) ;
2020-04-28 09:45:09 +01:00
2020-04-28 11:43:52 +01:00
Create foreign key constraints in notification_history_pivot with dummy names - prefix with " nh_ "
2020-04-28 10:20:13 +01:00
2020-04-28 10:21:51 +01:00
insert data into notification_history_pivot in batches
2020-04-28 09:45:09 +01:00
2020-04-28 10:21:51 +01:00
create remaining indices on notification_history_pivot
2020-04-28 10:20:13 +01:00
-- Run basic sanity checks on data in notification_history_pivot, ensuring same number of entries present in notification_history and notification_history_pivot
2020-04-28 11:21:11 +01:00
ALTER TABLE notification_history RENAME TO notification_history_old ;
2020-04-28 11:43:52 +01:00
Drop constraints on notification_history_old
2020-04-28 11:21:11 +01:00
ALTER TABLE notification_history_pivot RENAME TO notification_history ;
2020-04-28 11:43:52 +01:00
Create foreign key constraints in notification_history using correct names . Names of constraints will now be available after step above ( " Drop constraints on notification_history_old " ) .
Drop constraints in notification_history that are prefixed with " nh_ " .
2020-04-28 10:20:13 +01:00
-- Run sanity checks on data in notification_history_pivot
-- 1. Ensure same number of entries in notification_history and notification_history_pivot
-- If not then reconcile entries and add remaining entries to notification_history
-- When sure data in new notification_history table are ok:
2020-04-28 11:21:11 +01:00
ALTER TABLE notification_history_old DROP CONSTRAINT notification_history_pkey ; -- May not need this step.
DROP TABLE notification_history_old ;
-- Must rename primary key on new notification_history table or will get issues running this script in the future.
-- This is not very nice.
ALTER TABLE notification_history DROP CONSTRAINT notification_history_pivot_pkey ;
ALTER TABLE notification_history ADD PRIMARY KEY ( id ) ; -- Will create a new primary key named "notification_history_pkey".
-- Note that the only impact of these changes