Skip to content

Commit

Permalink
CRM-20958 - Remove automatic migration of case/activity timestamps
Browse files Browse the repository at this point in the history
As discussed in [civicrm#10754](civicrm#10754),
the source material for populating `created_date` and `modified_date` has some
quality issues (vis-a-vis timezones).

If we attempt to immediately initialize `created_date` and `modified_date` from
pre-existing data, then it gets a lot harder to clean up the pre-existing data.
Fortunately, most systems don't actually *need* this data immediately.
So we can defer.

I've moved these migration rules over to an extension

https://github.com/civicrm/org.civicrm.doctorwhen
  • Loading branch information
totten committed Aug 2, 2017
1 parent dbd838a commit c5eba0e
Showing 1 changed file with 0 additions and 130 deletions.
130 changes: 0 additions & 130 deletions CRM/Upgrade/Incremental/php/FourSeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,90 +421,6 @@ public function upgrade_4_7_24($rev) {
$this->addTask('CRM-20958 - Add modified_date to civicrm_case', 'addColumn',
'civicrm_case', 'modified_date', "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'When was the case (or closely related entity) was created or modified or deleted.'");

$this->addTask('CRM-20958 - Suspend modification tracking during upgrade', 'toggleModificationTracking', FALSE);

list($minId, $maxId) = CRM_Core_DAO::executeQuery(
"SELECT coalesce(min(id),0), coalesce(max(id),0) FROM civicrm_activity"
)->getDatabaseResult()->fetchRow();
for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
$endId = $startId + self::BATCH_SIZE - 1;
$vars = array(1 => array($startId, 'Int'), 2 => array($endId, 'Int'));

$title = sprintf('CRM-20958 - Compute civicrm_activity.created_date from civicrm_log (%d => %d)', $startId, $endId);
$sql = 'UPDATE civicrm_activity
SET created_date = (SELECT MIN(l.modified_date) FROM civicrm_log l WHERE l.entity_table ="civicrm_activity" AND civicrm_activity.id = l.entity_id)
WHERE (id BETWEEN %1 AND %2)
AND created_date IS NULL
';
$this->addTask($title, 'task_executeQuery', $sql, $vars);

$title = sprintf('CRM-20958 - Compute civicrm_activity.modified_date from civicrm_log (%d => %d)', $startId, $endId);
$sql = 'UPDATE civicrm_activity
SET modified_date = (SELECT MAX(l.modified_date) FROM civicrm_log l WHERE l.entity_table ="civicrm_activity" AND civicrm_activity.id = l.entity_id)
WHERE (id BETWEEN %1 AND %2)
AND modified_date IS NULL';

$this->addTask($title, 'task_executeQuery', $sql, $vars);
}

$openCaseTypeId = CRM_Core_DAO::singleValueQuery(
'SELECT value FROM civicrm_option_value cov
INNER JOIN civicrm_option_group cog ON cov.option_group_id = cog.id
WHERE cov.name = "Open Case" and cog.name = "activity_type"'
);

list($minId, $maxId) = CRM_Core_DAO::executeQuery(
"SELECT coalesce(min(id),0), coalesce(max(id),0) FROM civicrm_case"
)->getDatabaseResult()->fetchRow();
for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
$endId = $startId + self::BATCH_SIZE - 1;
$vars = array(1 => array($startId, 'Int'), 2 => array($endId, 'Int'), 3 => array($openCaseTypeId, 'Int'));

// CONSIDER: In my local system, the "Open Case" timestamps seem to be more synthetic (:00:00)
// $title = sprintf('CRM-20958 - Compute civicrm_case.created_date from "Open Case" (%d => %d)', $startId, $endId);
// $sql = 'UPDATE civicrm_case
// SET created_date = (
// SELECT MIN(a.activity_date_time)
// FROM civicrm_case_activity ca
// INNER JOIN civicrm_activity a ON (ca.activity_id = a.id)
// WHERE civicrm_case.id = ca.case_id
// AND a.activity_type_id = %3
// )
// WHERE (id BETWEEN %1 AND %2)
// AND created_date IS NULL
//';
// $this->addTask($title, 'task_executeQuery', $sql, $vars);

// In case... for some ungodly reason... the 'Open Case' activity was missing...
$title = sprintf('CRM-20958 - Compute civicrm_case.created_date from the activity log (%d => %d)', $startId, $endId);
$sql = 'UPDATE civicrm_case
SET created_date = (
SELECT MIN(l.modified_date)
FROM civicrm_case_activity ca
INNER JOIN civicrm_log l ON (l.entity_table = "civicrm_activity" AND ca.activity_id = l.entity_id)
WHERE civicrm_case.id = ca.case_id
)
WHERE (id BETWEEN %1 AND %2)
AND created_date IS NULL
';
$this->addTask($title, 'task_executeQuery', $sql, $vars);

$title = sprintf('CRM-20958 - Compute civicrm_case.modified_date from the activity log (%d => %d)', $startId, $endId);
$sql = 'UPDATE civicrm_case
SET modified_date = (
SELECT MAX(l.modified_date)
FROM civicrm_case_activity ca
INNER JOIN civicrm_log l ON (l.entity_table = "civicrm_activity" AND ca.activity_id = l.entity_id)
WHERE civicrm_case.id = ca.case_id
)
WHERE (id BETWEEN %1 AND %2)
AND modified_date IS NULL
';
$this->addTask($title, 'task_executeQuery', $sql, $vars);
}

$this->addTask('CRM-20958 - Restore modification tracking', 'toggleModificationTracking', TRUE);

return TRUE;
}

Expand Down Expand Up @@ -1301,50 +1217,4 @@ protected function checkImageUploadDir() {
return $config->imageUploadDir && $config->imageUploadURL && $check->isDirAccessible($config->imageUploadDir, $config->imageUploadURL);
}

/**
* (Queue Task Callback)
*
* Enable or disable automatic updates to the `modified_date` columns.
*
* When autopopulating the modification times, we don't want to fill in the
* current time.
*
* @param CRM_Queue_TaskContext $ctx
* @param bool $enabled
* @param
*
* @return bool
*/
public static function toggleModificationTracking($ctx, $enabled) {
if ($enabled) {
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_case CHANGE modified_date modified_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_activity CHANGE modified_date modified_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
}
else {
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_case CHANGE modified_date modified_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP');
CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_activity CHANGE modified_date modified_date TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP');
}
return TRUE;
}

/**
* (Queue Task Callback)
*
* Execute a single SQL query.
*
* @param CRM_Queue_TaskContext $ctx
* @param string $sql
* An SQL template. May include tokens `%1`, `%2`, etc.
* @param array $vars
* List of SQL parameters, as used by executeQuery().
*
* @return bool
*
* @see CRM_Core_DAO::executeQuery
*/
public static function task_executeQuery($ctx, $sql, $vars) {
CRM_Core_DAO::executeQuery($sql, $vars);
return TRUE;
}

}

0 comments on commit c5eba0e

Please sign in to comment.