Skip to content

Commit

Permalink
Adapt install/update processes
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Nov 4, 2024
1 parent d389323 commit be96238
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 107 deletions.
6 changes: 0 additions & 6 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\.$#',
Expand Down
147 changes: 56 additions & 91 deletions install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 "<p>" . __("You didn't select a database!") . "</p>";
$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 "<br>" . 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 "<p>" . __('Initializing database tables and default data...') . "</p>";
Toolbox::createSchema($_SESSION["glpilanguage"]);
echo "<p>" . __('OK - database was initialized') . "</p>";

$next_form();
} else { // can't create config_db file
echo "<p>" . __('Impossible to write the database setup file') . "</p>";
$prev_form($host, $user, $password);
}
return;
}
} else if (!empty($newdatabasename)) { // create new db
// Try to connect
if ($link->select_db($newdatabasename)) {
echo "<p>" . __('Database created') . "</p>";

$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 "<p>" . __('Initializing database tables and default data...') . "</p>";
Toolbox::createSchema($_SESSION["glpilanguage"]);
echo "<p>" . __('OK - database was initialized') . "</p>";
$next_form();
} else { // can't create config_db file
echo "<p>" . __('Impossible to write the database setup file') . "</p>";
$prev_form($host, $user, $password);
}
} else { // try to create the DB
if ($link->query("CREATE DATABASE IF NOT EXISTS `" . $newdatabasename . "`")) {
echo "<p>" . __('Database created') . "</p>";

$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 "<p>" . __('Initializing database tables and default data...') . "</p>";
Toolbox::createSchema($_SESSION["glpilanguage"]);
echo "<p>" . __('OK - database was initialized') . "</p>";
$next_form();
} else { // can't create config_db file
echo "<p>" . __('Impossible to write the database setup file') . "</p>";
$prev_form($host, $user, $password);
}
} else { // can't create database
echo __('Error in creating database!');
echo "<br>" . sprintf(__('The server answered: %s'), $link->error);
$prev_form($host, $user, $password);
}
}
} else { // no db selected
echo "<p>" . __("You didn't select a database!") . "</p>";
if (!$link->select_db($databasename)) {
echo __('Impossible to use the database:');
echo "<br>" . 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 "<p>" . __('Initializing database tables and default data...') . "</p>";
try {
Toolbox::createSchema($_SESSION["glpilanguage"]);
} catch (\Throwable $e) {
echo "<p>"
. sprintf(
__('An error occurred during the database initialization. The error was: %s'),
'<br />' . $e->getMessage()
)
. "</p>";
@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 "<p>" . __('OK - database was initialized') . "</p>";

$next_form();
} else { // can't create config_db file
echo "<p>" . __('Impossible to write the database setup file') . "</p>";
$prev_form($host, $user, $password);
}
}

//send telemetry information
Expand Down
4 changes: 1 addition & 3 deletions src/DBmysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/Glpi/Console/Database/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,15 @@ public function __construct($dbh)
'<comment>' . __('Loading default schema...') . '</comment>',
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('<error>' . $message . '</error>', OutputInterface::VERBOSITY_QUIET);
return self::ERROR_SCHEMA_CREATION_FAILED;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit be96238

Please sign in to comment.