diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index 64a70dc..bf121a2 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -5015,7 +5015,8 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
                         break;
                     case CONTEXT_COURSECAT :
                     case CONTEXT_SYSTEM :
-                        $context = get_system_context();
+                        $system_context = get_system_context();
+                        $context = get_context_instance(CONTEXT_COURSE, $system_context->id);
                         break;
                     default :
                         continue;
