MDL-50385 grades: Adding index to improve history search
[moodle.git] / lib / db / upgrade.php
index 7e6ca5c..c6be33c 100644 (file)
@@ -4916,5 +4916,22 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2016020201.00);
     }
 
+    if ($oldversion < 2016021100.01) {
+        // This could take a long time. Unfortunately, no way to know how long, and no way to do progress, so setting for 1 hour.
+        upgrade_set_timeout(3600);
+
+        // Define index userid-itemid (not unique) to be added to grade_grades_history.
+        $table = new xmldb_table('grade_grades_history');
+        $index = new xmldb_index('userid-itemid-timemodified', XMLDB_INDEX_NOTUNIQUE, array('userid', 'itemid', 'timemodified'));
+
+        // Conditionally launch add index userid-itemid.
+        if (!$dbman->index_exists($table, $index)) {
+            $dbman->add_index($table, $index);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2016021100.01);
+    }
+
     return true;
 }