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 24d2593
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 98 deletions.
142 changes: 54 additions & 88 deletions install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,102 +288,68 @@ public function __construct($dbh)
};
$timezones_requirement = new DbTimezones($db);

if (!empty($databasename)) { // use db already created
$DB_selected = $link->select_db($databasename);
if (empty($databasename) && empty($newdatabasename)) {
echo "<p>" . __("You didn't select a database!") . "</p>";
$prev_form($host, $user, $password);
return;
}

if (!$DB_selected) {
echo __('Impossible to use the database:');
if (empty($databasename) && !empty($newdatabasename)) {
// create new db
$databasename = $newdatabasename;

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
);
}
}

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 24d2593

Please sign in to comment.