From bab2d80ada184a16ab700bfd9789684a9d3fac93 Mon Sep 17 00:00:00 2001 From: Ravi Kumar Kempapura Srinivasa Date: Mon, 15 Feb 2021 16:43:23 +0100 Subject: [PATCH] Add foreign key with cascade delete constraints to history tables Earlier we did not have any foreign keys for history table. But when we delete a record from the parent history table the corresponding records in their child tables must also be deleted. This is done with the introduction of foreign key constraints with on cascade delete. --- schema/mysql/schema.sql | 11 ++++++++++- schema/mysql/upgrades/1.0.0-rc2.sql | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 832764da..02f89a90 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -915,7 +915,9 @@ CREATE TABLE user_notification_history ( notification_history_id binary(16) NOT NULL COMMENT 'UUID notification_history.id', user_id binary(20) NOT NULL COMMENT 'user.id', - PRIMARY KEY (id) + PRIMARY KEY (id), + + CONSTRAINT fk_user_notification_history_notification_history FOREIGN KEY (notification_history_id) REFERENCES notification_history (id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC; CREATE TABLE state_history ( @@ -1049,6 +1051,13 @@ CREATE TABLE history ( PRIMARY KEY (id), + CONSTRAINT fk_history_acknowledgement_history FOREIGN KEY (acknowledgement_history_id) REFERENCES acknowledgement_history (id) ON DELETE CASCADE, + CONSTRAINT fk_history_comment_history FOREIGN KEY (comment_history_id) REFERENCES comment_history (comment_id) ON DELETE CASCADE, + CONSTRAINT fk_history_downtime_history FOREIGN KEY (downtime_history_id) REFERENCES downtime_history (downtime_id) ON DELETE CASCADE, + CONSTRAINT fk_history_flapping_history FOREIGN KEY (flapping_history_id) REFERENCES flapping_history (id) ON DELETE CASCADE, + CONSTRAINT fk_history_notification_history FOREIGN KEY (notification_history_id) REFERENCES notification_history (id) ON DELETE CASCADE, + CONSTRAINT fk_history_state_history FOREIGN KEY (state_history_id) REFERENCES state_history (id) ON DELETE CASCADE, + INDEX idx_history_event_time (event_time) COMMENT 'History filtered/ordered by event_time', INDEX idx_history_acknowledgement (acknowledgement_history_id), INDEX idx_history_comment (comment_history_id), diff --git a/schema/mysql/upgrades/1.0.0-rc2.sql b/schema/mysql/upgrades/1.0.0-rc2.sql index 1f402802..e69f51fc 100644 --- a/schema/mysql/upgrades/1.0.0-rc2.sql +++ b/schema/mysql/upgrades/1.0.0-rc2.sql @@ -147,3 +147,14 @@ ALTER TABLE service_state ALTER TABLE user_notification_history MODIFY id binary(20) NOT NULL COMMENT 'sha1(notification_history_id + user_id)'; + +ALTER TABLE history + ADD CONSTRAINT fk_history_acknowledgement_history FOREIGN KEY (acknowledgement_history_id) REFERENCES acknowledgement_history (id) ON DELETE CASCADE, + ADD CONSTRAINT fk_history_comment_history FOREIGN KEY (comment_history_id) REFERENCES comment_history (comment_id) ON DELETE CASCADE, + ADD CONSTRAINT fk_history_downtime_history FOREIGN KEY (downtime_history_id) REFERENCES downtime_history (downtime_id) ON DELETE CASCADE, + ADD CONSTRAINT fk_history_flapping_history FOREIGN KEY (flapping_history_id) REFERENCES flapping_history (id) ON DELETE CASCADE, + ADD CONSTRAINT fk_history_notification_history FOREIGN KEY (notification_history_id) REFERENCES notification_history (id) ON DELETE CASCADE, + ADD CONSTRAINT fk_history_state_history FOREIGN KEY (state_history_id) REFERENCES state_history (id) ON DELETE CASCADE; + +ALTER TABLE user_notification_history + ADD CONSTRAINT fk_user_notification_history_notification_history FOREIGN KEY (notification_history_id) REFERENCES notification_history (id) ON DELETE CASCADE;