Permalink
Browse files

5260 change how changelog diffs are computed by the triggers

  • Loading branch information...
lcdservices committed May 24, 2012
1 parent a16fb84 commit 6025a08b7b4834200f337e93a14e955fb64a86bb
@@ -90,12 +90,18 @@ private function diffsInTableForId($table, $id)
3 => array($id, 'Integer'),
);
- // look for the last change in the given connection that happended less than 10 seconds later than log_date to the given id to catch multi-query changes
+ // look for all the changes in the given connection that happended less than 10 seconds later than log_date to the given id to catch multi-query changes
$changedSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id = %1 AND log_date < DATE_ADD(%2, INTERVAL 10 SECOND) AND id = %3 ORDER BY log_date DESC LIMIT 1";
- $changed = $this->sqlToArray($changedSQL, $params);
+ //$changed = $this->sqlToArray($changedSQL, $params);//NYSS 5260
+
+ $changedDAO = CRM_Core_DAO::executeQuery($changedSQL, $params);
+ while ($changedDAO->fetch( )) {
+ $changed = $changedDAO->toArray();
// return early if nothing found
- if (empty($changed)) return array();
+ if (empty($changed)) {
+ continue;
+ }
switch ($changed['log_action']) {
case 'Delete':
@@ -138,8 +144,8 @@ private function diffsInTableForId($table, $id)
'to' => CRM_Utils_Array::value($diff,$changed),
);
}
-
- return $diffs;
+ }//NYSS 5260
+ return $diffs;
}
function titlesAndValuesForTable($table)
@@ -56,7 +56,7 @@ function __construct()
function groupBy()
{
- $this->_groupBy = 'GROUP BY log_conn_id, log_user_id, EXTRACT(DAY_MINUTE FROM log_date)';
+ $this->_groupBy = 'GROUP BY log_conn_id, log_user_id, EXTRACT(DAY_MICROSECOND FROM log_date)';//NYSS 5260
}
function orderBy()
@@ -361,8 +361,10 @@ function triggerInfo( &$info, $tableName = null ) {
return;
}
- $upsert = array( 'INSERT', 'UPDATE' );
- $delete = array( 'DELETE' );
+ //NYSS 5260
+ $insert = array('INSERT');
+ $update = array('UPDATE');
+ $delete = array('DELETE');
if ( $tableName ) {
$tableNames = array( $tableName );
@@ -374,30 +376,71 @@ function triggerInfo( &$info, $tableName = null ) {
foreach ( $tableNames as $table ) {
$columns = $this->columnsOf($table);
- $upsertSQL = $deleteSQL = "INSERT INTO `{$this->db}`.log_{tableName} (";
+ //NYSS 5260
+ // only do the change if any data has changed
+ $cond = array( );
foreach ( $columns as $column ) {
- $upsertSQL .= "$column, ";
- $deleteSQL .= "$column, ";
+ $cond[] = "OLD.$column <> NEW.$column";
}
- $upsertSQL .= "log_conn_id, log_user_id, log_action, log_job_id) VALUES ("; //NYSS jobID
- $deleteSQL .= "log_conn_id, log_user_id, log_action, log_job_id) VALUES ("; //NYSS jobID
-
+ $updateSQL = "IF ( (" . implode( ' OR ', $cond ) . ") AND ( @civicrm_disable_logging IS NULL || @civicrm_disable_logging = 0 ) ) THEN ";
+
+ $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} (";
+
+ foreach ($columns as $column) {
+ $sqlStmt .= "$column, ";
+ }
+ $sqlStmt .= "log_conn_id, log_user_id, log_action, log_job_id) VALUES ("; //NYSS jobID
+
+ $insertSQL = $deleteSQL = "IF ( @civicrm_disable_logging IS NULL || @civicrm_disable_logging = 0 ) THEN $sqlStmt ";
+
+ $updateSQL .= $sqlStmt;
+
+ $sqlStmt = '';
foreach ( $columns as $column ) {
- $upsertSQL .= "NEW.$column, ";
+ $sqlStmt .= "NEW.$column, ";
$deleteSQL .= "OLD.$column, ";
}
- $upsertSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}', @jobID);"; //NYSS
+ $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}', @jobID);"; //NYSS
$deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}', @jobID);"; //NYSS
+ $sqlStmt .= "END IF;";
+ $deleteSQL .= "END IF;";
+
+ $insertSQL .= $sqlStmt;
+ $updateSQL .= $sqlStmt;
+
$info[] = array( 'table' => array( $table ),
'when' => 'AFTER',
- 'event' => $upsert,
- 'sql' => $upsertSQL );
+ 'event' => $insert,
+ 'sql' => $insertSQL
+ );
+
+ $info[] = array('table' => array($table),
+ 'when' => 'AFTER',
+ 'event' => $update,
+ 'sql' => $updateSQL,
+ );
$info[] = array( 'table' => array( $table ),
'when' => 'AFTER',
'event' => $delete,
'sql' => $deleteSQL );
}
} //triggerInfo
+
+ /**
+ * This allow logging to be temporarily disabled for certain cases
+ * where we want to do a mass cleanup but dont want to bother with
+ * an audit trail
+ *
+ * @static
+ * @public
+ */
+ static function disableLoggingForThisConnection( ) {
+ // do this only if logging is enabled
+ $config = CRM_Core_Config::singleton( );
+ if ( $config->logging ) {
+ CRM_Core_DAO::executeQuery( 'SET @civicrm_disable_logging = 1' );
+ }
+ }
}

0 comments on commit 6025a08

Please sign in to comment.