From 19952e0822d88155f4936f07668b77e23884833a Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 6 Dec 2019 09:14:56 +1100 Subject: [PATCH] dev/core#1447 Fix hard fail on upgrade due to duplicate names in the database when trying to fix the option group name --- CRM/Upgrade/Incremental/php/FiveTwentyOne.php | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/CRM/Upgrade/Incremental/php/FiveTwentyOne.php b/CRM/Upgrade/Incremental/php/FiveTwentyOne.php index 6cbffc39d9..499aa9a68f 100644 --- a/CRM/Upgrade/Incremental/php/FiveTwentyOne.php +++ b/CRM/Upgrade/Incremental/php/FiveTwentyOne.php @@ -40,10 +40,20 @@ class CRM_Upgrade_Incremental_php_FiveTwentyOne extends CRM_Upgrade_Incremental_ * an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs. */ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { - // Example: Generate a post-upgrade message. - // if ($rev == '5.12.34') { - // $postUpgradeMessage .= '

' . ts("By default, CiviCRM now disables the ability to import directly from SQL. To use this feature, you must explicitly grant permission 'import SQL datasource'."); - // } + if ($rev == '5.21.alpha1') { + // Find any option groups that were not converted during the upgrade. + $notConverted = []; + $optionGroups = \Civi\Api4\OptionGroup::get()->setCheckPermissions(FALSE)->execute(); + foreach ($optionGroups as $optionGroup) { + $trimmedName = trim($optionGroup['name']); + if (strpos($trimmedName, ' ') !== FALSE) { + $notConverted[] = $optionGroup['title']; + } + } + if (count($notConverted)) { + $postUpgradeMessage .= '

' . ts("The Following option Groups have not been converted due to there being already another option group with the same name in the database") . ""; + } + } } /* @@ -83,12 +93,19 @@ class CRM_Upgrade_Incremental_php_FiveTwentyOne extends CRM_Upgrade_Incremental_ $name = trim($optionGroup['name']); if (strpos($name, ' ') !== FALSE) { $fixedName = CRM_Utils_String::titleToVar(strtolower($name)); - \Civi::log()->debug('5.21 Upgrade Option Group name ' . $name . ' converted to ' . $fixedName); - \Civi\Api4\OptionGroup::update() - ->addWhere('id', '=', $optionGroup['id']) - ->addValue('name', $fixedName) + $check = \Civi\Api4\OptionGroup::get() + ->addWhere('name', '=', $fixedName) ->setCheckPermissions(FALSE) ->execute(); + // Fix hard fail in upgrade due to name already in database dev/core#1447 + if (!count($check)) { + \Civi::log()->debug('5.21 Upgrade Option Group name ' . $name . ' converted to ' . $fixedName); + \Civi\Api4\OptionGroup::update() + ->addWhere('id', '=', $optionGroup['id']) + ->addValue('name', $fixedName) + ->setCheckPermissions(FALSE) + ->execute(); + } } } return TRUE; -- 2.25.1