Commit a8405d30 authored by Johan Cwiklinski's avatar Johan Cwiklinski

Merge branch '9.5/bugfixes'

parents cd22385b 8d1a6a8d
......@@ -83,19 +83,17 @@ commands:
command: |
mysql -h 127.0.0.1 -u root -e 'create database glpitest080;'
mysql -h 127.0.0.1 -u root glpitest080 < tests/glpi-0.80-empty.sql
cp tests/circleci.db.yaml tests/db.yaml
bin/console glpi:database:configure --config-dir=./tests --no-interaction --reconfigure --db-name=glpitest0723 --db-host=127.0.0.1 --db-user=root
bin/console glpi:migration:myisam_to_innodb --config-dir=./tests --no-interaction
bin/console glpi:database:update --config-dir=./tests --allow-unstable --no-interaction
bin/console glpi:database:update --config-dir=./tests --allow-unstable --no-interaction |grep -q "No migration needed." || (echo "glpi:database:update command FAILED" && exit 1)
bin/console glpi:migration:myisam_to_innodb --config-dir=./tests --no-interaction
bin/console glpi:migration:timestamps --config-dir=./tests --no-interaction
rm tests/db.yaml
- run:
name: Database tests
command: |
cp tests/circleci.db.yaml tests/db.yaml
bin/console glpi:database:configure --config-dir=./tests --no-interaction --reconfigure --db-name=glpitest0723 --db-host=127.0.0.1 --db-user=root
php vendor/bin/atoum -p 'php -d memory_limit=512M' --debug --force-terminal --use-dot-report --configurations tests/telemetry.php --bootstrap-file tests/bootstrap.php --no-code-coverage --max-children-number 1 -d tests/database
rm tests/db.yaml
# Database install command.
install_db:
......@@ -104,7 +102,7 @@ commands:
name: Install DB
command: |
mysql -h 127.0.0.1 -u root -e 'create database glpitest;'
bin/console glpi:database:install --config-dir=./tests --no-interaction --db-name=glpitest --db-host=127.0.0.1 --db-user=root
bin/console glpi:database:install --config-dir=./tests --no-interaction --reconfigure --db-name=glpitest --db-host=127.0.0.1 --db-user=root
bin/console glpi:database:update --config-dir=./tests --no-interaction |grep -q "No migration needed." || (echo "glpi:database:update command FAILED" && exit 1)
cp tests/circleci.parameters.yaml tests/parameters.yaml
......
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2018 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/
namespace Glpi\Console\Database;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
use Config;
use DBConnection;
use Glpi\Console\AbstractCommand;
use Glpi\Console\Command\ForceNoPluginsOptionCommandInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
abstract class AbstractConfigureCommand extends AbstractCommand implements ForceNoPluginsOptionCommandInterface {
/**
* Error code returned if DB configuration is aborted by user.
*
* @var integer
*/
const ABORTED_BY_USER = -1;
/**
* Error code returned if DB configuration succeed.
*
* @var integer
*/
const SUCCESS = 0;
/**
* Error code returned if DB connection initialization fails.
*
* @var integer
*/
const ERROR_DB_CONNECTION_FAILED = 1;
/**
* Error code returned if DB engine is unsupported.
*
* @var integer
*/
const ERROR_DB_ENGINE_UNSUPPORTED = 2;
/**
* Error code returned when trying to configure and having a DB config already set.
*
* @var integer
*/
const ERROR_DB_CONFIG_ALREADY_SET = 3;
/**
* Error code returned when failing to save database configuration file.
*
* @var integer
*/
const ERROR_DB_CONFIG_FILE_NOT_SAVED = 4;
protected function configure() {
parent::configure();
$this->setName('glpi:database:install');
$this->setAliases(['db:install']);
$this->setDescription('Install database schema');
$this->addOption(
'db-host',
'H',
InputOption::VALUE_OPTIONAL,
__('Database host'),
'localhost'
);
$this->addOption(
'db-name',
'd',
InputOption::VALUE_REQUIRED,
__('Database name')
);
$this->addOption(
'db-password',
'p',
InputOption::VALUE_OPTIONAL,
__('Database password (will be prompted for value if option passed without value)'),
'' // Empty string by default (enable detection of null if passed without value)
);
$this->addOption(
'db-port',
'P',
InputOption::VALUE_OPTIONAL,
__('Database port')
);
$this->addOption(
'db-user',
'u',
InputOption::VALUE_REQUIRED,
__('Database user')
);
$this->addOption(
'reconfigure',
'r',
InputOption::VALUE_NONE,
__('Reconfigure database, override configuration file if it already exists')
);
}
protected function interact(InputInterface $input, OutputInterface $output) {
$questions = [
'db-name' => new Question(__('Database name:'), ''), // Required
'db-user' => new Question(__('Database user:'), ''), // Required
'db-password' => new Question(__('Database password:'), ''), // Prompt if null (passed without value)
];
$questions['db-password']->setHidden(true); // Make password input hidden
foreach ($questions as $name => $question) {
if (null === $input->getOption($name)) {
/** @var Symfony\Component\Console\Helper\QuestionHelper $question_helper */
$question_helper = $this->getHelper('question');
$value = $question_helper->ask($input, $output, $question);
$input->setOption($name, $value);
}
}
}
protected function initDbConnection() {
return; // Prevent DB connection
}
/**
* Save database configuration file.
*
* @param InputInterface $input
* @param OutputInterface $output
* @throws InvalidArgumentException
* @return string
*/
protected function configureDatabase(InputInterface $input, OutputInterface $output) {
$db_pass = $input->getOption('db-password');
$db_host = $input->getOption('db-host');
$db_name = $input->getOption('db-name');
$db_port = $input->getOption('db-port');
$db_user = $input->getOption('db-user');
$db_hostport = $db_host . (!empty($db_port) ? ':' . $db_port : '');
$reconfigure = $input->getOption('reconfigure');
$no_interaction = $input->getOption('no-interaction'); // Base symfony/console option
if (file_exists(GLPI_CONFIG_DIR . '/config_db.php') && !$reconfigure) {
// Prevent overriding of existing DB
$output->writeln(
'<error>' . __('Database configuration already exists. Use --reconfigure option to override existing configuration.') . '</error>'
);
return self::ERROR_DB_CONFIG_ALREADY_SET;
}
if (empty($db_name)) {
throw new InvalidArgumentException(
__('Database name defined by --db-name option cannot be empty.')
);
}
if (null === $db_pass) {
// Will be null if option used without value and without interaction
throw new InvalidArgumentException(
__('--db-password option value cannot be null.')
);
}
if (!$no_interaction) {
// Ask for confirmation (unless --no-interaction)
$informations = new Table($output);
$informations->addRow([__('Database host'), $db_hostport]);
$informations->addRow([__('Database name'), $db_name]);
$informations->addRow([__('Database user'), $db_user]);
$informations->render();
/** @var Symfony\Component\Console\Helper\QuestionHelper $question_helper */
$question_helper = $this->getHelper('question');
$run = $question_helper->ask(
$input,
$output,
new ConfirmationQuestion(__('Do you want to continue ?') . ' [Yes/no]', true)
);
if (!$run) {
$output->writeln(
'<comment>' . __('Configuration aborted.') . '</comment>',
OutputInterface::VERBOSITY_VERBOSE
);
return self::ABORTED_BY_USER;
}
}
$mysqli = new \mysqli();
@$mysqli->connect($db_host, $db_user, $db_pass, null, $db_port);
if (0 !== $mysqli->connect_errno) {
$message = sprintf(
__('Database connection failed with message "(%s) %s".'),
$mysqli->connect_errno,
$mysqli->connect_error
);
$output->writeln('<error>' . $message . '</error>', OutputInterface::VERBOSITY_QUIET);
return self::ERROR_DB_CONNECTION_FAILED;
}
ob_start();
$db_version_data = $mysqli->query('SELECT version()')->fetch_array();
$checkdb = Config::displayCheckDbEngine(false, $db_version_data[0]);
$message = ob_get_clean();
if ($checkdb > 0) {
$output->writeln('<error>' . $message . '</error>', OutputInterface::VERBOSITY_QUIET);
return self::ERROR_DB_ENGINE_UNSUPPORTED;
}
$db_name = $mysqli->real_escape_string($db_name);
$output->writeln(
'<comment>' . __('Saving configuration file...') . '</comment>',
OutputInterface::VERBOSITY_VERBOSE
);
if (!DBConnection::createMainConfig($db_hostport, $db_user, $db_pass, $db_name)) {
$message = sprintf(
__('Cannot write configuration file "%s".'),
GLPI_CONFIG_DIR . DIRECTORY_SEPARATOR . 'config_db.php'
);
$output->writeln(
'<error>' . $message . '</error>',
OutputInterface::VERBOSITY_QUIET
);
return self::ERROR_DB_CONFIG_FILE_NOT_SAVED;
}
return self::SUCCESS;
}
public function getNoPluginsOptionValue() {
return true;
}
}
......@@ -2431,10 +2431,16 @@ class Toolbox {
*
* @return void
**/
static function createSchema($lang = 'en_GB') {
static function createSchema($lang = 'en_GB', $db_instance = null) {
global $DB;
$DB = \Glpi\DatabaseFactory::create();
if (null === $db_instance) {
$DB = \Glpi\DatabaseFactory::create();
$db_instance = $DB;
} else {
$DB = $db_instance;
}
if (!$DB->runFile(GLPI_ROOT ."/install/mysql/glpi-empty.sql")) {
echo "Errors occurred inserting default database";
......
......@@ -30,6 +30,7 @@
* ---------------------------------------------------------------------
*/
<<<<<<< HEAD:src/Glpi/Application/View/TwigView.php
namespace Glpi\Application\View;
use Slim\Views\Twig;
......@@ -42,4 +43,36 @@ use Slim\Views\Twig;
class TwigView extends Twig
{
=======
namespace Glpi\Console\Database;
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ConfigureCommand extends AbstractConfigureCommand {
protected function configure() {
parent::configure();
$this->setName('glpi:database:configure');
$this->setAliases(['db:configure']);
$this->setDescription('Define database configuration');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$result = $this->configureDatabase($input, $output);
if (self::ABORTED_BY_USER === $result) {
return 0; // Considered as success
}
return $result;
}
>>>>>>> 9.5/bugfixes:inc/console/database/configurecommand.class.php
}
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