From f593111b677162e247d1d237c1cc0348f4a3bbdb Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Fri, 11 Feb 2011 15:41:35 +0100 Subject: [PATCH] MDL-26312 Workshop: data integrity check to detect eventual course_modules corruption Because of a bug in Workshop upgrade code, multiple workshop course_modules can potentially point to a single workshop instance. The chance is pretty low as in most cases, the upgrade failed. But under certain circumstances, workshop could be upgraded with this data integrity issue. We want to detect it now and let the admin know. --- mod/workshop/db/upgrade.php | 42 +++++++++++++++++++++++++++++++++++++ mod/workshop/version.php | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/mod/workshop/db/upgrade.php b/mod/workshop/db/upgrade.php index 13f02d8420089..34883cc0a248b 100644 --- a/mod/workshop/db/upgrade.php +++ b/mod/workshop/db/upgrade.php @@ -264,5 +264,47 @@ function xmldb_workshop_upgrade($oldversion) { upgrade_mod_savepoint(true, 2010111200, 'workshop'); } + /** + * Check the course_module integrity - see MDL-26312 for details + * + * Because of a bug in Workshop upgrade code, multiple workshop course_modules can + * potentially point to a single workshop instance. The chance is pretty low as in most cases, + * the upgrade failed. But under certain circumstances, workshop could be upgraded with + * this data integrity issue. We want to detect it now and let the admin know. + */ + if ($oldversion < 2011021100) { + $sql = "SELECT cm.id, cm.course, cm.instance + FROM {course_modules} cm + WHERE cm.module IN (SELECT id + FROM {modules} + WHERE name = ?)"; + $rs = $DB->get_recordset_sql($sql, array('workshop')); + $map = array(); // returned stdClasses by instance id + foreach ($rs as $cm) { + $map[$cm->instance][$cm->id] = $cm; + } + $rs->close(); + + $problems = array(); + foreach ($map as $instanceid => $cms) { + if (count($cms) > 1) { + $problems[] = 'workshop instance ' . $instanceid . ' referenced by course_modules ' . implode(', ', array_keys($cms)); + } + } + if ($problems) { + echo $OUTPUT->notification('¡Ay, caramba! Data integrity corruption has been detected in your workshop ' . PHP_EOL . + 'module database tables. This might be caused by a bug in workshop upgrade code. ' . PHP_EOL . + 'Please report this issue immediately in workshop module support forum at ' . PHP_EOL . + 'http://moodle.org so that we can help to fix this problem. Please copy and keep ' . PHP_EOL . + 'following information for future reference:'); + foreach ($problems as $problem) { + echo $OUTPUT->notification($problem); + upgrade_log(UPGRADE_LOG_NOTICE, 'mod_workshop', 'course_modules integrity problem', $problem); + } + } + + upgrade_mod_savepoint(true, 2011021100, 'workshop'); + } + return true; } diff --git a/mod/workshop/version.php b/mod/workshop/version.php index 9cc8d8871ef78..11348c1c9f6a1 100644 --- a/mod/workshop/version.php +++ b/mod/workshop/version.php @@ -29,6 +29,6 @@ defined('MOODLE_INTERNAL') || die(); -$module->version = 2010111200; -$module->requires = 2010111002; // Requires this Moodle version +$module->version = 2011021100; +$module->requires = 2011020900; // Requires this Moodle version //$module->cron = 60;