From bd3cb6fff6f99b20cfa7ba9cac780a800685b2ce Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 25 Jan 2023 21:50:24 -0800 Subject: [PATCH] Status Checks - Add alert for bad foreign key --- CRM/Utils/Check/Component/Schema.php | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index a61288d4b3..8a4327f16c 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -176,6 +176,41 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { return $messages; } + /** + * The column 'civicrm_activity.original_id' should not have 'ON DELETE CASCADE'. + * It is OK to have 'ON DELETE SET NULL' or to have no constraint. + * + * @return CRM_Utils_Check_Message[] + */ + public function checkOldAcitvityCascade(): array { + $messages = []; + + $sql = "SELECT CONSTRAINT_NAME, DELETE_RULE + FROM information_schema.referential_constraints + WHERE CONSTRAINT_SCHEMA=database() AND TABLE_NAME='civicrm_activity' AND CONSTRAINT_NAME='FK_civicrm_activity_original_id' + "; + $cascades = CRM_Core_DAO::executeQuery($sql, [], FALSE, NULL, FALSE, FALSE) + ->fetchMap('CONSTRAINT_NAME', 'DELETE_RULE'); + $cascade = $cascades['FK_civicrm_activity_original_id'] ?? NULL; + if ($cascade === 'CASCADE') { + $docUrl = 'https://civicrm.org/redirect/activities-5.57'; + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__, + ts( + '

The table %1 includes an incorrect constraint. Learn how to fix this.', [ + 1 => 'civicrm_activity', + 2 => 'target="_blank" href="' . htmlentities($docUrl) . '"', + ] + ), + ts('Schema Error'), + \Psr\Log\LogLevel::WARNING, + 'fa-server' + ); + } + + return $messages; + } + /** * @return CRM_Utils_Check_Message[] */ -- 2.25.1