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 fff8a67 commit 5134c3b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 57 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
8 changes: 4 additions & 4 deletions classes/remote/response/response_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function store_response($questionhash, $fraction, $remoteuserid, $
$record->timecreated = time();

try {
$DB->insert_record('local_catquiz_remote_responses', $record);
$DB->insert_record('local_catquiz_rresponses', $record);
return true;
} catch (\Exception $e) {
debugging('Error storing remote response: ' . $e->getMessage(), DEBUG_DEVELOPER);
Expand All @@ -63,7 +63,7 @@ public static function store_response($questionhash, $fraction, $remoteuserid, $
public static function process_responses($batchsize = 100) {
global $DB;

$responses = $DB->get_records('local_catquiz_remote_responses',
$responses = $DB->get_records('local_catquiz_rresponses',
['timeprocessed' => null],
'timecreated ASC',
'*',
Expand Down Expand Up @@ -103,7 +103,7 @@ private static function mark_response_processed($response) {
global $DB;
$response->timeprocessed = time();
$response->processinginfo = json_encode(['status' => 'success']);
$DB->update_record('local_catquiz_remote_responses', $response);
$DB->update_record('local_catquiz_rresponses', $response);
}

/**
Expand All @@ -116,6 +116,6 @@ private static function mark_response_error($response, $error) {
global $DB;
$response->timeprocessed = time();
$response->processinginfo = json_encode(['status' => 'error', 'message' => $error]);
$DB->update_record('local_catquiz_remote_responses', $response);
$DB->update_record('local_catquiz_rresponses', $response);
}
}
4 changes: 2 additions & 2 deletions 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 All @@ -266,7 +266,7 @@
<INDEX NAME="questionhash" UNIQUE="false" FIELDS="questionhash"/>
</INDEXES>
</TABLE>
<TABLE NAME="local_catquiz_remote_responses" COMMENT="Stores responses from remote instances">
<TABLE NAME="local_catquiz_rresponses" COMMENT="Stores responses from remote instances">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="questionhash" TYPE="char" LENGTH="64" NOTNULL="true" SEQUENCE="false" COMMENT="Matches questionhash in question_hashmap"/>
Expand Down
108 changes: 64 additions & 44 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,57 +986,77 @@ function xmldb_local_catquiz_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024092700, 'local', 'catquiz');
}

// This is a bit unconventional. The table already exists with old, long
// names on a moodle instance that supports longer table names but can't be
// created on a different instance that has stricter naming rules.
if ($oldversion < 2024110802) {
// Define table local_catquiz_question_hashmap.
$table = new xmldb_table('local_catquiz_question_hashmap');

// Add fields.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('questionhash', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
$table->add_field('hashdata', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');

// Add keys.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('questionid', XMLDB_KEY_FOREIGN, ['questionid'], 'question', ['id']);

// Add indexes.
$table->add_index('questionhash', XMLDB_INDEX_NOTUNIQUE, ['questionhash']);

// Create the table.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
// 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'
);
} else {
// 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);
$table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('questionhash', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
$table->add_field('hashdata', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');

// Add keys.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('questionid', XMLDB_KEY_FOREIGN, ['questionid'], 'question', ['id']);

// Add indexes.
$table->add_index('questionhash', XMLDB_INDEX_NOTUNIQUE, ['questionhash']);

// Create the table.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
}

// Define table local_catquiz_remote_responses.
$table = new xmldb_table('local_catquiz_remote_responses');

// Add fields.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('questionhash', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
$table->add_field('remoteuserid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('response', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('sourceurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timeprocessed', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('processinginfo', XMLDB_TYPE_TEXT, null, null, null, null, null);

// Add keys.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Add indexes.
$table->add_index('questionhash_sourceurl', XMLDB_INDEX_NOTUNIQUE, ['questionhash', 'sourceurl']);
$table->add_index('timeprocessed', XMLDB_INDEX_NOTUNIQUE, ['timeprocessed']);

// Create the table.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
if ($dbman->table_exists('local_catquiz_remote_responses')) {
$dbman->rename_table(
new xmldb_table('local_catquiz_remote_responses'),
'local_catquiz_rresponses'
);
} else {
// Define table local_catquiz_rresponses.
$table = new xmldb_table('local_catquiz_rresponses');

// Add fields.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('questionhash', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
$table->add_field('remoteuserid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('response', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('sourceurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timeprocessed', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('processinginfo', XMLDB_TYPE_TEXT, null, null, null, null, null);

// Add keys.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Add indexes.
$table->add_index('questionhash_sourceurl', XMLDB_INDEX_NOTUNIQUE, ['questionhash', 'sourceurl']);
$table->add_index('timeprocessed', XMLDB_INDEX_NOTUNIQUE, ['timeprocessed']);

// Create the table.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
}

upgrade_plugin_savepoint(true, 2024110802, 'local', 'catquiz');
}


return true;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$plugin->component = 'local_catquiz';
$plugin->release = '1.1.1';
$plugin->version = 2024111801;
$plugin->version = 2024111802;
$plugin->requires = 2022041900;
$plugin->maturity = MATURITY_STABLE;
$plugin->dependencies = [
Expand Down

0 comments on commit 5134c3b

Please sign in to comment.