From faf0012b036ed77da44c3f4c66ea78d871a74515 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 10 Jun 2020 21:07:08 +1000 Subject: [PATCH] [REF] Add in upgrade step to populate missing contact_type.name field Make changes to the other columns --- .../Incremental/php/FiveTwentyEight.php | 26 +++++++++++++++++++ .../Incremental/sql/5.28.alpha1.mysql.tpl | 6 ++++- .../CRM/Upgrade/Incremental/BaseTest.php | 8 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CRM/Upgrade/Incremental/php/FiveTwentyEight.php b/CRM/Upgrade/Incremental/php/FiveTwentyEight.php index 0526a39564ac..1cd3d2a3a430 100644 --- a/CRM/Upgrade/Incremental/php/FiveTwentyEight.php +++ b/CRM/Upgrade/Incremental/php/FiveTwentyEight.php @@ -69,4 +69,30 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { // return TRUE; // } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_5_28_alpha1($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); + $this->addTask('Populate missing Contact Type name fields', 'populateMissingContactTypeName'); + } + + public static function populateMissingContactTypeName() { + $contactTypes = \Civi\Api4\ContactType::get() + ->setCheckPermissions(FALSE) + ->execute(); + foreach ($contactTypes as $contactType) { + if (empty($contactType['name'])) { + \Civi\Api4\ContactType::update() + ->addWhere('id', '=', $contactType['id']) + ->addValue('name', ucfirst(CRM_Utils_String::munge($contactType['label']))) + ->setCheckPermissions(FALSE) + ->execute(); + } + } + return TRUE; + } + } diff --git a/CRM/Upgrade/Incremental/sql/5.28.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/5.28.alpha1.mysql.tpl index 47df161ed45b..a82693b7f0a3 100644 --- a/CRM/Upgrade/Incremental/sql/5.28.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/5.28.alpha1.mysql.tpl @@ -5,4 +5,8 @@ ALTER TABLE `civicrm_activity` CHANGE `activity_date_time` `activity_date_time` ALTER TABLE `civicrm_activity` CHANGE `created_date` `created_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'When was the activity was created.'; -- https://github.com/civicrm/civicrm-core/pull/17548 -ALTER table civicrm_contact_type modify name varchar(64) not null comment 'Internal name of Contact Type (or Subtype).'; +ALTER TABLE civicrm_contact_type CHANGE name name varchar(64) not null comment 'Internal name of Contact Type (or Subtype).'; +ALTER TABLE civicrm_contact_type CHANGE is_active is_active tinyint DEFAULT 1 COMMENT 'Is this entry active?'; +ALTER TABLE civicrm_contact_type CHANGE is_reserved is_reserved tinyint DEFAULT 0 COMMENT 'Is this contact type a predefined system type'; +UPDATE civicrm_contact_type SET is_active = 1 WHERE is_active IS NULL; +UPDATE civicrm_contact_type SET is_reserved = 0 WHERE is_reserved IS NULL; diff --git a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php index e9e0536764c2..c9a030bbd516 100644 --- a/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php +++ b/tests/phpunit/CRM/Upgrade/Incremental/BaseTest.php @@ -673,4 +673,12 @@ public function testReportFormConvertDatePicker() { $this->assertEquals('1991-11-01 00:00:00', $formValues['receive_date_from']); } + public function testUpdateContactTypeNameField() { + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_contact_type (name,label,parent_id, is_active) VALUES ('', 'Test Contact Type', 1, 1)"); + CRM_Upgrade_Incremental_php_FiveTwentyEight::populateMissingContactTypeName(); + $contactType = $this->callAPISuccess('ContactType', 'getsingle', ['label' => 'Test Contact Type']); + $this->assertNotEmpty($contactType['name']); + $this->callAPISuccess('ContactType', 'delete', ['id' => $contactType['id']]); + } + }