From 4303eba983ce7cad13e14444d19867293ebc8b3d Mon Sep 17 00:00:00 2001 From: poveyd <32199157+poveyd@users.noreply.github.com> Date: Tue, 28 Apr 2020 11:21:11 +0100 Subject: [PATCH] Update cycle_notification_history.sql --- .../cycle_notification_history.sql | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/database_maintenance/cycle_notification_history.sql b/database_maintenance/cycle_notification_history.sql index f88dcaa0d..4b34e3b4f 100644 --- a/database_maintenance/cycle_notification_history.sql +++ b/database_maintenance/cycle_notification_history.sql @@ -2,6 +2,7 @@ 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 create foreign key constraints in notification_history_pivot @@ -12,12 +13,20 @@ create remaining indices on notification_history_pivot -- Run basic sanity checks on data in notification_history_pivot, ensuring same number of entries present in notification_history and notification_history_pivot -rename table notification_history to notification_history_old -rename table notification_history_pivot to notification_history; +ALTER TABLE notification_history RENAME TO notification_history_old; +ALTER TABLE notification_history_pivot RENAME TO notification_history; -- 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: -drop table notification_history; +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