From 1fbda76b7250f4f68204b2928c1c2d549704c3b4 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 1 Jul 2020 17:31:36 +1200 Subject: [PATCH] Hacks to avoid failure on upgrade due to new field not yet being in the DB On attempting to upgrade I found I couldn't get to the upgrade screen add the new serialize field as I got fatal errors in these 2 places (maybe more to come). The new field is civicrm_custom_field.serialize --- CRM/Core/BAO/CustomField.php | 7 ++++++- CRM/Core/BAO/Domain.php | 13 +++++++++++++ CRM/Utils/Check/Component/Env.php | 4 +--- CRM/Utils/Check/Component/Schema.php | 4 ++++ api/v3/CustomField.php | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 7c6350553b..140f1e0a53 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -278,6 +278,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { * * @return array * an array of active custom fields. + * @throws \CRM_Core_Exception */ public static function &getFields( $customDataType = 'Individual', @@ -392,6 +393,10 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { if ($onlyParent) { $extends .= " AND $cgTable.extends_entity_column_value IS NULL AND $cgTable.extends_entity_column_id IS NULL "; } + // Temporary hack - in 5.27 a new field is added to civicrm_custom_field. There is a high + // risk this function is called before the upgrade page can be reached and if + // so it will potentially result in fatal error. + $serializeField = CRM_Core_BAO_Domain::isDBUpdateRequired() ? '' : "$cfTable.serialize,"; $query = "SELECT $cfTable.id, $cfTable.label, $cgTable.title, @@ -410,7 +415,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField { $cfTable.date_format, $cfTable.time_format, $cgTable.is_multiple, - $cfTable.serialize, + $serializeField $cgTable.table_name, og.name as option_group_name FROM $cfTable diff --git a/CRM/Core/BAO/Domain.php b/CRM/Core/BAO/Domain.php index 5f1f508cba..acbb2a93fd 100644 --- a/CRM/Core/BAO/Domain.php +++ b/CRM/Core/BAO/Domain.php @@ -82,6 +82,19 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { ); } + /** + * Is a database update required to apply latest schema changes. + * + * @return bool + * + * @throws \CRM_Core_Exception + */ + public static function isDBUpdateRequired() { + $dbVersion = CRM_Core_BAO_Domain::version(); + $codeVersion = CRM_Utils_System::version(); + return version_compare($dbVersion, $codeVersion) < 0; + } + /** * Get the location values of a domain. * diff --git a/CRM/Utils/Check/Component/Env.php b/CRM/Utils/Check/Component/Env.php index 4753496c8c..c37dcff4b5 100644 --- a/CRM/Utils/Check/Component/Env.php +++ b/CRM/Utils/Check/Component/Env.php @@ -739,10 +739,8 @@ class CRM_Utils_Check_Component_Env extends CRM_Utils_Check_Component { ); } else { - $codeVersion = CRM_Utils_System::version(); - // if db.ver < code.ver, time to upgrade - if (version_compare($dbVersion, $codeVersion) < 0) { + if (CRM_Core_BAO_Domain::isDBUpdateRequired()) { $messages[] = new CRM_Utils_Check_Message( __FUNCTION__, ts('New codebase version detected. You must visit upgrade screen to upgrade the database.', [1 => $upgradeUrl]), diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index fe5da9a0cf..6bfaa54f93 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -94,6 +94,10 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { * @return array */ public function checkSmartGroupCustomFieldCriteria() { + if (CRM_Core_BAO_Domain::isDBUpdateRequired()) { + // Do not run this check when the db has not been updated as it might fail on non-updated schema issues. + return []; + } $messages = $problematicSG = []; $customFieldIds = array_keys(CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE)); try { diff --git a/api/v3/CustomField.php b/api/v3/CustomField.php index 09907334c3..d9f54afbc6 100644 --- a/api/v3/CustomField.php +++ b/api/v3/CustomField.php @@ -122,7 +122,7 @@ function civicrm_api3_custom_field_delete($params) { * @return array */ function civicrm_api3_custom_field_get($params) { - if (($params['legacy_html_type'] ?? TRUE) && !empty($params['return'])) { + if (!CRM_Core_BAO_Domain::isDBUpdateRequired() && ($params['legacy_html_type'] ?? TRUE) && !empty($params['return'])) { if (is_array($params['return'])) { $params['return'][] = 'serialize'; } -- 2.25.1