Skip to content

Commit

Permalink
replace MySQL-only indexes on text fields (hotpot_question.name + hot…
Browse files Browse the repository at this point in the history
…pot_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
  • Loading branch information
gbateson committed Sep 1, 2006
1 parent 5f941d1 commit 1897e3e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions mod/hotpot/db/mysql.php
Expand Up @@ -37,6 +37,10 @@ function hotpot_upgrade($oldversion) {
require_once $update_to_v2; require_once $update_to_v2;
$ok = $ok && hotpot_update_to_v2_1_18(); $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; return $ok;
} }
Expand Down
4 changes: 4 additions & 0 deletions mod/hotpot/db/postgres7.php
Expand Up @@ -45,6 +45,10 @@ function hotpot_upgrade($oldversion) {
require_once $update_to_v2; require_once $update_to_v2;
$ok = $ok && hotpot_update_to_v2_1_21(); $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; return $ok;
} }
Expand Down
52 changes: 52 additions & 0 deletions mod/hotpot/db/update_to_v2.php
@@ -1,4 +1,56 @@
<?PHP <?PHP
function hotpot=update_to_v2_2() {
global $CFG;
$ok = true;

// remove the index on hotpot_questions.name
$table = 'hotpot_questions';
$field = 'name';
$index = "{$table}_{$field}_idx";
if (strtolower($CFG->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() { function hotpot_update_to_v2_1_21() {
global $CFG; global $CFG;
$ok = true; $ok = true;
Expand Down
9 changes: 6 additions & 3 deletions mod/hotpot/lib.php
Expand Up @@ -1962,7 +1962,8 @@ function hotpot_add_response(&$attempt, &$question, &$response) {
$questionname = $question->name; $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 // add question record
if (!$question->id = insert_record('hotpot_questions', $question)) { if (!$question->id = insert_record('hotpot_questions', $question)) {
error("Could not add question record (attempt_id=$attempt->id): ".$db->ErrorMsg(), $next_url); error("Could not add question record (attempt_id=$attempt->id): ".$db->ErrorMsg(), $next_url);
Expand Down Expand Up @@ -2133,11 +2134,13 @@ function hotpot_string_id($str) {
if (isset($str) && $str<>'') { if (isset($str) && $str<>'') {


// get the id from the table if it is already there // 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 // create a string record
$record = NULL; $record = new stdClass();
$record->string = $str; $record->string = $str;
$record->md5key = $md5key;


// try and add the new string record // try and add the new string record
if (!$id = insert_record('hotpot_strings', $record)) { if (!$id = insert_record('hotpot_strings', $record)) {
Expand Down
10 changes: 9 additions & 1 deletion mod/hotpot/restorelib.php
Expand Up @@ -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; $ok = true;
foreach ($foreign_keys as $key=>$value) { foreach ($foreign_keys as $key=>$value) {
if (is_numeric($value)) { if (is_numeric($value)) {
Expand Down Expand Up @@ -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 // check all "not null" fields have been set
foreach ($table_columns[$table] as $column) { foreach ($table_columns[$table] as $column) {
if ($column->not_null) { if ($column->not_null) {
Expand Down
2 changes: 1 addition & 1 deletion mod/hotpot/version.php
Expand Up @@ -3,7 +3,7 @@
/// Code fragment to define the version of hotpot /// Code fragment to define the version of hotpot
/// This fragment is called by moodle_needs_upgrading() and /admin/index.php /// 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->release = 'v2.2.0'; // human-friendly version name (used in mod/hotpot/lib.php)
$module->requires = 2006080900; // Requires this Moodle version $module->requires = 2006080900; // Requires this Moodle version
$module->cron = 0; // period for cron to check this module (secs) $module->cron = 0; // period for cron to check this module (secs)
Expand Down

0 comments on commit 1897e3e

Please sign in to comment.