Commit d92c1f85 authored by Johan Cwiklinski's avatar Johan Cwiklinski

Add check for db engine version

Add tests
Check db engine on update
parent 5303afe8
......@@ -89,6 +89,7 @@ jobs:
- image: circleci/php:latest-node-browsers
- image: circleci/mariadb:10.3
workflows:
version: 2
tests_all:
......
......@@ -1893,6 +1893,8 @@ class Config extends CommonDBTM {
self::displayCheckExtensions(true);
self::displayCheckDbEngine(true);
self::checkWriteAccessToDirs(true);
toolbox::checkSELinux(true);
......@@ -2221,6 +2223,48 @@ class Config extends CommonDBTM {
return true;
}
/**
* Display database engine checks report
*
* @since 9.3
*
* @param boolean $fordebug display for debug (no html required) (false by default)
*
* @return integer 2: missing extension, 1: missing optionnal extension, 0: OK,
**/
static function displayCheckDbEngine($fordebug = false) {
global $CFG_GLPI;
$error = 0;
$result = self::checkDbEngine();
$version = key($result);
$db_ver = $result[$version];
$ok_message = sprintf(__s('Database version seems correct (%s) - Perfect!'), $version);
$ko_message = sprintf(__('Your database engine version seems too old: %s.'), $version);
if (!$db_ver) {
$error = 2;
}
$message = $error > 0 ? $ko_message : $ok_message;
$html = '<tr class="tab_bg_1">';
$html .= "<td";
if ($error > 0) {
$html .= " class='red'";
}
$html .= "><img src='".$CFG_GLPI['root_doc']."/pics/";
$html .= ($error > 0 ? "ko_min" : "ok_min") . ".png' alt='$message'/>";
$html .= "$message</td>";
$html .= '</tr>';
if ($fordebug) {
echo $message . "\n";
} else {
echo $html;
}
return $error;
}
/**
* Display extensions checks report
......@@ -2274,6 +2318,28 @@ class Config extends CommonDBTM {
}
/**
* Check for needed extensions
*
* @since 9.3
*
* @return boolean
**/
static function checkDbEngine() {
global $DB;
// MySQL >= 5.6 || MariaDB >= 10
$raw = $DB->getVersion();
$db_ver = false;
preg_match('/(\d+(\.)?)+/', $raw, $found);
$version = $found[0];
$db_ver = version_compare($version, '5.6', '>=');
return [$version => $db_ver];
}
/**
* Check for needed extensions
*
......
......@@ -1202,4 +1202,16 @@ class DBmysql {
];
}
/**
* Get database raw version
*
* @return string
*/
public function getVersion() {
global $DB;
$req = $DB->request('SELECT version()')->next();
$raw = $req['version()'];
return $raw;
}
}
......@@ -1045,6 +1045,14 @@ class Toolbox {
$error = $suberr;
}
//database version check --is it posible?
echo "<tr class='tab_bg_1'><td class='b left'>".__('Testing DB engine version')."</td>";
$suberr = Config::displayCheckDbEngine();
if ($suberr > $error) {
$error = $suberr;
}
echo "</tr>";
// memory test
echo "<tr class='tab_bg_1'><td class='left b'>".__('Allocated memory test')."</td>";
......
......@@ -81,6 +81,10 @@ class Update extends CommonGLPI {
// Init debug variable
// Only show errors
Toolbox::setDebugMode(Session::DEBUG_MODE, 0, 0, 1);
$result = Config::displayCheckDbEngine(true);
if ($result > 0) {
die(1);
}
}
/**
......
......@@ -420,4 +420,51 @@ class Config extends DbTestCase {
->isIdenticalTo($expected);
}
/**
* Database engines data provider
*
* @return array
*/
protected function dbEngineProvider() {
return [
[
'raw' => '10.2.14-MariaDB',
'version' => '10.2.14',
'compat' => true
], [
'raw' => '5.5.10-MariaDB',
'version' => '5.5.10',
'compat' => false
], [
'raw' => '5.6.38-log',
'version' => '5.6.38',
'compat' => true
], [
'raw' => '5-5-57',
'version' => '5',
'compat' => false
], [
'raw' => '5-6-31',
'version' => '5',
'compat' => false // since version is 5, this is not compat.
], [
'raw' => '10-2-35',
'version' => '10',
'compat' => true
]
];
}
/**
* @dataProvider dbEngineProvider
*/
public function testCheckDbEngine($raw, $version, $compat) {
global $DB;
$DB = new \mock\DB();
$this->calling($DB)->getVersion = $raw;
$result = \Config::checkDbEngine();
$this->array($result)->isIdenticalTo([$version => $compat]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment