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

RelationshipCache - Add case_id column #21845

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CRM/Contact/BAO/RelationshipCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
],
];

Expand Down
3 changes: 2 additions & 1 deletion CRM/Contact/DAO/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/Relationship.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:402d6e2cc8ffa3be531d291a20f59560)
* (GenCodeChecksum:4d689d3ccc5aa155c858aac2f24ddfab)
*/

/**
Expand Down Expand Up @@ -361,6 +361,7 @@ public static function &fields() {
'bao' => 'CRM_Contact_BAO_Relationship',
'localizable' => 0,
'FKClassName' => 'CRM_Case_DAO_Case',
'component' => 'CiviCase',
'html' => [
'label' => ts("Case"),
],
Expand Down
29 changes: 28 additions & 1 deletion CRM/Contact/DAO/RelationshipCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/RelationshipCache.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0ebd9cc2623ee84848476636cfe86dcb)
* (GenCodeChecksum:f70f357cd7e45be254350f45ef41aee2)
*/

/**
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -156,6 +163,7 @@ public static function getReferenceColumns() {
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'];
Expand Down Expand Up @@ -372,6 +380,25 @@ public static function &fields() {
'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']);
}
Expand Down
46 changes: 26 additions & 20 deletions CRM/Upgrade/Incremental/php/FiveFortyFour.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,33 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev): void {
// }
}

/*
* 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'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw on upgrade your just adding the column not the FK but on install it would have an additional FK right based on the xml?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, how do we even do that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!self::checkFKExists('civicrm_contribution_product', 'FK_civicrm_contribution_product_product_id')) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @seamuslee001.

);
$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;
}

}
3 changes: 3 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.44.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions xml/schema/Contact/Relationship.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<field>
<name>case_id</name>
<type>int unsigned</type>
<component>CiviCase</component>
<title>Case ID</title>
<default>NULL</default>
<comment>FK to civicrm_case</comment>
Expand Down
20 changes: 20 additions & 0 deletions xml/schema/Contact/RelationshipCache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,24 @@
</html>
<readonly>true</readonly>
</field>
<field>
<name>case_id</name>
<type>int unsigned</type>
<component>CiviCase</component>
<title>Case ID</title>
<default>NULL</default>
<comment>FK to civicrm_case</comment>
<html>
<label>Case</label>
</html>
<add>5.44</add>
<readonly>true</readonly>
</field>
<foreignKey>
<name>case_id</name>
<table>civicrm_case</table>
<key>id</key>
<add>2.2</add>
<onDelete>CASCADE</onDelete>
</foreignKey>
</table>