CRM-21364 Non controversial fixes for ONLY_FULL_GROUP_BY and NO_ZERO_DATE sqlModes
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 31 Oct 2017 23:22:31 +0000 (10:22 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 2 Nov 2017 07:23:10 +0000 (18:23 +1100)
CRM/Activity/BAO/Activity.php
CRM/Mailing/Event/BAO/Opened.php
tests/phpunit/CRM/Contact/BAO/GroupContactTest.php
tests/phpunit/api/v3/CustomApiTest.php

index 05c3f6bb31cd1b5c70aea456605ef6186ae05137..a591d350f760803e05134412bc7cf9d5cb65dffe 100644 (file)
@@ -909,7 +909,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     );
 
     $sql = "CREATE TEMPORARY TABLE {$activityTempTable} ( ";
-    $insertValueSQL = array();
+    $insertValueSQL = $selectColumns = array();
     // The activityTempTable contains the sorted rows
     // so in order to maintain the sort order as-is we add an auto_increment
     // field; we can sort by this later to ensure the sort order stays correct.
@@ -917,6 +917,12 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     foreach ($tableFields as $name => $desc) {
       $sql .= "$name $desc,\n";
       $insertValueSQL[] = $name;
+      if ($name == 'source_contact_name' && CRM_Utils_SQL::supportsFullGroupBy()) {
+        $selectColumns[] = "ANY_VALUE(tbl.$name)";
+      }
+      else {
+        $selectColumns[] = "tbl.$name";
+      }
     }
 
     // add unique key on activity_id just to be sure
@@ -929,7 +935,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
 
     CRM_Core_DAO::executeQuery($sql);
 
-    $insertSQL = "INSERT INTO {$activityTempTable} (" . implode(',', $insertValueSQL) . " ) ";
+    $insertSQL = "INSERT IGNORE INTO {$activityTempTable} (" . implode(',', $insertValueSQL) . " ) ";
 
     $order = $limit = $groupBy = '';
     $groupBy = " GROUP BY tbl.activity_id, tbl.activity_type, tbl.case_id, tbl.case_subject ";
@@ -961,9 +967,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     $input['count'] = FALSE;
     list($sqlClause, $params) = self::deprecatedGetActivitySQLClause($input);
 
-    $query = "{$insertSQL}
-       SELECT DISTINCT tbl.*  from ( {$sqlClause} )
-as tbl ";
+    $query = sprintf("{$insertSQL} \n SELECT DISTINCT %s  from ( %s ) \n as tbl ", implode(', ', $selectColumns), $sqlClause);
 
     // Filter case activities - CRM-5761.
     $components = self::activityComponents();
index 11b99fadd56d70d6f3dc3e3654c62078c1e2455a..a1f63b4ec89dfd5034390c06f168544efdddc8d2 100644 (file)
@@ -283,7 +283,10 @@ class CRM_Mailing_Event_BAO_Opened extends CRM_Mailing_Event_DAO_Opened {
 
     $query .= $groupBy;
 
-    $orderBy = "sort_name ASC, {$open}.time_stamp DESC";
+    $orderBy = "sort_name ASC";
+    if (!$is_distinct) {
+      $orderBy .= ", {$open}.time_stamp DESC";
+    }
     if ($sort) {
       if (is_string($sort)) {
         $sort = CRM_Utils_Type::escape($sort, 'String');
index 3de4a6e18752ad347dcc169ea2f280c30048b543..2fd075ec9996481e41151a61efd5f20710fe1865 100644 (file)
@@ -329,7 +329,7 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase {
     foreach ($useCases as $case) {
       $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
       list($select, $from, $where, $having) = $query->query();
-      $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where ORDER BY contact_a.first_name")->fetchAll();
+      $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id, contact_a.first_name $from $where ORDER BY contact_a.first_name")->fetchAll();
       foreach ($groupContacts as $key => $value) {
         $groupContacts[$key] = $value['id'];
       }
index 715cc2a73daeb82850cbe61122dd4fd1345bde45..2b27d146d9897536e76b5cef693eb42dfbba9514 100644 (file)
@@ -60,8 +60,8 @@ class api_v3_CustomApiTest extends CiviUnitTestCase {
     $this->callAPISuccess('Mailing', 'create', array('name' => 'CiviMail', 'hash' => 'abx'));
     $result = $this->callAPISuccess('MailingProviderData', 'get', array('return' => array('mailing_identifier.name', 'contact_identifier', 'mailing_identifier')));
     $this->assertEquals(1, $result['count']);
-    $this->assertEquals('xyzabx0000-00-00 00:00:00', $result['id']);
-    $this->assertEquals('xyzabx0000-00-00 00:00:00', $result['id']);
+    $this->assertEquals('xyzabx2017-01-01 00:00:00', $result['id']);
+    $this->assertEquals('xyzabx2017-01-01 00:00:00', $result['id']);
     $this->assertEquals(array(
         'contact_identifier' => 'xyz',
         'mailing_identifier' => 'abx',
@@ -97,7 +97,7 @@ class api_v3_CustomApiTest extends CiviUnitTestCase {
    `mailing_identifier` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
    `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
    `event_type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
-  `recipient_action_datetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
+  `recipient_action_datetime` timestamp NOT NULL DEFAULT '2017-01-01 00:00:00',
    `contact_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
    `is_civicrm_updated`  TINYINT(4) DEFAULT '0',
  PRIMARY KEY (`contact_identifier`,`recipient_action_datetime`,`event_type`),