Skip to content

Commit

Permalink
GH-723 Rename too long DB table
Browse files Browse the repository at this point in the history
Changes local_catquiz_question_hashmap to local_catquiz_qhashmap
  • Loading branch information
davidszkiba committed Nov 18, 2024
1 parent 9e78079 commit b064f60
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
12 changes: 6 additions & 6 deletions classes/remote/hash/question_hasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/**
* Handles question hash generation and verification.
*
*
* Idea: Include model name and scale in calculation of hash.
*
* @package local_catquiz
Expand Down Expand Up @@ -75,11 +75,11 @@ public static function generate_hash($questionid) {
$record->timemodified = $record->timecreated;

// Store or update hash mapping.
if ($existing = $DB->get_record('local_catquiz_question_hashmap', ['questionid' => $questionid])) {
if ($existing = $DB->get_record('local_catquiz_qhashmap', ['questionid' => $questionid])) {
$record->id = $existing->id;
$DB->update_record('local_catquiz_question_hashmap', $record);
$DB->update_record('local_catquiz_qhashmap', $record);
} else {
$DB->insert_record('local_catquiz_question_hashmap', $record);
$DB->insert_record('local_catquiz_qhashmap', $record);
}

return $record->questionhash;
Expand All @@ -94,7 +94,7 @@ public static function generate_hash($questionid) {
public static function get_questionid_from_hash($hash) {
global $DB;

if ($record = $DB->get_record('local_catquiz_question_hashmap', ['questionhash' => $hash])) {
if ($record = $DB->get_record('local_catquiz_qhashmap', ['questionhash' => $hash])) {
return $record->questionid;
}
return null;
Expand All @@ -109,7 +109,7 @@ public static function get_questionid_from_hash($hash) {
public static function verify_hash($questionid) {
global $DB;

$currenthash = $DB->get_record('local_catquiz_question_hashmap', ['questionid' => $questionid]);
$currenthash = $DB->get_record('local_catquiz_qhashmap', ['questionid' => $questionid]);
if (!$currenthash) {
return true; // No hash exists yet.
}
Expand Down
2 changes: 1 addition & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
<INDEX NAME="attemptid" UNIQUE="false" FIELDS="attemptid"/>
</INDEXES>
</TABLE>
<TABLE NAME="local_catquiz_question_hashmap" COMMENT="Maps questions between instances using hashes">
<TABLE NAME="local_catquiz_qhashmap" COMMENT="Maps questions between instances using hashes">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Local question ID"/>
Expand Down
16 changes: 14 additions & 2 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,8 @@ function xmldb_local_catquiz_upgrade($oldversion) {
}

if ($oldversion < 2024110802) {
// Define table local_catquiz_question_hashmap.
$table = new xmldb_table('local_catquiz_question_hashmap');
// Define table local_catquiz_qhashmap.
$table = new xmldb_table('local_catquiz_qhashmap');

// Add fields.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
Expand Down Expand Up @@ -1038,5 +1038,17 @@ function xmldb_local_catquiz_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024110802, 'local', 'catquiz');
}

if ($oldversion < 2024111802) {
// Check if old table exists first.
if ($dbman->table_exists('local_catquiz_question_hashmap')) {
// Rename the table
$dbman->rename_table(
new xmldb_table('local_catquiz_question_hashmap'),
'local_catquiz_qhashmap'
);
}
upgrade_plugin_savepoint(true, 2024111802, 'local', 'catquiz');
}

return true;
}

0 comments on commit b064f60

Please sign in to comment.