From 5e900da1ce3b5f6e436ea8600c1eda50f254ec69 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Wed, 14 Nov 2007 05:07:33 +0000 Subject: [PATCH] removing duplicate entries in grade_grades prior to adding unique key --- lib/db/upgrade.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 52b4ca76836..891e5019f76 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');