From 017ced524261111bfeb5ab5d40c075f36432a7cb Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 24 May 2011 19:11:01 +0200 Subject: [PATCH] When installing Chamilo in a single database mode, database can already exist so we don't need to create it. If the database doesn't exists we create it. --- main/install/install.lib.php | 46 +++++++++++++++++++++++++-------- main/install/install_db.inc.php | 15 +++++++++-- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/main/install/install.lib.php b/main/install/install.lib.php index 2f841cb9e5..a296b94356 100644 --- a/main/install/install.lib.php +++ b/main/install/install.lib.php @@ -597,27 +597,50 @@ function database_server_connect() { @Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5) } +function database_exists($database_name) { + $result = @Database::query("SHOW DATABASES LIKE '".Database::escape_string($database_name)."' "); + if (Database::num_rows($result)) { + return true; + } + return false; +} + /** * In step 3. Tests establishing connection to the database server. - * Tests also the possibility for multiple databases configuration. + * If it's a single database environment the function checks if the database exist. + * If the database doesn't exist we check the creation permissions. + * * @return int 1 when there is no problem; * 0 when a new database is impossible to be created, then the single/multiple database configuration is impossible too * -1 when there is no connection established. */ -function test_db_connect($dbHostForm, $dbUsernameForm, $dbPassForm, $singleDbForm, $dbPrefixForm) { +function test_db_connect($dbHostForm, $dbUsernameForm, $dbPassForm, $singleDbForm, $dbPrefixForm, $dbNameForm) { $dbConnect = -1; + //Checking user credentials if (@Database::connect(array('server' => $dbHostForm, 'username' => $dbUsernameForm, 'password' => $dbPassForm)) !== false) { - @Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5) - $multipleDbCheck = @Database::query("CREATE DATABASE ".$dbPrefixForm."test_chamilo_connection"); - if ($multipleDbCheck !== false) { - $multipleDbCheck = @Database::query("DROP DATABASE IF EXISTS ".$dbPrefixForm."test_chamilo_connection"); + $check_user_can_create_databases = true; + //Checking if single database exist + if ($singleDbForm) { + if (database_exists($dbPrefixForm.$dbNameForm)) { + $check_user_can_create_databases = false; + $dbConnect = 1; + } + } + + //Checking database creation + if ($check_user_can_create_databases) { + @Database::query("set session sql_mode='';"); // Disabling special SQL modes (MySQL 5) + $multipleDbCheck = @Database::query("CREATE DATABASE ".$dbPrefixForm."test_chamilo_connection"); if ($multipleDbCheck !== false) { - $dbConnect = 1; + $multipleDbCheck = @Database::query("DROP DATABASE IF EXISTS ".$dbPrefixForm."test_chamilo_connection"); + if ($multipleDbCheck !== false) { + $dbConnect = 1; + } else { + $dbConnect = 0; + } } else { $dbConnect = 0; } - } else { - $dbConnect = 0; } } else { $dbConnect = -1; @@ -1596,8 +1619,9 @@ function display_database_settings_form($installType, $dbHostForm, $dbUsernameFo echo get_lang('DBSettingUpgradeIntro'); echo ''; } else { + if (empty($dbPrefixForm)) { //make sure there is a default value for db prefix - $dbPrefixForm = 'chamilo_'; + //$dbPrefixForm = 'chamilo_'; } echo '

' . display_step_sequence() .get_lang('DBSetting') . '

'; echo '
'; @@ -1681,7 +1705,7 @@ function display_database_settings_form($installType, $dbHostForm, $dbUsernameFo
diff --git a/main/install/install_db.inc.php b/main/install/install_db.inc.php index d18143c199..4e6b32a9c3 100755 --- a/main/install/install_db.inc.php +++ b/main/install/install_db.inc.php @@ -62,7 +62,7 @@ if (empty($mysqlUserDb) || $mysqlUserDb == 'mysql' || $mysqlUserDb == $dbPrefixF } //This parameter is needed to run a command line to install Chamilo using BNPanel + ISPConfig see #1799 -if(!defined('CLI_INSTALLATION')) { +if (!defined('CLI_INSTALLATION')) { $result = Database::query("SHOW VARIABLES LIKE 'datadir'") or die(Database::error()); @@ -72,7 +72,18 @@ if(!defined('CLI_INSTALLATION')) { if (!$singleDbForm) { Database::query("DROP DATABASE IF EXISTS `$mysqlMainDb`") or die(Database::error()); } - Database::query("CREATE DATABASE IF NOT EXISTS `$mysqlMainDb`") or die(Database::error()); + + $create_database = true; + if ($singleDbForm) { + if (database_exists($mysqlMainDb)) { + $create_database = false; + } + } + + if ($create_database) { + Database::query("CREATE DATABASE IF NOT EXISTS `$mysqlMainDb`") or die(Database::error()); + } + } /**