From 6937a359130209c4a49838b99af3ff19b08d7282 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Wed, 18 Jan 2023 22:25:23 -0500 Subject: [PATCH 1/3] avoid upgrade taking hours --- .../Incremental/php/FiveFiftySeven.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/CRM/Upgrade/Incremental/php/FiveFiftySeven.php b/CRM/Upgrade/Incremental/php/FiveFiftySeven.php index 08f80045a013..9cbccce5d2a6 100644 --- a/CRM/Upgrade/Incremental/php/FiveFiftySeven.php +++ b/CRM/Upgrade/Incremental/php/FiveFiftySeven.php @@ -21,11 +21,24 @@ */ class CRM_Upgrade_Incremental_php_FiveFiftySeven extends CRM_Upgrade_Incremental_Base { + /** + * How many activities before the queries used here are slow. Guessing. + */ + const ACTIVITY_THRESHOLD = 1000000; + public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { if ($rev === '5.57.alpha1') { - if (CRM_Core_DAO::singleValueQuery('SELECT COUNT(id) FROM civicrm_activity WHERE is_current_revision = 0')) { + // The query on is_current_revision is slow if there's a lot of activities. So limit when it gets run. + $activityCount = CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_activity'); + if ($activityCount < self::ACTIVITY_THRESHOLD && CRM_Core_DAO::singleValueQuery('SELECT COUNT(id) FROM civicrm_activity WHERE is_current_revision = 0')) { $preUpgradeMessage .= '

' . ts('Your database contains CiviCase activity revisions which are deprecated and will begin to appear as duplicates in SearchKit/api4/etc.

', [1 => 'target="_blank" href="https://lab.civicrm.org/-/snippets/85"']) . '

'; } + // Similarly the original_id ON DELETE drop+recreate is slow, so if we + // don't add the task farther down below, then tell people what to do at + // their convenience. + elseif ($activityCount >= self::ACTIVITY_THRESHOLD) { + $preUpgradeMessage .= '

' . ts('Your database contains too many activities to efficiently run a query needed to check for deprecated case activity revisions and to fix a bad foreign key constraint and it may take hours to run. You can run these queries manually at your convenience:

', [1 => 'target="_blank" href="https://lab.civicrm.org/-/snippets/85"']) . '

'; + } } } @@ -37,7 +50,9 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU */ public function upgrade_5_57_alpha1($rev): void { $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); - $this->addTask('Fix dangerous delete cascade', 'fixDeleteCascade'); + if (CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_activity') < self::ACTIVITY_THRESHOLD) { + $this->addTask('Fix dangerous delete cascade', 'fixDeleteCascade'); + } $this->addExtensionTask('Enable SearchKit extension', ['org.civicrm.search_kit'], 1100); $this->addExtensionTask('Enable Flexmailer extension', ['org.civicrm.flexmailer']); } From 0dd3cd8d3265379b7e90f732d1158acb87b32cf4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 25 Jan 2023 20:33:11 -0800 Subject: [PATCH 2/3] FiveFiftySeven - Point static link to adjustable redirect --- CRM/Upgrade/Incremental/php/FiveFiftySeven.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CRM/Upgrade/Incremental/php/FiveFiftySeven.php b/CRM/Upgrade/Incremental/php/FiveFiftySeven.php index 9cbccce5d2a6..62ca200c9f4a 100644 --- a/CRM/Upgrade/Incremental/php/FiveFiftySeven.php +++ b/CRM/Upgrade/Incremental/php/FiveFiftySeven.php @@ -28,16 +28,19 @@ class CRM_Upgrade_Incremental_php_FiveFiftySeven extends CRM_Upgrade_Incremental public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { if ($rev === '5.57.alpha1') { + $docUrl = 'https://civicrm.org/redirect/activities-5.57'; + $docAnchor = 'target="_blank" href="' . htmlentities($docUrl) . '"'; + // The query on is_current_revision is slow if there's a lot of activities. So limit when it gets run. $activityCount = CRM_Core_DAO::singleValueQuery('SELECT MAX(id) FROM civicrm_activity'); if ($activityCount < self::ACTIVITY_THRESHOLD && CRM_Core_DAO::singleValueQuery('SELECT COUNT(id) FROM civicrm_activity WHERE is_current_revision = 0')) { - $preUpgradeMessage .= '

' . ts('Your database contains CiviCase activity revisions which are deprecated and will begin to appear as duplicates in SearchKit/api4/etc.

', [1 => 'target="_blank" href="https://lab.civicrm.org/-/snippets/85"']) . '

'; + $preUpgradeMessage .= '

' . ts('Your database contains CiviCase activity revisions which are deprecated and will begin to appear as duplicates in SearchKit/api4/etc.

', [1 => $docAnchor]) . '

'; } // Similarly the original_id ON DELETE drop+recreate is slow, so if we // don't add the task farther down below, then tell people what to do at // their convenience. elseif ($activityCount >= self::ACTIVITY_THRESHOLD) { - $preUpgradeMessage .= '

' . ts('Your database contains too many activities to efficiently run a query needed to check for deprecated case activity revisions and to fix a bad foreign key constraint and it may take hours to run. You can run these queries manually at your convenience:

', [1 => 'target="_blank" href="https://lab.civicrm.org/-/snippets/85"']) . '

'; + $preUpgradeMessage .= '

' . ts('Your database contains too many activities to efficiently run a query needed to check for deprecated case activity revisions and to fix a bad foreign key constraint and it may take hours to run. You can run these queries manually at your convenience:

', [1 => $docAnchor]) . '

'; } } } From abd178f209795ff0c3ba022cada012e04fa8091c Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 25 Jan 2023 21:09:05 -0800 Subject: [PATCH 3/3] FiveFiftySeven - Simplify call to action. --- CRM/Upgrade/Incremental/php/FiveFiftySeven.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Upgrade/Incremental/php/FiveFiftySeven.php b/CRM/Upgrade/Incremental/php/FiveFiftySeven.php index 62ca200c9f4a..eb0464079d9e 100644 --- a/CRM/Upgrade/Incremental/php/FiveFiftySeven.php +++ b/CRM/Upgrade/Incremental/php/FiveFiftySeven.php @@ -40,7 +40,7 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU // don't add the task farther down below, then tell people what to do at // their convenience. elseif ($activityCount >= self::ACTIVITY_THRESHOLD) { - $preUpgradeMessage .= '

' . ts('Your database contains too many activities to efficiently run a query needed to check for deprecated case activity revisions and to fix a bad foreign key constraint and it may take hours to run. You can run these queries manually at your convenience:

  • SELECT COUNT(id) FROM `civicrm_activity` WHERE `is_current_revision` = 0;
  • ALTER TABLE `civicrm_activity` DROP FOREIGN KEY `FK_civicrm_activity_original_id`;
  • ALTER TABLE `civicrm_activity` ADD CONSTRAINT `FK_civicrm_activity_original_id` FOREIGN KEY (`original_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE SET NULL;
  • For more information see this Lab Snippet.
', [1 => $docAnchor]) . '

'; + $preUpgradeMessage .= '

' . ts('The activity table will not update automatically because it contains too many records. You will need to apply a manual update. Please read about how to clean data from the defunct "Embedded Activity Revisions" setting.', [1 => $docAnchor]) . '

'; } } }