From 33b4331eaf3ca04e2e869e6e627c0849d8515779 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 1 Jul 2013 11:18:16 +0100 Subject: [PATCH] MDL-40370 qtype_shortanswer: unique index upgrade problem. Eliminate duplicate rows before we try to add a unique index. We arbitrarily keep the duplicate row with the lowest id. --- question/type/shortanswer/db/upgrade.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/question/type/shortanswer/db/upgrade.php b/question/type/shortanswer/db/upgrade.php index 745d8af7d55..99dbc153f70 100644 --- a/question/type/shortanswer/db/upgrade.php +++ b/question/type/shortanswer/db/upgrade.php @@ -39,6 +39,25 @@ function xmldb_qtype_shortanswer_upgrade($oldversion) { // Moodle v2.4.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2013011799) { + // Find duplicate rows before they break the 2013011803 step below. + $problemids = $DB->get_recordset_sql(" + SELECT question, MIN(id) as recordidtokeep + FROM {question_shortanswer} + GROUP BY question + HAVING COUNT(1) > 1 + "); + foreach ($problemids as $problem) { + $DB->delete_records_select('question_shortanswer', + 'question = ? AND id > ?', + array($problem->question, $problem->recordidtokeep)); + } + $problemids->close(); + + // Shortanswer savepoint reached. + upgrade_plugin_savepoint(true, 2013011799, 'qtype', 'shortanswer'); + } + if ($oldversion < 2013011800) { // Define field answers to be dropped from question_shortanswer.