In the file /mod/quiz/db/upgrade.php the function xmldb_quiz_upgrade() sets the debug to false before calling assignment_update_grades() to avoid excess debug information that might halt the client. However, this has no effect as the $db is not in the global definition.
Suggested fix, change
global $CFG, $THEME;
global $CFG, $THEME, $db;
in the beginning of the function.