Commit 65a7f3af authored by Cédric Anne's avatar Cédric Anne
Browse files

Give ability to prevent muting of MySQL deprecation warnings

parent 76f76c9e
#!/bin/bash
bin/console glpi:database:configure --config-dir=./tests --no-interaction --ansi --reconfigure --db-name=glpitest-9.5.3 --db-host=db --db-user=root --use-utf8mb4
bin/console glpi:database:configure --config-dir=./tests --no-interaction --ansi --reconfigure --db-name=glpitest-9.5.3 --db-host=db --db-user=root --use-utf8mb4 --log-deprecation-warnings
# Execute update
## First run should do the migration (with no warnings).
......
......@@ -215,7 +215,7 @@ jobs:
- name: "Install DB tests"
if: env.skip != 'true'
run: |
docker exec app bin/console glpi:database:install --config-dir=./tests --ansi --no-interaction --reconfigure --db-name=glpi --db-host=db --db-user=root | tee install.log
docker exec app bin/console glpi:database:install --config-dir=./tests --ansi --no-interaction --reconfigure --db-name=glpi --db-host=db --db-user=root --log-deprecation-warnings | tee install.log
if [[ -n $(grep "Warning" install.log) ]]; then echo "glpi:database:install command FAILED" && exit 1; fi
docker exec app bin/console glpi:database:update --config-dir=./tests --ansi --no-interaction | tee migration.log
if [[ -z $(grep "No migration needed." migration.log) ]]; then echo "glpi:database:update command FAILED" && exit 1; fi
......@@ -230,7 +230,7 @@ jobs:
- name: "Database tests"
if: env.skip != 'true'
run: |
docker exec app bin/console glpi:database:configure --config-dir=./tests --ansi --no-interaction --reconfigure --db-name=glpi --db-host=db --db-user=root --use-utf8mb4
docker exec app bin/console glpi:database:configure --config-dir=./tests --ansi --no-interaction --reconfigure --db-name=glpi --db-host=db --db-user=root --use-utf8mb4 --log-deprecation-warnings
docker exec app vendor/bin/atoum -p 'php -d memory_limit=512M' --debug --force-terminal --use-dot-report --bootstrap-file tests/bootstrap.php --no-code-coverage --fail-if-skipped-methods --max-children-number 1 -d tests/database
- name: "Unit tests"
if: env.skip != 'true'
......
......@@ -145,6 +145,13 @@ abstract class AbstractConfigureCommand extends AbstractCommand implements Force
InputOption::VALUE_NONE,
__('Reconfigure database, override configuration file if it already exists')
);
$this->addOption(
'log-deprecation-warnings',
null,
InputOption::VALUE_NONE,
__('Indicated if deprecation warnings sent by database server should be logged')
);
}
protected function interact(InputInterface $input, OutputInterface $output) {
......@@ -192,6 +199,7 @@ abstract class AbstractConfigureCommand extends AbstractCommand implements Force
$db_hostport = $db_host . (!empty($db_port) ? ':' . $db_port : '');
$reconfigure = $input->getOption('reconfigure');
$log_deprecation_warnings = $input->getOption('log-deprecation-warnings');
if (file_exists(GLPI_CONFIG_DIR . '/config_db.php') && !$reconfigure) {
// Prevent overriding of existing DB
......@@ -254,7 +262,7 @@ abstract class AbstractConfigureCommand extends AbstractCommand implements Force
'<comment>' . __('Saving configuration file...') . '</comment>',
OutputInterface::VERBOSITY_VERBOSE
);
if (!DBConnection::createMainConfig($db_hostport, $db_user, $db_pass, $db_name, $use_utf8mb4)) {
if (!DBConnection::createMainConfig($db_hostport, $db_user, $db_pass, $db_name, $use_utf8mb4, $log_deprecation_warnings)) {
$message = sprintf(
__('Cannot write configuration file "%s".'),
GLPI_CONFIG_DIR . DIRECTORY_SEPARATOR . 'config_db.php'
......
......@@ -54,7 +54,7 @@ class ConfigureCommand extends AbstractConfigureCommand {
'use-utf8mb4',
null,
InputOption::VALUE_NONE,
__('Use utf8mb4 character set.')
__('Use utf8mb4 character set')
);
}
......
......@@ -297,6 +297,7 @@ class InstallCommand extends AbstractConfigureCommand {
$DB->dbpassword = rawurlencode($db_pass);
$DB->dbdefault = $db_name;
$DB->use_utf8mb4 = true;
$DB->log_deprecation_warnings = $input->getOption('log-deprecation-warnings');
$DB->clearSchemaCache();
$DB->connect();
......
......@@ -53,15 +53,16 @@ class DBConnection extends CommonDBTM {
*
* @since 9.1
*
* @param string $host The DB host
* @param string $user The DB user
* @param string $password The DB password
* @param string $DBname The name of the DB
* @param string $host The DB host
* @param string $user The DB user
* @param string $password The DB password
* @param string $DBname The name of the DB
* @param boolean $use_utf8mb4 Flag that indicates if utf8mb4 charset/collation should be used
* @param boolean $log_deprecation_warnings Flag that indicates if DB deprecation warnings should be logged
*
* @return boolean
*
**/
static function createMainConfig($host, $user, $password, $DBname, $use_utf8mb4 = false) {
*/
static function createMainConfig($host, $user, $password, $DBname, $use_utf8mb4 = false, $log_deprecation_warnings = false) {
$DB_str = "<?php\nclass DB extends DBmysql {\n" .
" public \$dbhost = '$host';\n" .
......@@ -69,6 +70,7 @@ class DBConnection extends CommonDBTM {
" public \$dbpassword = '". rawurlencode($password) . "';\n" .
" public \$dbdefault = '$DBname';\n" .
" public \$use_utf8mb4 = " . ($use_utf8mb4 ? 'true' : 'false') . ";\n" .
" public \$log_deprecation_warnings = " . ($log_deprecation_warnings ? 'true' : 'false') . ";\n" .
"}\n";
return Toolbox::writeConfig('config_db.php', $DB_str);
......@@ -152,6 +154,7 @@ class DBConnection extends CommonDBTM {
}
};
$use_utf8mb4 = $master->use_utf8mb4;
$log_deprecation_warnings = $master->log_deprecation_warnings;
$DB_str = "<?php \n class DBSlave extends DBmysql { \n public \$slave = true; \n public \$dbhost = ";
$host = trim($host);
......@@ -177,6 +180,7 @@ class DBConnection extends CommonDBTM {
. " public \$dbpassword = '" . rawurlencode($password) . "'; \n"
. " public \$dbdefault = '" . $DBname . "'; \n"
. " public \$use_utf8mb4 = " . ($use_utf8mb4 ? 'true' : 'false') . "; \n"
. " public \$log_deprecation_warnings = " . ($log_deprecation_warnings ? 'true' : 'false') . "; \n"
. "}\n";
return Toolbox::writeConfig('config_db_slave.php', $DB_str);
......
......@@ -114,6 +114,14 @@ class DBmysql {
*/
public $use_utf8mb4 = false;
/**
* Determine if warnings related to MySQL deprecations should be logged too.
* Defaults to false as this option should only on development/test environment.
*
* @var bool
*/
public $log_deprecation_warnings = false;
/** Is it a first connection ?
* Indicates if the first connection attempt is successful or not
......@@ -1690,14 +1698,21 @@ class DBmysql {
$warnings = [];
if ($this->dbh->warning_count > 0 && $warnings_result = $this->dbh->query('SHOW WARNINGS')) {
// Exclude some warnings related to deprecated features.
// They are complicated to fix, so they are muted for now to not block the SQL warning logging feature.
$excludes = [
1287, // 'utf8mb3' is deprecated and will be removed in a future release. Please use utf8mb4 instead.
1681, // Integer display width is deprecated and will be removed in a future release.
3719, // 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
3778, // 'utf8_unicode_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
];
// Warnings to exclude
$excludes = [];
if (!$this->use_utf8mb4 || !$this->log_deprecation_warnings) {
// Exclude warnings related to usage of "utf8mb3" charset, as database has not been migrated yet.
$excludes[] = 1287; // 'utf8mb3' is deprecated and will be removed in a future release. Please use utf8mb4 instead.
$excludes[] = 3719; // 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
$excludes[] = 3778; // 'utf8_unicode_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
}
if (!$this->log_deprecation_warnings) {
// Mute deprecations related to elements that are heavilly used in old migrations and in plugins
// as it may require a lot of work to fix them.
$excludes[] = 1681; // Integer display width is deprecated and will be removed in a future release.
}
while ($warning = $warnings_result->fetch_assoc()) {
if ($warning['Level'] === 'Note' || in_array($warning['Code'], $excludes)) {
continue;
......
......@@ -407,6 +407,7 @@ SQL;
->exists();
$db->use_utf8mb4 = true;
$db->log_deprecation_warnings = false;
$this->when(
function () use ($db, $create_query_template, $drop_query_template) {
......
Supports Markdown
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