MDL-41093 qtype match: unique index upgrade problem.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Thu, 8 Aug 2013 17:30:33 +0000 (18:30 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Thu, 15 Aug 2013 11:20:07 +0000 (12:20 +0100)
Eliminate duplicate rows before we try to add a unique index. We
arbitrarily keep the duplicate row with the lowest id.

question/type/match/db/upgrade.php

index ff85c2a..42bfd04 100644 (file)
@@ -44,6 +44,25 @@ function xmldb_qtype_match_upgrade($oldversion) {
     // Moodle v2.4.0 release upgrade line.
     // Put any upgrade step following this.
 
+    if ($oldversion < 2013012099) {
+        // Find duplicate rows before they break the 2013012103 step below.
+        $problemids = $DB->get_recordset_sql("
+                SELECT question, MIN(id) AS recordidtokeep
+                  FROM {question_match}
+              GROUP BY question
+                HAVING COUNT(1) > 1
+                ");
+        foreach ($problemids as $problem) {
+            $DB->delete_records_select('question_match',
+                    'question = ? AND id > ?',
+                    array($problem->question, $problem->recordidtokeep));
+        }
+        $problemids->close();
+
+        // Shortanswer savepoint reached.
+        upgrade_plugin_savepoint(true, 2013012099, 'qtype', 'match');
+    }
+
     if ($oldversion < 2013012100) {
 
         // Define table question_match to be renamed to qtype_match_options.