From be96238a5d0b53148cf32a6ff7ece4d4ffa98157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Mon, 4 Nov 2024 10:59:32 +0100 Subject: [PATCH] Adapt install/update processes --- .phpstan-baseline.php | 6 - install/install.php | 147 +++++++------------ src/DBmysql.php | 4 +- src/Glpi/Console/Database/InstallCommand.php | 15 +- src/Update.php | 11 +- 5 files changed, 76 insertions(+), 107 deletions(-) diff --git a/.phpstan-baseline.php b/.phpstan-baseline.php index 28d342e9983..d20b164f87c 100644 --- a/.phpstan-baseline.php +++ b/.phpstan-baseline.php @@ -79,12 +79,6 @@ 'count' => 2, 'path' => __DIR__ . '/install/empty_data.php', ]; -$ignoreErrors[] = [ - // identifier: if.alwaysFalse - 'message' => '#^If condition is always false\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/install/install.php', -]; $ignoreErrors[] = [ // identifier: foreach.emptyArray 'message' => '#^Empty array passed to foreach\\.$#', diff --git a/install/install.php b/install/install.php index 193c5e8f2a3..72e7cc070b5 100644 --- a/install/install.php +++ b/install/install.php @@ -277,9 +277,6 @@ function step4($databasename, $newdatabasename) $link = new mysqli($hostport[0], $user, $password, '', $hostport[1]); } - $databasename = $link->real_escape_string($databasename); - $newdatabasename = $link->real_escape_string($newdatabasename); - $db = new class ($link) extends DBmysql { public function __construct($dbh) { @@ -288,102 +285,70 @@ public function __construct($dbh) }; $timezones_requirement = new DbTimezones($db); - if (!empty($databasename)) { // use db already created - $DB_selected = $link->select_db($databasename); + if ($databasename === '' && $newdatabasename === '') { + echo "

" . __("You didn't select a database!") . "

"; + $prev_form($host, $user, $password); + return; + } + + if ($databasename !== '' && $newdatabasename !== '') { + // create new db + $databasename = $link->real_escape_string($newdatabasename); - if (!$DB_selected) { - echo __('Impossible to use the database:'); + if ( + !$link->select_db($databasename) + && !$link->query("CREATE DATABASE IF NOT EXISTS `" . $databasename . "`") + ) { + echo __('Error in creating database!'); echo "
" . sprintf(__('The server answered: %s'), $link->error); $prev_form($host, $user, $password); - } else { - $success = DBConnection::createMainConfig( - $host, - $user, - $password, - $databasename, - use_timezones: $timezones_requirement->isValidated(), - log_deprecation_warnings: false, - use_utf8mb4: true, - allow_datetime: false, - allow_signed_keys: false - ); - if ($success) { - echo "

" . __('Initializing database tables and default data...') . "

"; - Toolbox::createSchema($_SESSION["glpilanguage"]); - echo "

" . __('OK - database was initialized') . "

"; - - $next_form(); - } else { // can't create config_db file - echo "

" . __('Impossible to write the database setup file') . "

"; - $prev_form($host, $user, $password); - } + return; } - } else if (!empty($newdatabasename)) { // create new db - // Try to connect - if ($link->select_db($newdatabasename)) { - echo "

" . __('Database created') . "

"; - - $success = DBConnection::createMainConfig( - $host, - $user, - $password, - $newdatabasename, - use_timezones: $timezones_requirement->isValidated(), - log_deprecation_warnings: false, - use_utf8mb4: true, - allow_datetime: false, - allow_signed_keys: false - ); - if ($success) { - echo "

" . __('Initializing database tables and default data...') . "

"; - Toolbox::createSchema($_SESSION["glpilanguage"]); - echo "

" . __('OK - database was initialized') . "

"; - $next_form(); - } else { // can't create config_db file - echo "

" . __('Impossible to write the database setup file') . "

"; - $prev_form($host, $user, $password); - } - } else { // try to create the DB - if ($link->query("CREATE DATABASE IF NOT EXISTS `" . $newdatabasename . "`")) { - echo "

" . __('Database created') . "

"; - - $select_db = $link->select_db($newdatabasename); - $success = false; - if ($select_db) { - $success = DBConnection::createMainConfig( - $host, - $user, - $password, - $newdatabasename, - use_timezones: $timezones_requirement->isValidated(), - log_deprecation_warnings: false, - use_utf8mb4: true, - allow_datetime: false, - allow_signed_keys: false - ); - } + } else { + $databasename = $link->real_escape_string($databasename); + } - if ($success) { - echo "

" . __('Initializing database tables and default data...') . "

"; - Toolbox::createSchema($_SESSION["glpilanguage"]); - echo "

" . __('OK - database was initialized') . "

"; - $next_form(); - } else { // can't create config_db file - echo "

" . __('Impossible to write the database setup file') . "

"; - $prev_form($host, $user, $password); - } - } else { // can't create database - echo __('Error in creating database!'); - echo "
" . sprintf(__('The server answered: %s'), $link->error); - $prev_form($host, $user, $password); - } - } - } else { // no db selected - echo "

" . __("You didn't select a database!") . "

"; + if (!$link->select_db($databasename)) { + echo __('Impossible to use the database:'); + echo "
" . sprintf(__('The server answered: %s'), $link->error); $prev_form($host, $user, $password); + return; } - $link->close(); + $success = DBConnection::createMainConfig( + $host, + $user, + $password, + $databasename, + use_timezones: $timezones_requirement->isValidated(), + log_deprecation_warnings: false, + use_utf8mb4: true, + allow_datetime: false, + allow_signed_keys: false + ); + + if ($success) { + echo "

" . __('Initializing database tables and default data...') . "

"; + try { + Toolbox::createSchema($_SESSION["glpilanguage"]); + } catch (\Throwable $e) { + echo "

" + . sprintf( + __('An error occurred during the database initialization. The error was: %s'), + '
' . $e->getMessage() + ) + . "

"; + @unlink(GLPI_CONFIG_DIR . '/config_db.php'); // try to remove the config file, to be able to restart the process + $prev_form($host, $user, $password); + return; + } + echo "

" . __('OK - database was initialized') . "

"; + + $next_form(); + } else { // can't create config_db file + echo "

" . __('Impossible to write the database setup file') . "

"; + $prev_form($host, $user, $password); + } } //send telemetry information diff --git a/src/DBmysql.php b/src/DBmysql.php index 8578a8ccff2..7033f4fd4af 100644 --- a/src/DBmysql.php +++ b/src/DBmysql.php @@ -1030,9 +1030,7 @@ public function runFile($path) $query = trim($query); if ($query != '') { $query = htmlentities($query, ENT_COMPAT, 'UTF-8'); - if (!$this->doQuery($query)) { - return false; - } + $this->doQuery($query); if (!isCommandLine()) { // Flush will prevent proxy to timeout as it will receive data. // Flush requires a content to be sent, so we sent spaces as multiple spaces diff --git a/src/Glpi/Console/Database/InstallCommand.php b/src/Glpi/Console/Database/InstallCommand.php index 1d65ad68ae0..9fb6405d36a 100644 --- a/src/Glpi/Console/Database/InstallCommand.php +++ b/src/Glpi/Console/Database/InstallCommand.php @@ -306,12 +306,15 @@ public function __construct($dbh) '' . __('Loading default schema...') . '', OutputInterface::VERBOSITY_VERBOSE ); - // TODO Get rid of output buffering - ob_start(); - $this->db->connect(); // Reconnect DB to ensure it uses update configuration (see `self::configureDatabase()`) - Toolbox::createSchema($default_language, $this->db); - $message = ob_get_clean(); - if (!empty($message)) { + + try { + $this->db->connect(); // Reconnect DB to ensure it uses update configuration (see `self::configureDatabase()`) + Toolbox::createSchema($default_language, $this->db); + } catch (\Throwable $e) { + $message = sprintf( + __('An error occurred during the database initialization. The error was: %s'), + $e->getMessage() + ); $output->writeln('' . $message . '', OutputInterface::VERBOSITY_QUIET); return self::ERROR_SCHEMA_CREATION_FAILED; } diff --git a/src/Update.php b/src/Update.php index a0b143929d0..a8e03c48bc3 100644 --- a/src/Update.php +++ b/src/Update.php @@ -259,7 +259,16 @@ function (string $flag) { $migrations = $this->getMigrationsToDo($current_version, $force_latest); foreach ($migrations as $key => $migration_specs) { include_once($migration_specs['file']); - $migration_specs['function'](); + + try { + $migration_specs['function'](); + } catch (\Throwable $e) { + $this->migration->displayError(sprintf( + __('An error occurred during the update. The error was: %s'), + $e->getMessage() + )); + die(1); + } if ($key !== array_key_last($migrations)) { // Set current version to target version to ensure complete migrations to not be replayed if one