Skip to content

Commit

Permalink
Merge pull request #19642 from demeritcowboy/case-views2
Browse files Browse the repository at this point in the history
dev/core#2385 and dev/core#2262 - Case db views are no longer needed
  • Loading branch information
eileenmcnaughton authored Mar 6, 2021
2 parents 16d5ffe + 6912ecb commit 27dff1b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 99 deletions.
5 changes: 0 additions & 5 deletions CRM/Admin/Form/Setting/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ public static function formRule($fields, $files, $options) {
) {
$errors['enable_components'] = ts('You need to enable CiviContribute before enabling CiviPledge.');
}
if (!empty($fields['enable_components']['CiviCase']) &&
!CRM_Core_DAO::checkTriggerViewPermission(TRUE, FALSE)
) {
$errors['enable_components'] = ts('CiviCase requires CREATE VIEW and DROP VIEW permissions for the database.');
}
}

return $errors;
Expand Down
94 changes: 27 additions & 67 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,37 @@ public static function getCaseActivityQuery($type = 'upcoming', $userID, $condit
INNER JOIN civicrm_contact ON civicrm_case_contact.contact_id = civicrm_contact.id
HERESQL;

// 'upcoming' and 'recent' show the next scheduled and most recent
// not-scheduled activity on each case, respectively.
$scheduled_id = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled');
switch ($type) {
case 'upcoming':
$query .= <<<HERESQL
INNER JOIN (SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id
FROM civicrm_case_activity ca
INNER JOIN civicrm_activity a ON ca.activity_id=a.id
WHERE a.id =
(SELECT b.id FROM civicrm_case_activity bca
INNER JOIN civicrm_activity b ON bca.activity_id=b.id
WHERE b.activity_date_time <= DATE_ADD( NOW(), INTERVAL 14 DAY )
AND b.is_current_revision = 1 AND b.is_deleted=0 AND b.status_id = $scheduled_id
AND bca.case_id = ca.case_id ORDER BY b.activity_date_time ASC LIMIT 1)) t_act
ON t_act.case_id = civicrm_case.id
HERESQL;
break;

case 'recent':
// civicrm_view_case_activity_upcoming and
// civicrm_view_case_activity_recent are views that show the next
// scheduled and most recent not-scheduled activity on each case,
// respectively.
$query .= <<<HERESQL
INNER JOIN civicrm_view_case_activity_$type t_act
ON t_act.case_id = civicrm_case.id
INNER JOIN (SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id
FROM civicrm_case_activity ca
INNER JOIN civicrm_activity a ON ca.activity_id=a.id
WHERE a.id =
(SELECT b.id FROM civicrm_case_activity bca
INNER JOIN civicrm_activity b ON bca.activity_id=b.id
WHERE b.activity_date_time >= DATE_SUB( NOW(), INTERVAL 14 DAY )
AND b.is_current_revision = 1 AND b.is_deleted=0 AND b.status_id <> $scheduled_id
AND bca.case_id = ca.case_id ORDER BY b.activity_date_time DESC LIMIT 1)) t_act
ON t_act.case_id = civicrm_case.id
HERESQL;
break;

Expand Down Expand Up @@ -2783,67 +2804,6 @@ public static function isCaseConfigured($contactId = NULL) {
return $configured;
}

/**
* Used during case component enablement and during upgrade.
*
* @return bool
*/
public static function createCaseViews() {
$dao = new CRM_Core_DAO();
try {
$sql = self::createCaseViewsQuery('upcoming');
$dao->query($sql);

$sql = self::createCaseViewsQuery('recent');
$dao->query($sql);
}
catch (Exception $e) {
return FALSE;
}

return TRUE;
}

/**
* Helper function, also used by the upgrade in case of error
*
* @param string $section
*
* @return string
*/
public static function createCaseViewsQuery($section = 'upcoming') {
$sql = "";
$scheduled_id = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled');
switch ($section) {
case 'upcoming':
$sql = "CREATE OR REPLACE VIEW `civicrm_view_case_activity_upcoming`
AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id
FROM civicrm_case_activity ca
INNER JOIN civicrm_activity a ON ca.activity_id=a.id
WHERE a.id =
(SELECT b.id FROM civicrm_case_activity bca
INNER JOIN civicrm_activity b ON bca.activity_id=b.id
WHERE b.activity_date_time <= DATE_ADD( NOW(), INTERVAL 14 DAY )
AND b.is_current_revision = 1 AND b.is_deleted=0 AND b.status_id = $scheduled_id
AND bca.case_id = ca.case_id ORDER BY b.activity_date_time ASC LIMIT 1)";
break;

case 'recent':
$sql = "CREATE OR REPLACE VIEW `civicrm_view_case_activity_recent`
AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id
FROM civicrm_case_activity ca
INNER JOIN civicrm_activity a ON ca.activity_id=a.id
WHERE a.id =
(SELECT b.id FROM civicrm_case_activity bca
INNER JOIN civicrm_activity b ON bca.activity_id=b.id
WHERE b.activity_date_time >= DATE_SUB( NOW(), INTERVAL 14 DAY )
AND b.is_current_revision = 1 AND b.is_deleted=0 AND b.status_id <> $scheduled_id
AND bca.case_id = ca.case_id ORDER BY b.activity_date_time DESC LIMIT 1)";
break;
}
return $sql;
}

/**
* Add/copy relationships, when new client is added for a case
*
Expand Down
3 changes: 0 additions & 3 deletions CRM/Case/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ public static function onToggleComponents($oldValue, $newValue, $metadata) {
) {
$pathToCaseSampleTpl = __DIR__ . '/xml/configuration.sample/';
self::loadCaseSampleData($pathToCaseSampleTpl . 'case_sample.mysql.tpl');
if (!CRM_Case_BAO_Case::createCaseViews()) {
throw new CRM_Core_Exception(ts("Could not create the MySQL views for CiviCase. Your mysql user needs to have the 'CREATE VIEW' permission"));
}
}
}

Expand Down
24 changes: 0 additions & 24 deletions CRM/Upgrade/Incremental/php/FiveFourteen.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,9 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
*/
public function upgrade_5_14_alpha1($rev) {
$this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);

// Only need to rebuild view if CiviCase is enabled: otherwise will be
// rebuilt when component is enabled
$config = CRM_Core_Config::singleton();
if (in_array('CiviCase', $config->enableComponents)) {
$this->addTask('Rebuild case activity views', 'rebuildCaseActivityView', $rev);
}
// Additional tasks here...
// Note: do not use ts() in the addTask description because it adds unnecessary strings to transifex.
// The above is an exception because 'Upgrade DB to %1: SQL' is generic & reusable.
}

/**
* Rebuild the view of recent and upcoming case activities
*
* See https://github.com/civicrm/civicrm-core/pull/14086 and
* https://lab.civicrm.org/dev/core/issues/832
*
* @param CRM_Queue_TaskContext $ctx
* @return bool
*/
public static function rebuildCaseActivityView($ctx) {
if (!CRM_Case_BAO_Case::createCaseViews()) {
CRM_Core_Error::debug_log_message(ts("Could not create the MySQL views for CiviCase. Your mysql user needs to have the 'CREATE VIEW' permission"));
return FALSE;
}
return TRUE;
}

}
3 changes: 3 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.37.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{* file to handle db changes in 5.37.alpha1 during upgrade *}

DROP VIEW IF EXISTS civicrm_view_case_activity_upcoming;
DROP VIEW IF EXISTS civicrm_view_case_activity_recent;

0 comments on commit 27dff1b

Please sign in to comment.