From: toyomoyo Date: Wed, 14 Nov 2007 05:07:33 +0000 (+0000) Subject: removing duplicate entries in grade_grades prior to adding unique key X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=5e900da1ce3b5f6e436ea8600c1eda50f254ec69;p=moodle.git removing duplicate entries in grade_grades prior to adding unique key --- diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 52b4ca7683..891e5019f7 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2587,6 +2587,32 @@ function xmldb_main_upgrade($oldversion=0) { if ($result && $oldversion < 2007101502) { + /// try to remove duplicate entries + + $SQL = "SELECT id, userid, itemid + FROM {$CFG->prefix}grade_grades + GROUP BY userid, itemid + HAVING COUNT( * ) >1"; + // duplicates found + if ($dups = get_records_sql($SQL)) { + // for each set of userid, itemid + foreach ($dups as $dup) { + if ($thisdups = get_records_sql("SELECT id,id FROM {$CFG->prefix}grade_grades + WHERE itemid = $dup->itemid AND userid = $dup->userid + ORDER BY timemodified DESC")) { + + $processed = 0; // keep the first one + foreach ($thisdups as $thisdup) { + if ($processed) { + // remove the duplicates + delete_records('grade_grades', 'id', $thisdup->id); + } + $processed++; + } + } + } + } + /// Define key userid-itemid (unique) to be added to grade_grades $table = new XMLDBTable('grade_grades'); $key = new XMLDBKey('userid-itemid');