From fe9864f48884dd73646ff03bc29334fd215e9aa4 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Wed, 16 Nov 2016 13:20:18 +0800 Subject: [PATCH] MDL-56972 backup: fix question category stamp duplicates during restore To kerb future stamp duplication, MDL-54864 added a unique index to the question_categories table (stamp, contextid) and added an upgrade step to fix existing duplicate stamps. It missed the case where a course with duplicate stamps in the same context is directly restored into moodle 3.1, causing index clashes. This issue provides a fix for that. --- backup/moodle2/restore_stepslib.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index d303ba1009d..e07b50f81f3 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -4331,6 +4331,14 @@ class restore_create_categories_and_questions extends restore_structure_step { } $data->contextid = $mapping->parentitemid; + // Before 3.1, the 'stamp' field could be erroneously duplicated. + // From 3.1 onwards, there's a unique index of (contextid, stamp). + // If we encounter a duplicate in an old restore file, just generate a new stamp. + // This is the same as what happens during an upgrade to 3.1+ anyway. + if ($DB->record_exists('question_categories', ['stamp' => $data->stamp, 'contextid' => $data->contextid])) { + $data->stamp = make_unique_id_code(); + } + // Let's create the question_category and save mapping $newitemid = $DB->insert_record('question_categories', $data); $this->set_mapping('question_category', $oldid, $newitemid);