From cc2a5ff65e4853e46d934928c80464faa714d6ee Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 11 Dec 2011 22:14:02 +0100 Subject: [PATCH] MDL-28180 backup - take rid of dupes in course_completion_aggr_methd --- lib/db/upgrade.php | 28 ++++++++++++++++++++++++++++ version.php | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index b12f6beee9a..2a9d18eebc4 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -6177,6 +6177,34 @@ FROM upgrade_main_savepoint(true, 2011033005.07); } + if ($oldversion < 2011033006.04) { + + upgrade_set_timeout(60*20); // This may take a while + // MDL-28180. Some missing restrictions in certain backup & restore operations + // were causing incorrect duplicates in the course_completion_aggr_methd table. + // This upgrade step takes rid of them. + $sql = 'SELECT course, criteriatype, MIN(id) AS minid + FROM {course_completion_aggr_methd} + GROUP BY course, criteriatype + HAVING COUNT(*) > 1'; + $duprs = $DB->get_recordset_sql($sql); + foreach ($duprs as $duprec) { + // We need to handle NULLs in criteriatype diferently + if (is_null($duprec->criteriatype)) { + $where = 'course = ? AND criteriatype IS NULL AND id > ?'; + $params = array($duprec->course, $duprec->minid); + } else { + $where = 'course = ? AND criteriatype = ? AND id > ?'; + $params = array($duprec->course, $duprec->criteriatype, $duprec->minid); + } + $DB->delete_records_select('course_completion_aggr_methd', $where, $params); + } + $duprs->close(); + + // Main savepoint reached + upgrade_main_savepoint(true, 2011033006.04); + } + return true; } diff --git a/version.php b/version.php index 9c80a87f8f8..340d96a6753 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2011033006.03; // 20110330 = branching date YYYYMMDD - do not modify! +$version = 2011033006.04; // 20110330 = branching date YYYYMMDD - do not modify! // RR = release version - do not change in weeklies // .XX = incremental changes