From 5134c3bcb48023a6b4cc5e453cb2475e5348cc73 Mon Sep 17 00:00:00 2001 From: David Szkiba Date: Mon, 18 Nov 2024 14:29:04 +0100 Subject: [PATCH] GH-723 Rename too long DB table Changes local_catquiz_question_hashmap to local_catquiz_qhashmap --- classes/remote/hash/question_hasher.php | 12 +-- classes/remote/response/response_handler.php | 8 +- db/install.xml | 4 +- db/upgrade.php | 108 +++++++++++-------- version.php | 2 +- 5 files changed, 77 insertions(+), 57 deletions(-) diff --git a/classes/remote/hash/question_hasher.php b/classes/remote/hash/question_hasher.php index 7a3c70d95..0cd71e17b 100644 --- a/classes/remote/hash/question_hasher.php +++ b/classes/remote/hash/question_hasher.php @@ -20,7 +20,7 @@ /** * Handles question hash generation and verification. - * + * * Idea: Include model name and scale in calculation of hash. * * @package local_catquiz @@ -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; @@ -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; @@ -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. } diff --git a/classes/remote/response/response_handler.php b/classes/remote/response/response_handler.php index bcb37fbaa..3cf013a18 100644 --- a/classes/remote/response/response_handler.php +++ b/classes/remote/response/response_handler.php @@ -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); @@ -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', '*', @@ -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); } /** @@ -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); } } diff --git a/db/install.xml b/db/install.xml index 108f82adf..3d05c04b7 100644 --- a/db/install.xml +++ b/db/install.xml @@ -249,7 +249,7 @@ - +
@@ -266,7 +266,7 @@
- +
diff --git a/db/upgrade.php b/db/upgrade.php index f2ad950e6..7e6f9bf74 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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; } diff --git a/version.php b/version.php index d4abb0631..3f5c70d36 100644 --- a/version.php +++ b/version.php @@ -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 = [