From: gbateson Date: Fri, 1 Sep 2006 02:08:39 +0000 (+0000) Subject: replace MySQL-only indexes on text fields (hotpot_question.name + hotpot_strings... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=1897e3ef9543c3bef9425be9a534bf9080190d14;p=moodle.git replace MySQL-only indexes on text fields (hotpot_question.name + hotpot_strings.string) with two new md5key fields (hotpot_question.md5key + hotpot_strings.md5key), which are the MD5 hash of the respective text fields. Microincrement version number to 2006083101 --- diff --git a/mod/hotpot/db/mysql.php b/mod/hotpot/db/mysql.php index ab75b1e84f..551d0a799f 100644 --- a/mod/hotpot/db/mysql.php +++ b/mod/hotpot/db/mysql.php @@ -37,6 +37,10 @@ function hotpot_upgrade($oldversion) { require_once $update_to_v2; $ok = $ok && hotpot_update_to_v2_1_18(); } + if ($oldversion < 2006083101) { + require_once $update_to_v2; + $ok = $ok && hotpot_update_to_v2_2(); + } return $ok; } diff --git a/mod/hotpot/db/postgres7.php b/mod/hotpot/db/postgres7.php index e5f80e4f94..59ee021610 100644 --- a/mod/hotpot/db/postgres7.php +++ b/mod/hotpot/db/postgres7.php @@ -45,6 +45,10 @@ function hotpot_upgrade($oldversion) { require_once $update_to_v2; $ok = $ok && hotpot_update_to_v2_1_21(); } + if ($oldversion < 2006083101) { + require_once $update_to_v2; + $ok = $ok && hotpot_update_to_v2_2(); + } return $ok; } diff --git a/mod/hotpot/db/update_to_v2.php b/mod/hotpot/db/update_to_v2.php index b8eb14a854..0406adf54d 100644 --- a/mod/hotpot/db/update_to_v2.php +++ b/mod/hotpot/db/update_to_v2.php @@ -1,4 +1,56 @@ dbtype)=='postgres7') { + $index = "{$CFG->prefix}$index"; + } + hotpot_db_delete_index("{$CFG->prefix}$table", $index); + + // add new hotpot_questions.md5key field (and index) + $table = 'hotpot_questions'; + $field = 'md5key'; + $ok = $ok && hotpot_db_update_field_type($table, '', $field, 'VARCHAR', 32, '', 'NOT NULL', '', 'name'); + $ok = $ok && hotpot_db_add_index($table, $field); + + // add new values hotpot_questions.md5key + $table = 'hotpot_questions'; + if ($records = get_records($table)) { + foreach ($records as $record) { + $ok = $ok && set_field($table, 'md5key', md5($record->name), 'id', $record->id); + } + } + + // remove the index on hotpot_strings.string + $table = 'hotpot_strings'; + $field = 'string'; + $index = "{$table}_{$field}_idx"; + if (strtolower($CFG->dbtype)=='postgres7') { + $index = "{$CFG->prefix}$index"; + } + hotpot_db_delete_index("{$CFG->prefix}$table", $index); + + // add new hotpot_strings.md5key field (and index) + $table = 'hotpot_strings'; + $field = 'md5key'; + $ok = $ok && hotpot_db_update_field_type($table, '', $field, 'VARCHAR', 32, '', 'NOT NULL', '', 'string'); + $ok = $ok && hotpot_db_add_index($table, $field); + + // add new values hotpot_strings.md5key + $table = 'hotpot_strings'; + if ($records = get_records($table)) { + foreach ($records as $record) { + $ok = $ok && set_field($table, 'md5key', md5($record->string), 'id', $record->id); + } + } + + return $ok; +} function hotpot_update_to_v2_1_21() { global $CFG; $ok = true; diff --git a/mod/hotpot/lib.php b/mod/hotpot/lib.php index 133bd88c36..e7c0eb838e 100644 --- a/mod/hotpot/lib.php +++ b/mod/hotpot/lib.php @@ -1962,7 +1962,8 @@ function hotpot_add_response(&$attempt, &$question, &$response) { $questionname = $question->name; } - if (!$question->id = get_field('hotpot_questions', 'id', 'name', $question->name, 'hotpot', $attempt->hotpot)) { + $question->md5key = md5($question->name); + if (!$question->id = get_field('hotpot_questions', 'id', 'hotpot', $attempt->hotpot, 'md5key', $question->md5key, 'name', $question->name)) { // add question record if (!$question->id = insert_record('hotpot_questions', $question)) { error("Could not add question record (attempt_id=$attempt->id): ".$db->ErrorMsg(), $next_url); @@ -2133,11 +2134,13 @@ function hotpot_string_id($str) { if (isset($str) && $str<>'') { // get the id from the table if it is already there - if (!$id = get_field('hotpot_strings', 'id', 'string', $str)) { + $md5key = md5($str); + if (!$id = get_field('hotpot_strings', 'id', 'md5key', $md5key, 'string', $str)) { // create a string record - $record = NULL; + $record = new stdClass(); $record->string = $str; + $record->md5key = $md5key; // try and add the new string record if (!$id = insert_record('hotpot_strings', $record)) { diff --git a/mod/hotpot/restorelib.php b/mod/hotpot/restorelib.php index c68b2f0f6f..8dc340b160 100644 --- a/mod/hotpot/restorelib.php +++ b/mod/hotpot/restorelib.php @@ -299,7 +299,7 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys, } } - // update secondary keys, if any + // update foreign keys, if any $ok = true; foreach ($foreign_keys as $key=>$value) { if (is_numeric($value)) { @@ -336,6 +336,14 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys, } } + // set md5 keys if necessary (restoring from Moodle<1.6) + if ($table=='hotpot_questions' && empty($record->md5key)) { + $record->md5key = md5($record->name); + } + if ($table=='hotpot_strings' && empty($record->md5key)) { + $record->md5key = md5($record->string); + } + // check all "not null" fields have been set foreach ($table_columns[$table] as $column) { if ($column->not_null) { diff --git a/mod/hotpot/version.php b/mod/hotpot/version.php index abb56c6271..b6dbdef88b 100644 --- a/mod/hotpot/version.php +++ b/mod/hotpot/version.php @@ -3,7 +3,7 @@ /// Code fragment to define the version of hotpot /// This fragment is called by moodle_needs_upgrading() and /admin/index.php ///////////////////////////////////////////////////////////////////////////////// -$module->version = 2006083100; // release date of this version (see note below) +$module->version = 2006083101; // release date of this version (see note below) $module->release = 'v2.2.0'; // human-friendly version name (used in mod/hotpot/lib.php) $module->requires = 2006080900; // Requires this Moodle version $module->cron = 0; // period for cron to check this module (secs)