MDL-68683 core: fix DML exception on upgrade step
authorSimey Lameze <simey@moodle.com>
Thu, 14 May 2020 05:02:29 +0000 (13:02 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 20 Aug 2020 01:02:50 +0000 (09:02 +0800)
lib/db/upgrade.php

index 32298db..96867f6 100644 (file)
@@ -2194,8 +2194,11 @@ function xmldb_main_upgrade($oldversion) {
             $DB->delete_records('competency_userevidencecomp', ['userevidenceid' => $userevidence->id]);
             $DB->delete_records('competency_userevidence', ['id' => $userevidence->id]);
 
             $DB->delete_records('competency_userevidencecomp', ['userevidenceid' => $userevidence->id]);
             $DB->delete_records('competency_userevidence', ['id' => $userevidence->id]);
 
-            $context = context_user::instance($userevidence->userid);
-            $fs->delete_area_files($context->id, 'core_competency', 'userevidence', $userevidence->id);
+            if ($record = $DB->get_record('context', ['contextlevel' => CONTEXT_USER, 'instanceid' => $userevidence->userid],
+                    '*', IGNORE_MISSING)) {
+                // Delete all orphaned user evidences files.
+                $fs->delete_area_files($record->id, 'core_competency', 'userevidence', $userevidence->userid);
+            }
         }
 
         $sql = "SELECT cp.id
         }
 
         $sql = "SELECT cp.id