From: Coleman Watts Date: Sat, 16 Oct 2021 20:19:19 +0000 (-0400) Subject: RelationshipCache - Add case_id column X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=bcd7e8d9c2eb6601c8a4997400f68507c078bca9;p=civicrm-core.git RelationshipCache - Add case_id column --- diff --git a/CRM/Contact/BAO/RelationshipCache.php b/CRM/Contact/BAO/RelationshipCache.php index 8681158e7a..e4f6e6695c 100644 --- a/CRM/Contact/BAO/RelationshipCache.php +++ b/CRM/Contact/BAO/RelationshipCache.php @@ -33,6 +33,7 @@ class CRM_Contact_BAO_RelationshipCache extends CRM_Contact_DAO_RelationshipCach 'start_date' => 'rel.start_date', 'end_date' => 'rel.end_date', 'is_active' => 'rel.is_active', + 'case_id' => 'rel.case_id', ], 'b_a' => [ 'relationship_id' => 'rel.id', @@ -45,6 +46,7 @@ class CRM_Contact_BAO_RelationshipCache extends CRM_Contact_DAO_RelationshipCach 'start_date' => 'rel.start_date', 'end_date' => 'rel.end_date', 'is_active' => 'rel.is_active', + 'case_id' => 'rel.case_id', ], ]; diff --git a/CRM/Contact/DAO/RelationshipCache.php b/CRM/Contact/DAO/RelationshipCache.php index 2adb4eea5c..f9e0c8ecc7 100644 --- a/CRM/Contact/DAO/RelationshipCache.php +++ b/CRM/Contact/DAO/RelationshipCache.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Contact/RelationshipCache.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:0ebd9cc2623ee84848476636cfe86dcb) + * (GenCodeChecksum:f70f357cd7e45be254350f45ef41aee2) */ /** @@ -125,6 +125,13 @@ class CRM_Contact_DAO_RelationshipCache extends CRM_Core_DAO { */ public $end_date; + /** + * FK to civicrm_case + * + * @var int + */ + public $case_id; + /** * Class constructor. */ @@ -156,6 +163,7 @@ class CRM_Contact_DAO_RelationshipCache extends CRM_Core_DAO { Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'relationship_type_id', 'civicrm_relationship_type', 'id'); Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'near_contact_id', 'civicrm_contact', 'id'); Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'far_contact_id', 'civicrm_contact', 'id'); + Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'case_id', 'civicrm_case', 'id'); CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']); } return Civi::$statics[__CLASS__]['links']; @@ -372,6 +380,25 @@ class CRM_Contact_DAO_RelationshipCache extends CRM_Core_DAO { 'readonly' => TRUE, 'add' => '5.29', ], + 'case_id' => [ + 'name' => 'case_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('Case ID'), + 'description' => ts('FK to civicrm_case'), + 'where' => 'civicrm_relationship_cache.case_id', + 'default' => 'NULL', + 'table_name' => 'civicrm_relationship_cache', + 'entity' => 'RelationshipCache', + 'bao' => 'CRM_Contact_BAO_RelationshipCache', + 'localizable' => 0, + 'FKClassName' => 'CRM_Case_DAO_Case', + 'component' => 'CiviCase', + 'html' => [ + 'label' => ts("Case"), + ], + 'readonly' => TRUE, + 'add' => '5.44', + ], ]; CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']); } diff --git a/CRM/Upgrade/Incremental/php/FiveFortyFour.php b/CRM/Upgrade/Incremental/php/FiveFortyFour.php index b1f2a48c43..2714f32b1e 100644 --- a/CRM/Upgrade/Incremental/php/FiveFortyFour.php +++ b/CRM/Upgrade/Incremental/php/FiveFortyFour.php @@ -49,27 +49,33 @@ class CRM_Upgrade_Incremental_php_FiveFortyFour extends CRM_Upgrade_Incremental_ // } } - /* - * Important! All upgrade functions MUST add a 'runSql' task. - * Uncomment and use the following template for a new upgrade version - * (change the x in the function name): + /** + * Upgrade function. + * @param string $rev */ + public function upgrade_5_44_alpha1($rev) { + // The runSql task populates the new column, so this addColumn task runs first + $this->addTask('Add case_id column to civicrm_relationship_cache', 'addColumn', + 'civicrm_relationship_cache', 'case_id', "int unsigned DEFAULT NULL COMMENT 'FK to civicrm_case'" + ); + $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); + $this->addTask('Add FK to civicrm_relationship_cache.case_id', 'addRelationshipCacheCaseFK'); + } - // /** - // * Upgrade function. - // * - // * @param string $rev - // */ - // public function upgrade_5_0_x($rev): void { - // $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); - // $this->addTask('Do the foo change', 'taskFoo', ...); - // // Additional tasks here... - // // Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex. - // // The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable. - // } - - // public static function taskFoo(CRM_Queue_TaskContext $ctx, ...): bool { - // return TRUE; - // } + /** + * @param \CRM_Queue_TaskContext $ctx + * @return bool + */ + public static function addRelationshipCacheCaseFK(CRM_Queue_TaskContext $ctx): bool { + if (!self::checkFKExists('civicrm_relationship_cache', 'FK_civicrm_relationship_cache_case_id')) { + CRM_Core_DAO::executeQuery(" + ALTER TABLE `civicrm_relationship_cache` + ADD CONSTRAINT `FK_civicrm_relationship_cache_case_id` + FOREIGN KEY (`case_id`) REFERENCES `civicrm_case` (`id`) + ON DELETE CASCADE; + ", [], TRUE, NULL, FALSE, FALSE); + } + return TRUE; + } } diff --git a/CRM/Upgrade/Incremental/sql/5.44.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/5.44.alpha1.mysql.tpl index 5164151af4..2290afb544 100644 --- a/CRM/Upgrade/Incremental/sql/5.44.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/5.44.alpha1.mysql.tpl @@ -1 +1,4 @@ {* file to handle db changes in 5.44.alpha1 during upgrade *} + +{* Polulate RelationshipCache.case_id column *} +UPDATE `civicrm_relationship_cache` rc, `civicrm_relationship` r SET rc.case_id = r.case_id WHERE r.case_id IS NOT NULL AND r.id = rc.relationship_id; diff --git a/xml/schema/Contact/RelationshipCache.xml b/xml/schema/Contact/RelationshipCache.xml index 0487942ed4..e53094d61a 100644 --- a/xml/schema/Contact/RelationshipCache.xml +++ b/xml/schema/Contact/RelationshipCache.xml @@ -227,4 +227,24 @@ true + + case_id + int unsigned + CiviCase + Case ID + NULL + FK to civicrm_case + + + + 5.44 + true + + + case_id + civicrm_case
+ id + 2.2 + CASCADE +