From 8def548b8df6403497d6e6f44bc65fb207a42c41 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 19 Feb 2015 21:46:56 +0000 Subject: [PATCH] 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. --- .../moodle2/restore_qtype_match_plugin.class.php | 9 +++++++-- .../restore_qtype_multichoice_plugin.class.php | 12 +++++++----- .../restore_qtype_randomsamatch_plugin.class.php | 12 +++++++----- .../restore_qtype_shortanswer_plugin.class.php | 9 +++++++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php b/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php index e40bea47d3c..f9dc84bf59a 100644 --- a/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php +++ b/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php @@ -104,8 +104,13 @@ class restore_qtype_match_plugin extends restore_qtype_plugin { // 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); + } } } diff --git a/question/type/multichoice/backup/moodle2/restore_qtype_multichoice_plugin.class.php b/question/type/multichoice/backup/moodle2/restore_qtype_multichoice_plugin.class.php index cceada35aa1..7ef6ab68ec7 100644 --- a/question/type/multichoice/backup/moodle2/restore_qtype_multichoice_plugin.class.php +++ b/question/type/multichoice/backup/moodle2/restore_qtype_multichoice_plugin.class.php @@ -70,12 +70,14 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin { // 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); + } } } diff --git a/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php b/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php index a86e0e3b528..b561a3acca0 100644 --- a/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php +++ b/question/type/randomsamatch/backup/moodle2/restore_qtype_randomsamatch_plugin.class.php @@ -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); + } } } diff --git a/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php b/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php index c74178b3133..1ec39417f82 100644 --- a/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php +++ b/question/type/shortanswer/backup/moodle2/restore_qtype_shortanswer_plugin.class.php @@ -71,8 +71,13 @@ class restore_qtype_shortanswer_plugin extends restore_qtype_plugin { // 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); + } } } }