Skip to content

Commit

Permalink
MDL-49247 question restore: avoid unique key errors from old bad data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timhunt committed Feb 19, 2015
1 parent 8235b61 commit 87e581f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ public function process_matchoptions($data) {

// Adjust some columns.
$data->questionid = $newquestionid;
$newitemid = $DB->insert_record('qtype_match_options', $data);
$this->set_mapping('qtype_match_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_match_options', array('questionid' => $data->questionid))) {
$newitemid = $DB->insert_record('qtype_match_options', $data);
$this->set_mapping('qtype_match_options', $oldid, $newitemid);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ public function process_multichoice($data) {
// If the question has been created by restore, we need to create its
// qtype_multichoice_options too.
if ($questioncreated) {
// Adjust some columns.
$data->questionid = $newquestionid;
// Insert record.
$newitemid = $DB->insert_record('qtype_multichoice_options', $data);
// Create mapping (needed for decoding links).
$this->set_mapping('qtype_multichoice_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_multichoice_options', array('questionid' => $data->questionid))) {
$newitemid = $DB->insert_record('qtype_multichoice_options', $data);
$this->set_mapping('qtype_multichoice_options', $oldid, $newitemid);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ public function process_randomsamatch($data) {
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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ public function process_shortanswer($data) {
// qtype_shortanswer_options too, if they are defined (the gui should ensure this).
if ($questioncreated) {
$data->questionid = $newquestionid;
$newitemid = $DB->insert_record('qtype_shortanswer_options', $data);
$this->set_mapping('qtype_shortanswer_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_shortanswer_options', array('questionid' => $data->questionid))) {
$newitemid = $DB->insert_record('qtype_shortanswer_options', $data);
$this->set_mapping('qtype_shortanswer_options', $oldid, $newitemid);
}
}
}
}

0 comments on commit 87e581f

Please sign in to comment.