CRM-21687 fix issue with MariaDB 10.2 logging query writing due to current_timestamp...
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 23 Jan 2018 23:29:44 +0000 (10:29 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Tue, 23 Jan 2018 23:36:51 +0000 (10:36 +1100)
CRM/Logging/Schema.php
tests/phpunit/CRM/Logging/SchemaTest.php [new file with mode: 0644]

index 4551b89a64f6bae1c2e7bb56bc9500cb755fc256..e91f02d52ffb889ad654b28bea34813233fe3aaa 100644 (file)
@@ -444,7 +444,7 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
     $line = preg_grep("/^  `$col` /", $createQuery);
     $line = rtrim(array_pop($line), ',');
     // CRM-11179
-    $line = $this->fixTimeStampAndNotNullSQL($line);
+    $line = self::fixTimeStampAndNotNullSQL($line);
     return $line;
   }
 
@@ -484,9 +484,12 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
    *
    * @return mixed
    */
-  public function fixTimeStampAndNotNullSQL($query) {
+  public static function fixTimeStampAndNotNullSQL($query) {
+    $query = str_ireplace("TIMESTAMP() NOT NULL", "TIMESTAMP NULL", $query);
     $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query);
+    $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()", '', $query);
     $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query);
+    $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP()", '', $query);
     $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query);
     $query = str_ireplace("NOT NULL", '', $query);
     return $query;
diff --git a/tests/phpunit/CRM/Logging/SchemaTest.php b/tests/phpunit/CRM/Logging/SchemaTest.php
new file mode 100644 (file)
index 0000000..889cdaf
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * Class CRM_Logging_SchmeaTest
+ * @group headless
+ */
+class CRM_Logging_SchemaTest extends CiviUnitTestCase {
+
+  public function setUp() {
+    parent::setUp();
+  }
+
+  public function tearDown() {
+    parent::tearDown();
+  }
+
+  public function queryExamples() {
+    $examples = [];
+    $examples[] = ["`modified_date` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'", "`modified_date` timestamp NULL  COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'"];
+    $examples[] = ["`modified_date` timestamp NULL DEFAULT current_timestamp ON UPDATE current_timestamp COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'", "`modified_date` timestamp NULL  COMMENT 'When the mailing (or closely related entity) was created or modified or deleted.'"];
+    return $examples;
+  }
+
+  /**
+   * Tests the function fixTimeStampAndNotNullSQL in CRM_Logging_Schema
+   *
+   * @dataProvider queryExamples
+   */
+  public function testQueryRewrite($query, $expectedQuery) {
+    $this->assertEquals($expectedQuery, CRM_Logging_Schema::fixTimeStampAndNotNullSQL($query));
+  }
+
+}