MDL-61255 core_message: migrate data from the 'message_popup' table

This commit is contained in:
Mark Nelson
2018-04-10 17:26:43 +08:00
parent 6f0731adc5
commit 84f6a716b2
2 changed files with 34 additions and 5 deletions
+14 -4
View File
@@ -132,7 +132,7 @@ class migrate_message_data extends \core\task\adhoc_task {
$messages = $DB->get_recordset_select('message', $select, $params, 'id ASC');
foreach ($messages as $message) {
if ($message->notification) {
$this->migrate_notification($message);
$this->migrate_notification($message, false);
} else {
$this->migrate_message($conversationid, $message);
}
@@ -146,7 +146,7 @@ class migrate_message_data extends \core\task\adhoc_task {
$messages = $DB->get_recordset_select('message_read', $select, $params, 'id ASC');
foreach ($messages as $message) {
if ($message->notification) {
$this->migrate_notification($message);
$this->migrate_notification($message, true);
} else {
$this->migrate_message($conversationid, $message);
}
@@ -161,9 +161,10 @@ class migrate_message_data extends \core\task\adhoc_task {
* Helper function to deal with migrating an individual notification.
*
* @param \stdClass $notification
* @param bool $isread Was the notification read?
* @throws \dml_exception
*/
private function migrate_notification($notification) {
private function migrate_notification($notification, $isread) {
global $DB;
$tabledata = new \stdClass();
@@ -181,7 +182,16 @@ class migrate_message_data extends \core\task\adhoc_task {
$tabledata->timeread = $notification->timeread ?? null;
$tabledata->timecreated = $notification->timecreated;
$DB->insert_record('notifications', $tabledata);
$newid = $DB->insert_record('notifications', $tabledata);
// Check if there is a record to move to the new 'message_popup_notifications' table.
if ($mp = $DB->get_record('message_popup', ['messageid' => $notification->id, 'isread' => (int) $isread])) {
$mpn = new \stdClass();
$mpn->notificationid = $newid;
$DB->insert_record('message_popup_notifications', $mpn);
$DB->delete_records('message_popup', ['id' => $mp->id]);
}
}
/**
@@ -107,6 +107,8 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
$this->assertEquals(2, $DB->count_records('message'));
$this->assertEquals(1, $DB->count_records('message_read'));
$this->assertEquals(6, $DB->count_records('messages'));
$this->assertEquals(0, $DB->count_records('notifications'));
$this->assertEquals(0, $DB->count_records('message_popup_notifications'));
// Get the conversations.
$conversation1 = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
@@ -224,12 +226,18 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
// Remember - we are only converting the notifications related to user 1.
$this->assertEquals(2, $DB->count_records('message'));
$this->assertEquals(1, $DB->count_records('message_read'));
$this->assertEquals(3, $DB->count_records('message_popup'));
$this->assertEquals(6, $DB->count_records('notifications'));
$this->assertEquals(6, $DB->count_records('message_popup_notifications'));
// Confirm what we have in the notifications table is correct.
$notifications = $DB->get_records('notifications', [], 'timecreated ASC');
$popupnotifications = $DB->get_records('message_popup_notifications', [], 'notificationid ASC', 'notificationid');
$i = 1;
foreach ($notifications as $notification) {
// Assert the correct id is stored in the 'message_popup_notifications' table.
$this->assertArrayHasKey($notification->id, $popupnotifications);
$useridfrom = $user1->id;
$useridto = $user2->id;
if ($i > 3) {
@@ -309,6 +317,17 @@ class core_message_migrate_message_data_task_testcase extends advanced_testcase
$tabledata->smallmessage = 'Small message ' . $timecreated;
$tabledata->timecreated = $timecreated;
return $DB->insert_record($table, $tabledata);
$id = $DB->insert_record($table, $tabledata);
// Insert into the legacy 'message_popup' table if it is a notification.
if ($notification) {
$mp = new stdClass();
$mp->messageid = $id;
$mp->isread = (!is_null($timeread)) ? 1 : 0;
$DB->insert_record('message_popup', $mp);
}
return $id;
}
}