Skip to content

Commit

Permalink
Add created_date & modified_date to civicrm_relationship
Browse files Browse the repository at this point in the history
As with other tables we have done this to, knowing when we added a record to the
DB is useful (and differs from the relationship start_date).

I'm not sure yet if I will try to expose in the UI or whether search-kit
is all we need
  • Loading branch information
eileenmcnaughton committed Jan 12, 2022
1 parent 75b5ef5 commit 2c309f3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
44 changes: 43 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:4d689d3ccc5aa155c858aac2f24ddfab)
* (GenCodeChecksum:feaa06b2f8873b614075e11d456c0ce7)
*/

/**
Expand Down Expand Up @@ -124,6 +124,20 @@ class CRM_Contact_DAO_Relationship extends CRM_Core_DAO {
*/
public $case_id;

/**
* Relationship created date.
*
* @var timestamp
*/
public $created_date;

/**
* Relationship last modified.
*
* @var timestamp
*/
public $modified_date;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -367,6 +381,34 @@ public static function &fields() {
],
'add' => '2.2',
],
'created_date' => [
'name' => 'created_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => ts('Created Date'),
'description' => ts('Relationship created date.'),
'required' => TRUE,
'where' => 'civicrm_relationship.created_date',
'default' => 'CURRENT_TIMESTAMP',
'table_name' => 'civicrm_relationship',
'entity' => 'Relationship',
'bao' => 'CRM_Contact_BAO_Relationship',
'localizable' => 0,
'add' => '5.36',
],
'modified_date' => [
'name' => 'modified_date',
'type' => CRM_Utils_Type::T_TIMESTAMP,
'title' => ts('Relationship Modified Date'),
'description' => ts('Relationship last modified.'),
'where' => 'civicrm_relationship.modified_date',
'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
'table_name' => 'civicrm_relationship',
'entity' => 'Relationship',
'bao' => 'CRM_Contact_BAO_Relationship',
'localizable' => 0,
'readonly' => TRUE,
'add' => '5.47',
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
24 changes: 24 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveFortySeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,35 @@ class CRM_Upgrade_Incremental_php_FiveFortySeven extends CRM_Upgrade_Incremental
public function upgrade_5_47_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Migrate CiviGrant component to an extension', 'migrateCiviGrant');
$this->addTask('Add created_date to civicrm_relationship', 'addColumn', 'civicrm_relationship', 'created_date',
"timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Relationship created date'"
);
$this->addTask('Add modified_date column to civicrm_relationship', 'addColumn',
'civicrm_relationship', 'modified_date',
"timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL COMMENT 'Relationship last modified.'"
);
$this->addTask('Set initial value for relationship created_date and modified_date to start_date', 'updateRelationship dates Dates');
}

/**
* @param \CRM_Queue_TaskContext $ctx
*
* @return bool
*/
public static function updateRelationshipDates(CRM_Queue_TaskContext $ctx): bool {
CRM_Core_DAO::executeQuery('
UPDATE civicrm_relationship SET created_date = start_date, modified_date = start_date WHERE start_date IS NOT NULL
');
return TRUE;
}

/**
* @param \CRM_Queue_TaskContext $ctx
*
* @return bool
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\NotImplementedException
*/
public static function migrateCiviGrant(CRM_Queue_TaskContext $ctx): bool {
$civiGrantEnabled = in_array('CiviGrant', Civi::settings()->get('enable_components'), TRUE);
Expand Down
17 changes: 17 additions & 0 deletions xml/schema/Contact/Relationship.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@
</html>
<add>2.2</add>
</field>
<field>
<name>created_date</name>
<type>timestamp</type>
<comment>Relationship created date.</comment>
<required>true</required>
<default>CURRENT_TIMESTAMP</default>
<add>5.36</add>
</field>
<field>
<name>modified_date</name>
<title>Relationship Modified Date</title>
<type>timestamp</type>
<comment>Relationship last modified.</comment>
<default>CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</default>
<add>5.47</add>
<readonly>true</readonly>
</field>
<foreignKey>
<name>case_id</name>
<table>civicrm_case</table>
Expand Down

0 comments on commit 2c309f3

Please sign in to comment.