2016-10-28 00:40:09 +02:00
|
|
|
-- CASCADE HANDLERS TABLE
|
|
|
|
ALTER TABLE Account.reportHandlers DROP FOREIGN KEY reportHandlers_reports_id_fk;
|
|
|
|
ALTER TABLE Account.reportHandlers
|
|
|
|
ADD CONSTRAINT reportHandlers_reports_id_fk
|
|
|
|
FOREIGN KEY (reportId) REFERENCES reports (id) ON DELETE CASCADE;
|
|
|
|
|
|
|
|
-- CASCADE REASONS/REPORTERS TABLE
|
|
|
|
ALTER TABLE Account.reportReasons DROP FOREIGN KEY reportReasons_reports_id_fk;
|
|
|
|
ALTER TABLE Account.reportReasons
|
|
|
|
ADD CONSTRAINT reportReasons_reports_id_fk
|
|
|
|
FOREIGN KEY (reportId) REFERENCES reports (id) ON DELETE CASCADE;
|
|
|
|
|
|
|
|
-- CASCADE RESULTS TABLE
|
|
|
|
ALTER TABLE Account.reportResults DROP FOREIGN KEY reportResults_reports_id_fk;
|
|
|
|
ALTER TABLE Account.reportResults
|
|
|
|
ADD CONSTRAINT reportResults_reports_id_fk
|
|
|
|
FOREIGN KEY (reportId) REFERENCES reports (id) ON DELETE CASCADE;
|
|
|
|
|
|
|
|
-- CASCADE SNAPSHOT MESSAGE MAP
|
|
|
|
ALTER TABLE Account.snapshotMessageMap DROP FOREIGN KEY snapshotMessageMap_snapshots_id_fk;
|
|
|
|
ALTER TABLE Account.snapshotMessageMap
|
|
|
|
ADD CONSTRAINT snapshotMessageMap_snapshots_id_fk
|
|
|
|
FOREIGN KEY (snapshotId) REFERENCES snapshots (id) ON DELETE CASCADE;
|
|
|
|
ALTER TABLE Account.snapshotMessageMap DROP FOREIGN KEY snapshotMessageMap_snapshotMessages_id_fk;
|
|
|
|
ALTER TABLE Account.snapshotMessageMap
|
|
|
|
ADD CONSTRAINT snapshotMessageMap_snapshotMessages_id_fk
|
|
|
|
FOREIGN KEY (messageId) REFERENCES snapshotMessages (id) ON DELETE CASCADE;
|
|
|
|
|
|
|
|
-- CASCADE SNAPSHOT RECIPIENTS TABLE
|
|
|
|
ALTER TABLE Account.snapshotRecipients DROP FOREIGN KEY snapshotRecipients_snapshotMessages_id_fk;
|
|
|
|
ALTER TABLE Account.snapshotRecipients
|
|
|
|
ADD CONSTRAINT snapshotRecipients_snapshotMessages_id_fk
|
|
|
|
FOREIGN KEY (messageId) REFERENCES snapshotMessages (id) ON DELETE CASCADE;
|
|
|
|
|
|
|
|
-- CREATE CLEANUP TASK
|
|
|
|
DELIMITER //
|
|
|
|
|
2016-10-28 19:13:20 +02:00
|
|
|
-- CREATE EVENT TO RUN EVERY DAY AT 00:00
|
2016-10-28 00:40:09 +02:00
|
|
|
CREATE EVENT `report_cleanup`
|
2016-10-28 19:13:20 +02:00
|
|
|
ON SCHEDULE
|
|
|
|
EVERY 1 DAY
|
|
|
|
-- FORCE TASK TO RUN AT 00:00 DAILY
|
|
|
|
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY)
|
2016-10-28 00:40:09 +02:00
|
|
|
ON COMPLETION PRESERVE
|
2016-10-28 19:13:20 +02:00
|
|
|
COMMENT 'Cleans up old report and snapshot data.'
|
2016-10-28 00:40:09 +02:00
|
|
|
DO BEGIN
|
|
|
|
-- DELETE REPORTS (AND ASSOCIATED SNAPSHOT IF ANY) CLOSED > 30 DAYS AGO
|
2016-10-28 19:13:20 +02:00
|
|
|
DELETE reports, snapshots FROM reports
|
2016-10-28 00:40:09 +02:00
|
|
|
LEFT JOIN reportResults ON reports.id = reportResults.reportId
|
|
|
|
LEFT JOIN snapshots ON reports.snapshotId = snapshots.id
|
|
|
|
WHERE reportResults.closedTime NOT BETWEEN NOW() - INTERVAL 30 DAY AND NOW();
|
|
|
|
|
|
|
|
-- DELETE ORPHANED SNAPSHOT MESSAGES
|
|
|
|
DELETE snapshotMessages FROM snapshotMessages
|
|
|
|
LEFT JOIN snapshotMessageMap ON snapshotMessages.id = snapshotMessageMap.messageId
|
|
|
|
WHERE snapshotMessageMap.snapshotId IS NULL;
|
|
|
|
END//
|