Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid 5.57 upgrade taking hours on large activity tables #25380

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions CRM/Upgrade/Incremental/php/FiveFiftySeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<p>' . ts('Your database contains CiviCase activity revisions which are deprecated and will begin to appear as duplicates in SearchKit/api4/etc.<ul><li>For further instructions see this <a %1>Lab Snippet</a>.</li></ul>', [1 => 'target="_blank" href="https://lab.civicrm.org/-/snippets/85"']) . '</p>';
}
// 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 .= '<p>' . 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:<ul><li>SELECT COUNT(id) FROM `civicrm_activity` WHERE `is_current_revision` = 0;</li><li>ALTER TABLE `civicrm_activity` DROP FOREIGN KEY `FK_civicrm_activity_original_id`;</li><li>ALTER TABLE `civicrm_activity` ADD CONSTRAINT `FK_civicrm_activity_original_id` FOREIGN KEY (`original_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE SET NULL;</li><li>For more information see this <a %1>Lab Snippet</a>.</li></ul>', [1 => 'target="_blank" href="https://lab.civicrm.org/-/snippets/85"']) . '</p>';
}
}
}

Expand All @@ -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']);
}
Expand Down