Skip to content

Commit

Permalink
CRM-21687 fix issue with MariaDB 10.2 logging query writing due to cu…
Browse files Browse the repository at this point in the history
…rrent_timestamp and timestamp getting () added on the end in Maria10.2
  • Loading branch information
seamuslee001 committed Jan 23, 2018
1 parent 62d684a commit 91bd5c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 5 additions & 2 deletions CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ private function _getColumnQuery($col, $createQuery) {
$line = preg_grep("/^ `$col` /", $createQuery);
$line = rtrim(array_pop($line), ',');
// CRM-11179
$line = $this->fixTimeStampAndNotNullSQL($line);
$line = self::fixTimeStampAndNotNullSQL($line);
return $line;
}

Expand Down Expand Up @@ -484,9 +484,12 @@ public function fixSchemaDifferencesForAll($rebuildTrigger = FALSE) {
*
* @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;
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/CRM/Logging/SchemaTest.php
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 91bd5c1

Please sign in to comment.