From 0590c631bce015c10a5b6d3ec03828fbe717a2c9 Mon Sep 17 00:00:00 2001 From: David Knoll Date: Thu, 20 Nov 2014 11:14:38 +0000 Subject: [PATCH] CRM-15602 Fix for logging- only regenerate timestamp triggers on requested tables --- CRM/Contact/BAO/Contact.php | 73 +++++++++++++++++++++++++------------ CRM/Core/DAO.php | 2 +- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index d544bb6480..168eb37f53 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -3097,6 +3097,52 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) } } + /** + * Generate triggers to update the timestamp on the corresponding civicrm_contact row, + * 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 + * @return void + * + * @link https://issues.civicrm.org/jira/browse/CRM-15602 + * @see triggerInfo + * @access public + * @static + */ + 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 * @@ -3115,6 +3161,7 @@ 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()), @@ -3132,18 +3179,7 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) 'civicrm_phone', 'civicrm_website', ); - $info[] = array( - 'table' => $relatedTables, - 'when' => 'AFTER', - 'event' => array('INSERT', 'UPDATE'), - 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.contact_id;\n", - ); - $info[] = array( - 'table' => $relatedTables, - 'when' => 'AFTER', - 'event' => array('DELETE'), - 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.contact_id;\n", - ); + self::generateTimestampTriggers($info, $tableName, $relatedTables, 'contact_id'); // Update timestamp when modifying related custom-data tables $customGroupTables = array(); @@ -3154,18 +3190,7 @@ LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) $customGroupTables[] = $customGroupDAO->table_name; } if (!empty($customGroupTables)) { - $info[] = array( - 'table' => $customGroupTables, - 'when' => 'AFTER', - 'event' => array('INSERT', 'UPDATE'), - 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.entity_id;\n", - ); - $info[] = array( - 'table' => $customGroupTables, - 'when' => 'AFTER', - 'event' => array('DELETE'), - 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.entity_id;\n", - ); + self::generateTimestampTriggers($info, $tableName, $customGroupTables, 'entity_id'); } // Update phone table to populate phone_numeric field diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 0399e88fb1..489c068a56 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1721,7 +1721,7 @@ SELECT contact_id $logging->dropTriggers($tableName); // now create the set of new triggers - self::createTriggers($info); + self::createTriggers($info, $tableName); } /** -- 2.25.1