MDL-49247 question restore: avoid unique key errors from old bad data

Several tables have had unique keys added to enforce a constraint that
should always have been there. It is possible for old sites to have
data that violate the constraints, and sometimes people want to backup
data from those old sites, and restore them into a new Moodle.
Therefore, we need to guard agains the unique key violation errors.
This commit is contained in:
Tim Hunt 2015-02-19 21:46:56 +00:00
parent 95751e81ac
commit 8def548b8d
4 changed files with 28 additions and 14 deletions

View file

@ -86,12 +86,14 @@ class restore_qtype_randomsamatch_plugin extends restore_qtype_plugin {
if (!isset($data->shownumcorrect)) {
$data->shownumcorrect = 0;
}
// Adjust some columns.
$data->questionid = $newquestionid;
// Insert record.
$newitemid = $DB->insert_record('qtype_randomsamatch_options', $data);
// Create mapping.
$this->set_mapping('qtype_randomsamatch_options', $oldid, $newitemid);
// It is possible for old backup files to contain unique key violations.
// We need to check to avoid that.
if (!$DB->record_exists('qtype_randomsamatch_options', array('questionid' => $data->questionid))) {
$newitemid = $DB->insert_record('qtype_randomsamatch_options', $data);
$this->set_mapping('qtype_randomsamatch_options', $oldid, $newitemid);
}
}
}