From fa4d89443f28605fc741c2eab3bccdc420921a79 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Fri, 30 Jan 2015 18:07:53 -0500 Subject: [PATCH] Add install/upgrade scripts for 1.10.x --- main/inc/lib/add_course.lib.inc.php | 3 + main/install/db_main.sql | 2 + main/install/index.php | 2 +- main/install/migrate-db-1.9.0-1.10.0-pre.sql | 23 +++ main/install/update-db-1.9.0-1.10.0.inc.php | 177 ++++++++++++++++++ .../install/update-files-1.9.0-1.10.0.inc.php | 25 +++ 6 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 main/install/migrate-db-1.9.0-1.10.0-pre.sql create mode 100644 main/install/update-db-1.9.0-1.10.0.inc.php create mode 100644 main/install/update-files-1.9.0-1.10.0.inc.php diff --git a/main/inc/lib/add_course.lib.inc.php b/main/inc/lib/add_course.lib.inc.php index 5dcdb9c420..00fac20d6d 100755 --- a/main/inc/lib/add_course.lib.inc.php +++ b/main/inc/lib/add_course.lib.inc.php @@ -2015,6 +2015,9 @@ function create_course_tables($course_db_name = null) { Database::query($sql); + // New course tables for 1.10.x come here + + return 0; } diff --git a/main/install/db_main.sql b/main/install/db_main.sql index e44afcddaa..4629bf223d 100755 --- a/main/install/db_main.sql +++ b/main/install/db_main.sql @@ -3025,3 +3025,5 @@ CREATE TABLE usergroup_rel_question ( usergroup_id int unsigned not null, coefficient float(6,2) ); + +-- 1.10.x-specific, non-course-related, database changes \ No newline at end of file diff --git a/main/install/index.php b/main/install/index.php index ba8449b1f0..c66e9bf160 100755 --- a/main/install/index.php +++ b/main/install/index.php @@ -866,7 +866,7 @@ if (@$_POST['step2']) { case '1.9.8.2': case '1.9.10': include 'update-db-1.9.0-1.10.0.inc.php'; - //include 'update-files-1.9.0-1.10.0.inc.php'; + include 'update-files-1.9.0-1.10.0.inc.php'; //Only updates the configuration.inc.php with the new version include 'update-configuration.inc.php'; break; diff --git a/main/install/migrate-db-1.9.0-1.10.0-pre.sql b/main/install/migrate-db-1.9.0-1.10.0-pre.sql new file mode 100644 index 0000000000..6c4ea9bf59 --- /dev/null +++ b/main/install/migrate-db-1.9.0-1.10.0-pre.sql @@ -0,0 +1,23 @@ +-- This script updates the databases structure before migrating the data from +-- version 1.9.0 (or version 1.9.*) to version 1.10.0 +-- it is intended as a standalone script, however, because of the multiple +-- databases related difficulties, it should be parsed by a PHP script in +-- order to connect to and update the right databases. +-- There is one line per query, allowing the PHP function file() to read +-- all lines separately into an array. The xxMAINxx-type markers are there +-- to tell the PHP script which database we're talking about. +-- By always using the keyword "TABLE" in the queries, we should be able +-- to retrieve and modify the table name from the PHP script if needed, which +-- will allow us to deal with the unique-database-type installations +-- +-- This first part is for the main database + +-- xxMAINxx + + + +-- Do not move this query +UPDATE settings_current SET selected_value = '1.9.0.18716' WHERE variable = 'chamilo_database_version'; + +-- xxCOURSExx + diff --git a/main/install/update-db-1.9.0-1.10.0.inc.php b/main/install/update-db-1.9.0-1.10.0.inc.php new file mode 100644 index 0000000000..7260246f0b --- /dev/null +++ b/main/install/update-db-1.9.0-1.10.0.inc.php @@ -0,0 +1,177 @@ +'.get_lang('Error').' ! ' + .'Chamilo '.implode('|', $updateFromVersion).' '.get_lang('HasNotBeenFound').' + .

' + .get_lang('PleasGoBackToStep1') + .'

' + .'' + .'

' + .''; + exit (); + } + + $_configuration['db_glue'] = get_config_param('dbGlu'); + + if ($singleDbForm) { + $_configuration['table_prefix'] = get_config_param('courseTablePrefix'); + $_configuration['main_database'] = get_config_param('mainDbName'); + $_configuration['db_prefix'] = get_config_param('dbNamePrefix'); + } + + /* Normal upgrade procedure: start by updating the main database */ + + // If this script has been included by index.php, not update_courses.php, so + // that we want to change the main databases as well... + $onlyTest = false; + if (defined('SYSTEM_INSTALLATION')) { + + /** + * Update the databases "pre" migration + */ + include '../lang/english/create_course.inc.php'; + + if ($languageForm != 'english') { + // languageForm has been escaped in index.php + include '../lang/' . $languageForm . '/create_course.inc.php'; + } + + // Get the main queries list (m_q_list) + $sqlFile = 'migrate-db-' . $oldFileVersion . '-' . $newFileVersion . '-pre.sql'; + $mainQueriesList = get_sql_file_contents($sqlFile, 'main'); + + if (count($mainQueriesList) > 0) { + // Now use the $mainQueriesList + /** + * We connect to the right DB first to make sure we can use the queries + * without a database name + */ + if (strlen($dbNameForm) > 40) { + Log::error('Database name ' . $dbNameForm . ' is too long, skipping'); + } elseif (!in_array($dbNameForm, $dblist)) { + Log::error('Database ' . $dbNameForm . ' was not found, skipping'); + } else { + iDatabase::select_db($dbNameForm); + foreach ($mainQueriesList as $query) { + if ($onlyTest) { + Log::notice("iDatabase::query($dbNameForm,$query)"); + } else { + $res = iDatabase::query($query); + if ($res === false) { + Log::error('Error in ' . $query . ': ' . iDatabase::error()); + } + } + } + } + } + + if (INSTALL_TYPE_UPDATE == 'update') { + + /* + $sql = "SELECT selected_value FROM $dbNameForm.settings_current WHERE variable='use_session_mode' "; + $result = iDatabase::query($sql); + $result = Database::fetch_array($result); + $session_mode = $result['selected_value']; + if ($session_mode == 'true') + { ... } + */ + + } + } + + $prefix = ''; + if ($singleDbForm) { + $prefix = get_config_param('table_prefix'); + } + + Log::notice("Database prefix: '$prefix'"); + + // Get the courses databases queries list (c_q_list) + + $sqlFile = 'migrate-db-'.$oldFileVersion.'-'.$newFileVersion.'-pre.sql'; + $courseQueriesList = get_sql_file_contents($sqlFile, 'course'); + Log::notice('Starting migration: '.$oldFileVersion.' - '.$newFileVersion); + + if (count($courseQueriesList) > 0) { + // Get the courses list + if (strlen($dbNameForm) > 40) { + Log::error('Database name '.$dbNameForm.' is too long, skipping'); + } elseif (!in_array($dbNameForm, $dblist)) { + Log::error('Database '.$dbNameForm.' was not found, skipping'); + } else { + iDatabase::select_db($dbNameForm); + $res = iDatabase::query( + "SELECT id, code, db_name, directory, course_language, id as real_id " + ." FROM course WHERE target_course_code IS NULL ORDER BY code" + ); + + if ($res === false) { + die('Error while querying the courses list in update_db-1.9.0-1.10.0.inc.php'); + } + $errors = array(); + + if (iDatabase::num_rows($res) > 0) { + $i = 0; + $list = array(); + while ($row = iDatabase::fetch_array($res)) { + $list[] = $row; + $i++; + } + + foreach ($list as $rowCourse) { + if (!$singleDbForm) { // otherwise just use the main one + iDatabase::select_db($rowCourse['db_name']); + } + Log::notice('Course db ' . $rowCourse['db_name']); + + // Now use the $c_q_list + foreach ($courseQueriesList as $query) { + if ($singleDbForm) { + $query = preg_replace('/^(UPDATE|ALTER TABLE|CREATE TABLE|DROP TABLE|INSERT INTO|DELETE FROM)\s+(\w*)(.*)$/', "$1 $prefix{$rowCourse['db_name']}_$2$3", $query); + } + if ($onlyTest) { + Log::notice("iDatabase::query(".$rowCourse['db_name'].",$query)"); + } else { + $res = iDatabase::query($query); + if ($res === false) { + Log::error('Error in '.$query.': '.iDatabase::error()); + } + } + } + + Log::notice('<<<------- end -------->>'); + } + } + } + } +} else { + echo 'You are not allowed here !' . __FILE__; +} diff --git a/main/install/update-files-1.9.0-1.10.0.inc.php b/main/install/update-files-1.9.0-1.10.0.inc.php new file mode 100644 index 0000000000..fbe33d5fe7 --- /dev/null +++ b/main/install/update-files-1.9.0-1.10.0.inc.php @@ -0,0 +1,25 @@ +