MDL-69687 Course: remove_course_contents orphaned completion data
[moodle.git] / lib / db / upgrade.php
index 412f94b..1eb9c95 100644 (file)
@@ -2782,5 +2782,16 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2021052500.24);
     }
 
+    if ($oldversion < 2021052500.26) {
+        // Delete orphaned course_modules_completion rows; these were not deleted properly
+        // by remove_course_contents function.
+        $DB->delete_records_subquery('course_modules_completion', 'id', 'id',
+               "SELECT cmc.id
+                  FROM {course_modules_completion} cmc
+             LEFT JOIN {course_modules} cm ON cm.id = cmc.coursemoduleid
+                 WHERE cm.id IS NULL");
+        upgrade_main_savepoint(true, 2021052500.26);
+    }
+
     return true;
 }