From: Coleman Watts Date: Thu, 4 Aug 2016 19:03:55 +0000 (-0400) Subject: CRM-17663 - Add unique index to civicrm_dashboard_contact X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=00c27b41efba71a7bc91cb488e7bf73b36313ebe;p=civicrm-core.git CRM-17663 - Add unique index to civicrm_dashboard_contact --- diff --git a/CRM/Core/BAO/Dashboard.php b/CRM/Core/BAO/Dashboard.php index 3d74b07cfb..1bb316b3bf 100644 --- a/CRM/Core/BAO/Dashboard.php +++ b/CRM/Core/BAO/Dashboard.php @@ -368,9 +368,6 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { throw new RuntimeException("Failed to determine contact ID"); } - //we need to get existing dashlets, so we know when to update or insert - $contactDashlets = self::getContactDashlets(TRUE, $contactID); - $dashletIDs = array(); if (is_array($columns)) { foreach ($columns as $colNo => $dashlets) { @@ -380,18 +377,13 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { $weight = 1; foreach ($dashlets as $dashletID => $isMinimized) { $isMinimized = (int) $isMinimized; - if (in_array($dashletID, $contactDashlets)) { - $query = " UPDATE civicrm_dashboard_contact - SET weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1 - WHERE dashboard_id = {$dashletID} AND contact_id = {$contactID} "; - } - else { - $query = " INSERT INTO civicrm_dashboard_contact - ( weight, is_minimized, column_no, is_active, dashboard_id, contact_id ) - VALUES( {$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID} )"; - } + $dashletID = (int) $dashletID; + $query = "INSERT INTO civicrm_dashboard_contact + (weight, is_minimized, column_no, is_active, dashboard_id, contact_id) + VALUES({$weight}, {$isMinimized}, {$colNo}, 1, {$dashletID}, {$contactID}) + ON DUPLICATE KEY UPDATE weight = {$weight}, is_minimized = {$isMinimized}, column_no = {$colNo}, is_active = 1"; // fire update query for each column - $dao = CRM_Core_DAO::executeQuery($query); + CRM_Core_DAO::executeQuery($query); $dashletIDs[] = $dashletID; $weight++; @@ -399,18 +391,11 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { } } - if (!empty($dashletIDs)) { - // we need to disable widget that removed - $updateQuery = " UPDATE civicrm_dashboard_contact - SET is_active = 0 - WHERE dashboard_id NOT IN ( " . implode(',', $dashletIDs) . ") AND contact_id = {$contactID}"; - } - else { - // this means all widgets are disabled - $updateQuery = " UPDATE civicrm_dashboard_contact - SET is_active = 0 - WHERE contact_id = {$contactID}"; - } + // Disable inactive widgets + $dashletClause = $dashletIDs ? "dashboard_id NOT IN (" . implode(',', $dashletIDs) . ")" : '(1)'; + $updateQuery = "UPDATE civicrm_dashboard_contact + SET is_active = 0 + WHERE $dashletClause AND contact_id = {$contactID}"; CRM_Core_DAO::executeQuery($updateQuery); } diff --git a/CRM/Upgrade/Incremental/php/FourSeven.php b/CRM/Upgrade/Incremental/php/FourSeven.php index 74a875f151..9030a6afee 100644 --- a/CRM/Upgrade/Incremental/php/FourSeven.php +++ b/CRM/Upgrade/Incremental/php/FourSeven.php @@ -221,6 +221,16 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base $this->addTask(ts('Alter index and type for image URL'), 'alterIndexAndTypeForImageURL'); } + /** + * Upgrade function. + * + * @param string $rev + */ + public function upgrade_4_7_11($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev); + $this->addTask('Dashboard schema updates', 'dashboardSchemaUpdate'); + } + /* * Important! All upgrade functions MUST call the 'runSql' task. * Uncomment and use the following template for a new upgrade version @@ -727,6 +737,21 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_ return TRUE; } + /** + * CRM-17663 - Dashboard schema changes + * + * @param \CRM_Queue_TaskContext $ctx + * + * @return bool + */ + public function dashboardSchemaUpdate(CRM_Queue_TaskContext $ctx) { + if (!CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_dashboard_contact', 'index_dashboard_id_contact_id')) { + // Delete any stray duplicate rows and add unique index to prevent new dupes and enable INSERT/UPDATE combo query + CRM_Core_DAO::executeQuery('DELETE c1 FROM civicrm_dashboard_contact c1, civicrm_dashboard_contact c2 WHERE c1.contact_id = c2.contact_id AND c1.dashboard_id = c2.dashboard_id AND c1.id > c2.id'); + CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_dashboard_contact ADD UNIQUE INDEX index_dashboard_id_contact_id (dashboard_id, contact_id);'); + } + } + /** * CRM-19100 - Alter Index and Type for Image URL * @return bool diff --git a/xml/schema/Contact/DashboardContact.xml b/xml/schema/Contact/DashboardContact.xml index 95d990523a..8a5fc6791b 100644 --- a/xml/schema/Contact/DashboardContact.xml +++ b/xml/schema/Contact/DashboardContact.xml @@ -47,6 +47,13 @@ CASCADE 3.1 + + index_dashboard_id_contact_id + dashboard_id + contact_id + true + 4.7 + column_no boolean