Browse files

Merge branch 'MDL-30515_22' of git://github.com/dmonllao/moodle into …

…MOODLE_22_STABLE
  • Loading branch information...
2 parents 09c3821 + 16c5104 commit 6cb03ab5d34861e315e929f94c068008e7d28f39 Sam Hemelryk committed Jul 31, 2012
Showing with 14 additions and 0 deletions.
  1. +14 −0 lib/db/upgrade.php
View
14 lib/db/upgrade.php
@@ -163,6 +163,20 @@ function xmldb_main_upgrade($oldversion) {
$table = new xmldb_table('grade_letters');
$index = new xmldb_index('contextid-lowerboundary-letter', XMLDB_INDEX_UNIQUE, array('contextid', 'lowerboundary', 'letter'));
+ // MDL-30515 Removing duplicate entries before adding the unique index
+ $sql = "SELECT MAX(id) as newestid, contextid, lowerboundary, letter, COUNT('x') count
+ FROM {grade_letters}
+ GROUP BY contextid, lowerboundary, letter
+ HAVING COUNT('x') > 1";
+ $duplicateletters = $DB->get_recordset_sql($sql);
+ foreach ($duplicateletters as $duplicateletter) {
+ // Removing duplicate/s and keeping the latest one
+ $where = 'contextid = ? AND lowerboundary = ? AND letter = ? AND id != ?';
+ $params = array($duplicateletter->contextid, $duplicateletter->lowerboundary, $duplicateletter->letter, $duplicateletter->newestid);
+ $DB->delete_records_select('grade_letters', $where, $params);
+ }
+ $duplicateletters->close();
+
/// Launch add index contextid-lowerboundary-letter
$dbman->add_index($table, $index);

0 comments on commit 6cb03ab

Please sign in to comment.