Skip to content

Commit

Permalink
Allow to distinguish between MySQL and MariaDB (also in installation)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Mar 28, 2023
1 parent aea8f07 commit 38b06b5
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 18 deletions.
12 changes: 12 additions & 0 deletions core/Db/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,16 @@ public function hasTables()
{
return $this->getSchema()->hasTables();
}

/**
* Adds a MAX_EXECUTION_TIME hint into a SELECT query if $limit is bigger than 1
*
* @param string $sql query to add hint to
* @param int $limit time limit in seconds
* @return string
*/
public function addMaxExecutionTimeHintToQuery($sql, $limit)
{
return $this->getSchema()->addMaxExecutionTimeHintToQuery($sql, $limit);
}
}
38 changes: 38 additions & 0 deletions core/Db/Schema/Mariadb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Db\Schema;

/**
* Mariadb schema
*/
class Mariadb extends Mysql
{
/**
* Adds a max_statement_time hint into a SELECT query if $limit is bigger than 1
*
* @param string $sql query to add hint to
* @param int $limit time limit in seconds
* @return string
*/
public static function addMaxExecutionTimeHintToQuery($sql, $limit)
{
if ($limit <= 0) {
return $sql;
}

$sql = trim($sql);
$pos = stripos($sql, 'SELECT');
if ($pos !== false) {
$maxExecutionTimeHint = 'SET STATEMENT max_statement_time='.$limit.' FOR ';
$sql = substr_replace($sql, $maxExecutionTimeHint . 'SELECT', $pos, strlen('SELECT'));
}

return $sql;
}
}
26 changes: 26 additions & 0 deletions core/Db/Schema/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,32 @@ public function truncateAllTables()
}
}

/**
* Adds a MAX_EXECUTION_TIME hint into a SELECT query if $limit is bigger than 1
*
* @param string $sql query to add hint to
* @param int $limit time limit in seconds
* @return string
*/
public static function addMaxExecutionTimeHintToQuery($sql, $limit)
{
if ($limit <= 0) {
return $sql;
}

$sql = trim($sql);
$pos = stripos($sql, 'SELECT');
if ($pos !== false) {
$timeInMs = $limit * 1000;
$timeInMs = (int) $timeInMs;
$maxExecutionTimeHint = ' /*+ MAX_EXECUTION_TIME('.$timeInMs.') */ ';

$sql = substr_replace($sql, 'SELECT ' . $maxExecutionTimeHint, $pos, strlen('SELECT'));
}

return $sql;
}

private function getTablePrefix()
{
return $this->getDbSettings()->getTablePrefix();
Expand Down
9 changes: 9 additions & 0 deletions core/Db/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@ public function getTableColumns($tableName);
* @return bool True if tables exist; false otherwise
*/
public function hasTables();

/**
* Adds a MAX_EXECUTION_TIME hint into a SELECT query if $limit is bigger than 1
*
* @param string $sql query to add hint to
* @param int $limit time limit in seconds
* @return string
*/
public static function addMaxExecutionTimeHintToQuery($sql, $limit);
}
17 changes: 1 addition & 16 deletions core/DbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,7 @@ public static function deleteArchiveTables()
*/
public static function addMaxExecutionTimeHintToQuery($sql, $limit)
{
if ($limit <= 0) {
return $sql;
}

$sql = trim($sql);
$pos = stripos($sql, 'SELECT');
if ($pos !== false) {

$timeInMs = $limit * 1000;
$timeInMs = (int) $timeInMs;
$maxExecutionTimeHint = ' /*+ MAX_EXECUTION_TIME('.$timeInMs.') */ ';

$sql = substr_replace($sql, 'SELECT ' . $maxExecutionTimeHint, $pos, strlen('SELECT'));
}

return $sql;
return Schema::getInstance()->addMaxExecutionTimeHintToQuery($sql, $limit);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions plugins/Diagnostics/Diagnostic/DatabaseAbilitiesCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function execute()
$result->addItem($this->checkTemporaryTables());
$result->addItem($this->checkTransactionLevel());

$databaseVersion = Db::fetchOne('SELECT VERSION();');

if (strpos(strtolower($databaseVersion), 'mariadb')) {
$comment = $this->translator->translate('Diagnostics_MariaDbNotConfigured');
$result->addItem(new DiagnosticResultItem(DiagnosticResult::STATUS_WARNING, $comment));
}

return [$result];
}

