Skip to content

Commit

Permalink
MDL-59854 forum: Created unique db key for forum_subscriptions
Browse files Browse the repository at this point in the history
Due to race conditions in the function subscribe_user
it was possible to create duplicate forum subscriptions.
This lead to error messages, when displaying the list of
all subscriptions.
This patch removes all existing duplicate entries and
creates a unique db key to prevent this from happening in
the future.
  • Loading branch information
tobiasreischmann authored and junpataleta committed Oct 12, 2017
1 parent d8e9a23 commit 05f9380
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mod/forum/db/install.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/forum/db" VERSION="20160912" COMMENT="XMLDB file for Moodle mod/forum"
<XMLDB PATH="mod/forum/db" VERSION="20171012" COMMENT="XMLDB file for Moodle mod/forum"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -118,6 +118,7 @@
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="forum" TYPE="foreign" FIELDS="forum" REFTABLE="forum" REFFIELDS="id"/>
<KEY NAME="useridforum" TYPE="unique" FIELDS="userid, forum" COMMENT="Unique key"/>
</KEYS>
<INDEXES>
<INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/>
Expand Down
30 changes: 30 additions & 0 deletions mod/forum/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,35 @@ function xmldb_forum_upgrade($oldversion) {
// Automatically generated Moodle v3.3.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2017092200) {

// Remove duplicate entries from forum_subscriptions.
// Find records with multiple userid/forum combinations and find the highest ID.
// Later we will remove all those entries.
$sql = "
SELECT MIN(id) as minid, userid, forum
FROM {forum_subscriptions}
GROUP BY userid, forum
HAVING COUNT(id) > 1";

if ($duplicatedrows = $DB->get_recordset_sql($sql)) {
foreach ($duplicatedrows as $row) {
$DB->delete_records_select('forum_subscriptions',
'userid = :userid AND forum = :forum AND id <> :minid', (array)$row);
}
}
$duplicatedrows->close();

// Define key useridforum (primary) to be added to forum_subscriptions.
$table = new xmldb_table('forum_subscriptions');
$key = new xmldb_key('useridforum', XMLDB_KEY_UNIQUE, array('userid', 'forum'));

// Launch add key useridforum.
$dbman->add_key($table, $key);

// Forum savepoint reached.
upgrade_mod_savepoint(true, 2017092200, 'forum');
}

return true;
}
2 changes: 1 addition & 1 deletion mod/forum/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2017051500; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2017092200; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2017050500; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)

0 comments on commit 05f9380

Please sign in to comment.