From c47c19634ed39d15065575dfd421faaf75596eba Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 25 Feb 2019 08:53:59 +0800 Subject: [PATCH] MDL-64568 core: upgrade step to remove orphaned group conversations --- lib/db/upgrade.php | 36 ++++++++++++++++++++++++++++++++++++ version.php | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index fdb23f8bb16..57ba1857b6c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2952,5 +2952,41 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2018120302.04); } + if ($oldversion < 2018120303.01) { + // Remove any conversations and their members associated with non-existent groups. + $sql = "SELECT mc.id + FROM {message_conversations} mc + LEFT JOIN {groups} g + ON mc.itemid = g.id + WHERE mc.component = :component + AND mc.itemtype = :itemtype + AND g.id is NULL"; + $conversations = $DB->get_records_sql($sql, ['component' => 'core_group', 'itemtype' => 'groups']); + + if ($conversations) { + $conversationids = array_keys($conversations); + + $DB->delete_records_list('message_conversations', 'id', $conversationids); + $DB->delete_records_list('message_conversation_members', 'conversationid', $conversationids); + + // Now, go through each conversation and delete any messages and related message actions. + foreach ($conversationids as $conversationid) { + if ($messages = $DB->get_records('messages', ['conversationid' => $conversationid])) { + $messageids = array_keys($messages); + + // Delete the actions. + list($insql, $inparams) = $DB->get_in_or_equal($messageids); + $DB->delete_records_select('message_user_actions', "messageid $insql", $inparams); + + // Delete the messages. + $DB->delete_records('messages', ['conversationid' => $conversationid]); + } + } + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2018120303.01); + } + return true; } diff --git a/version.php b/version.php index bd6ebf6e9b0..8b5d519959b 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018120303.00; // 20181203 = branching date YYYYMMDD - do not modify! +$version = 2018120303.01; // 20181203 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches. // .XX = incremental changes.