From 41db2e21a166ad604820f6f912fe94777abd7305 Mon Sep 17 00:00:00 2001 From: colemanw Date: Thu, 14 Dec 2023 20:47:58 -0500 Subject: [PATCH] RelationshipCache - Add status check to ensure cache table is up-to-date --- CRM/Utils/Check/Component/Schema.php | 36 +++++++++++++++++-- .../Api4/Action/RelationshipCache/Rebuild.php | 24 +++++++++++++ Civi/Api4/RelationshipCache.php | 9 +++++ ang/crmStatusPage/StatusPageCtrl.js | 5 +++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Civi/Api4/Action/RelationshipCache/Rebuild.php diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index 5244d453dc..fbaea4943d 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -259,7 +259,7 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { } /** - * Check the function to populate phone_numeric exists. + * Check the SQL trigger to populate `civicrm_relationship_cache` exists. * * @return array|\CRM_Utils_Check_Message[] */ @@ -275,7 +275,7 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { ts("Your database is missing functionality to populate the relationship cache."), ts('Missing Relationship Cache Trigger'), \Psr\Log\LogLevel::WARNING, - 'fa-server' + 'fa-database' ); $msg->addAction( ts('Rebuild triggers'), @@ -288,4 +288,36 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { return []; } + /** + * Verify `civicrm_relationship_cache` table contains the right amount of data. + * + * @return array|\CRM_Utils_Check_Message[] + */ + public function checkRelationshipCacheData(): array { + $relationshipCount = (int) CRM_Core_DAO::singleValueQuery("SELECT COUNT(`id`) FROM `civicrm_relationship`"); + $cacheCount = (int) CRM_Core_DAO::singleValueQuery("SELECT COUNT(`id`) FROM `civicrm_relationship_cache`"); + $expectedCount = 2 * $relationshipCount; + if ($cacheCount !== $expectedCount) { + $msg = new CRM_Utils_Check_Message( + __FUNCTION__, + ts("Your database is missing relationship cache data; this can cause related contact information to not show when it should.") . + '', + ts('Missing Relationship Cache Data'), + \Psr\Log\LogLevel::WARNING, + 'fa-database' + ); + $msg->addAction( + ts('Rebuild cache'), + '

' . ts('Rebuild relationship cache now? This may take a few minutes.') . '

' . + '

' . ts('Note: on very large databases it may be necessary to run this via cli instead to avoid timeouts:') . '

' . + '
cv api4 RelationshipCache.rebuild
', + 'api4', + ['RelationshipCache', 'rebuild'] + ); + return [$msg]; + } + return []; + } + } diff --git a/Civi/Api4/Action/RelationshipCache/Rebuild.php b/Civi/Api4/Action/RelationshipCache/Rebuild.php new file mode 100644 index 0000000000..d852834db5 --- /dev/null +++ b/Civi/Api4/Action/RelationshipCache/Rebuild.php @@ -0,0 +1,24 @@ +setCheckPermissions($checkPermissions); } + /** + * @param bool $checkPermissions + * @return Action\RelationshipCache\Rebuild + */ + public static function rebuild($checkPermissions = TRUE) { + return (new Action\RelationshipCache\Rebuild(__CLASS__, __FUNCTION__)) + ->setCheckPermissions($checkPermissions); + } + /** * @return array */ diff --git a/ang/crmStatusPage/StatusPageCtrl.js b/ang/crmStatusPage/StatusPageCtrl.js index 5dad04fc59..737518d190 100644 --- a/ang/crmStatusPage/StatusPageCtrl.js +++ b/ang/crmStatusPage/StatusPageCtrl.js @@ -57,6 +57,11 @@ case 'api3': refresh([action.params], action.title); break; + + case 'api4': + $('#crm-status-list').block(); + CRM.api4([action.params]).then(() => refresh()); + break; } } -- 2.25.1