Commit 6050e9ad authored by totten's avatar totten

CRM-20958 - Extract helper Civi\Core\SqlTrigger\TimestampTriggers

== Before ==

 * SQL triggers to populate `civicrm_contact.created_date` and  `civicrm_contact.modified_date` are
   generate via `CRM_Contact_BAO_Contact::triggerInfo($info, $tableName)`

== After ==

 * `CRM_Contact_BAO_Contact::triggerInfo` calls a helper `TimestampTriggers`
 * The helper `TimestampTriggers` accepts arguments describing the names of the tables/columns
   which needed for the timestamp triggers.

== Comments ==

To test, I used this command to update and dump the schema:

```
cv api system.flush triggers=1 && mysqldump --triggers ...
```

The schema was identical before and after.  (Notably, by alternately hacking
the code, I was able to validate the test was capable of revealing
discrepencies.)
parent f6cecca8
......@@ -3277,51 +3277,6 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id )
}
}
/**
* Generate triggers to update the timestamp.
*
* The corresponding civicrm_contact row is updated on insert/update/delete
* to a table that extends civicrm_contact.
* Don't regenerate triggers for all such tables if only asked for one table.
*
* @param array $info
* Reference to the array where generated trigger information is being stored
* @param string|null $reqTableName
* Name of the table for which triggers are being generated, or NULL if all tables
* @param array $relatedTableNames
* Array of all core or all custom table names extending civicrm_contact
* @param string $contactRefColumn
* 'contact_id' if processing core tables, 'entity_id' if processing custom tables
*
* @link https://issues.civicrm.org/jira/browse/CRM-15602
* @see triggerInfo
*/
public static function generateTimestampTriggers(&$info, $reqTableName, $relatedTableNames, $contactRefColumn) {
// Safety
$contactRefColumn = CRM_Core_DAO::escapeString($contactRefColumn);
// If specific related table requested, just process that one
if (in_array($reqTableName, $relatedTableNames)) {
$relatedTableNames = array($reqTableName);
}
// If no specific table requested (include all related tables),
// or a specific related table requested (as matched above)
if (empty($reqTableName) || in_array($reqTableName, $relatedTableNames)) {
$info[] = array(
'table' => $relatedTableNames,
'when' => 'AFTER',
'event' => array('INSERT', 'UPDATE'),
'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.$contactRefColumn;\n",
);
$info[] = array(
'table' => $relatedTableNames,
'when' => 'AFTER',
'event' => array('DELETE'),
'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.$contactRefColumn;\n",
);
}
}
/**
* Get a list of triggers for the contact table.
*
......@@ -3343,37 +3298,17 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id )
}
}
// Update timestamp for civicrm_contact itself
if ($tableName == NULL || $tableName == self::getTableName()) {
$info[] = array(
'table' => array(self::getTableName()),
'when' => 'BEFORE',
'event' => array('INSERT'),
'sql' => "\nSET NEW.created_date = CURRENT_TIMESTAMP;\n",
);
}
// Update timestamp when modifying closely related core tables
$relatedTables = array(
'civicrm_address',
'civicrm_email',
'civicrm_im',
'civicrm_phone',
'civicrm_website',
);
self::generateTimestampTriggers($info, $tableName, $relatedTables, 'contact_id');
// Update timestamp when modifying related custom-data tables
$customGroupTables = array();
$customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
$customGroupDAO->is_multiple = 0;
$customGroupDAO->find();
while ($customGroupDAO->fetch()) {
$customGroupTables[] = $customGroupDAO->table_name;
}
if (!empty($customGroupTables)) {
self::generateTimestampTriggers($info, $tableName, $customGroupTables, 'entity_id');
}
// Modifications to these records should update the contact timestamps.
\Civi\Core\SqlTrigger\TimestampTriggers::create('civicrm_contact', 'Contact')
->setRelations(array(
array('table' => 'civicrm_address', 'column' => 'contact_id'),
array('table' => 'civicrm_email', 'column' => 'contact_id'),
array('table' => 'civicrm_im', 'column' => 'contact_id'),
array('table' => 'civicrm_phone', 'column' => 'contact_id'),
array('table' => 'civicrm_website', 'column' => 'contact_id'),
)
)
->alterTriggerInfo($info, $tableName);
// Update phone table to populate phone_numeric field
if (!$tableName || $tableName == 'civicrm_phone') {
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment