From bb8ad909385a753c7c10b0e466065fcc60861b9c Mon Sep 17 00:00:00 2001 From: Justin Merrill Date: Wed, 3 Nov 2021 12:45:41 -0400 Subject: [PATCH] MDL-72236 forum: index restructure on table forum_read --- mod/forum/db/install.xml | 5 ++-- mod/forum/db/upgrade.php | 51 ++++++++++++++++++++++++++++++++++++++++ mod/forum/version.php | 2 +- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/mod/forum/db/install.xml b/mod/forum/db/install.xml index bbda97aba99..96c9c7067d2 100644 --- a/mod/forum/db/install.xml +++ b/mod/forum/db/install.xml @@ -162,9 +162,10 @@ - - + + + diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index 81ec6a9e29e..c6b87ef583e 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -284,5 +284,56 @@ function xmldb_forum_upgrade($oldversion) { upgrade_mod_savepoint(true, 2021101100, 'forum'); } + if ($oldversion < 2021101101) { + // Remove the userid-forumid index as it gets replaces with forumid-userid. + $table = new xmldb_table('forum_read'); + $index = new xmldb_index('userid-forumid', XMLDB_INDEX_NOTUNIQUE, ['userid', 'forumid']); + + // Conditionally launch drop index userid-forumid. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + + // Remove the userid-discussionid index as it gets replaced with discussionid-userid. + $table = new xmldb_table('forum_read'); + $index = new xmldb_index('userid-discussionid', XMLDB_INDEX_NOTUNIQUE, ['userid', 'discussionid']); + + // Conditionally launch drop index userid-discussionid. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + + // Define index userid (not unique) to be added to forum_read. + $table = new xmldb_table('forum_read'); + $index = new xmldb_index('userid', XMLDB_INDEX_NOTUNIQUE, ['userid']); + + // Conditionally launch add index userid. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Build replacement indexes to replace the two dropped earlier. + // Define index forumid-userid (not unique) to be added to forum_read. + $table = new xmldb_table('forum_read'); + $index = new xmldb_index('forumid-userid', XMLDB_INDEX_NOTUNIQUE, ['forumid', 'userid']); + + // Conditionally launch add index forumid-userid. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Define index discussionid-userid (not unique) to be added to forum_read. + $table = new xmldb_table('forum_read'); + $index = new xmldb_index('discussionid-userid', XMLDB_INDEX_NOTUNIQUE, ['discussionid', 'userid']); + + // Conditionally launch add index discussionid-userid. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Forum savepoint reached. + upgrade_mod_savepoint(true, 2021101101, 'forum'); + } + return true; } diff --git a/mod/forum/version.php b/mod/forum/version.php index f439dc21aa0..962ba41d9a4 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021101100; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2021101101; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2021052500; // Requires this Moodle version. $plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)