Expand Down
1 change: 1 addition & 0 deletions plugins/Diagnostics/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"MysqlTransactionLevel": "Changing transaction isolation level not supported. Archiving will still work but it may be slower and it is recommended to change for example the `binlog_format` to `row` if possible.",
"MysqlMaxPacketSize": "Max Packet Size",
"MysqlMaxPacketSizeWarning": "It is recommended to configure a 'max_allowed_packet' size in your MySQL database of at least %1$s. Configured is currently %2$s.",
"MariaDbNotConfigured": "It seems you are using a MariaDb server. To ensure all database features can be used, please ensure to set <code>[database] schema = Mariadb</code> in the \"config/config.ini.php\" file.",
"ConfigFileIntroduction": "Here you can view the Matomo configuration. If you are running Matomo in a load balanced environment, the page might be different depending on from which server this page is loaded. Rows with a different background color are changed config values that are specified for example in the %1$s file.",
"EnableRequiredDirectoriesDiagnostic": "This check was skipped as this check is disabled in the config. To enable this check set [General] enable_required_directories_diagnostic = 1 in the \"config/config.ini.php\" file.",
"HideUnchanged": "If you want to see only changed values you can %1$shide all unchanged values%2$s.",
Expand Down
10 changes: 8 additions & 2 deletions plugins/Installation/FormDatabaseSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ function init()
->loadOptions($adapters)
->addRule('required', Piwik::translate('General_Required', Piwik::translate('Installation_DatabaseSetupAdapter')));

$this->addElement('select', 'schema')
->setLabel(Piwik::translate('Installation_DatabaseSetupSchema'))
->loadOptions(['Mysql' => 'MySQL', 'Mariadb' => 'MariaDB'])
->addRule('required', Piwik::translate('General_Required', Piwik::translate('Installation_DatabaseSetupSchema')));

$this->addElement('submit', 'submit', array('value' => Piwik::translate('General_Next') . ' »', 'class' => 'btn'));

$defaultDatabaseType = Config::getInstance()->database['type'];
Expand All @@ -85,9 +90,10 @@ function init()
'host' => '127.0.0.1',
'type' => $defaultDatabaseType,
'tables_prefix' => 'matomo_',
'schema' => 'Mysql',
);

$defaultsEnvironment = array('host', 'adapter', 'tables_prefix', 'username', 'password', 'dbname');
$defaultsEnvironment = array('host', 'adapter', 'tables_prefix', 'username', 'schema', 'password', 'dbname');
foreach ($defaultsEnvironment as $name) {
$envName = 'DATABASE_' . strtoupper($name); // fyi getenv is case insensitive
$envNameMatomo = 'MATOMO_' . $envName;
Expand Down Expand Up @@ -130,7 +136,7 @@ public function createDatabaseObject()
'tables_prefix' => (is_null($tables_prefix)) ? $tables_prefix : trim($tables_prefix),
'adapter' => $adapter,
'port' => $port,
'schema' => Config::getInstance()->database['schema'],
'schema' => $this->getSubmitValue('schema'),
'type' => $this->getSubmitValue('type'),
'enable_ssl' => false
);
Expand Down
1 change: 1 addition & 0 deletions plugins/Installation/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"DatabaseSetupDatabaseName": "Database Name",
"DatabaseSetupLogin": "Login",
"DatabaseSetupServer": "Database Server",
"DatabaseSetupSchema": "Database Schema",
"DatabaseSetupTablePrefix": "Table Prefix",
"Email": "Email",
"Extension": "extension",
Expand Down

0 comments on commit 38b06b5

Please sign in to comment.