MDL-26219 fix borked upgrade logging and add new fileversion column
authorPetr Skoda <commits@skodak.org>
Thu, 3 Feb 2011 09:07:13 +0000 (10:07 +0100)
committerPetr Skoda <commits@skodak.org>
Mon, 7 Feb 2011 19:00:20 +0000 (20:00 +0100)
lib/db/install.xml
lib/db/upgrade.php
lib/upgradelib.php
version.php

index 2088df6..e7676bb 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20110125" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20110206" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
@@ -50,8 +50,9 @@
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="type"/>
         <FIELD NAME="type" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" COMMENT="type: 0==info, 1==notice, 2==error" PREVIOUS="id" NEXT="plugin"/>
         <FIELD NAME="plugin" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" PREVIOUS="type" NEXT="version"/>
-        <FIELD NAME="version" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="plugin or main version if known" PREVIOUS="plugin" NEXT="info"/>
-        <FIELD NAME="info" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="version" NEXT="details"/>
+        <FIELD NAME="version" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="plugin or main version if known" PREVIOUS="plugin" NEXT="targetversion"/>
+        <FIELD NAME="targetversion" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" COMMENT="version of plugin or core specified in version.php at the time of upgrade loggging" PREVIOUS="version" NEXT="info"/>
+        <FIELD NAME="info" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="targetversion" NEXT="details"/>
         <FIELD NAME="details" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" PREVIOUS="info" NEXT="backtrace"/>
         <FIELD NAME="backtrace" TYPE="text" LENGTH="small" NOTNULL="false" SEQUENCE="false" PREVIOUS="details" NEXT="userid"/>
         <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="backtrace" NEXT="timemodified"/>
index 1be183d..0e1aad1 100644 (file)
@@ -74,6 +74,7 @@ function xmldb_main_upgrade($oldversion) {
         $table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
         $table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null);
         $table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null);
+        $table->add_field('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null);
         $table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
         $table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
         $table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
@@ -6005,6 +6006,22 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
         upgrade_main_savepoint(true, 2011012501);
     }
 
+    if ($oldversion < 2011020200.01) {
+
+        // Define field targetversion to be added to upgrade_log
+        $table = new xmldb_table('upgrade_log');
+        $field = new xmldb_field('targetversion', XMLDB_TYPE_CHAR, '100', null, null, null, null, 'version');
+
+        // Conditionally launch add field targetversion
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
+        // Main savepoint reached
+        upgrade_main_savepoint(true, 2011020200.01);
+    }
+
+
     return true;
 }
 
index 81de9b7..53c8727 100644 (file)
@@ -987,11 +987,8 @@ function upgrade_handle_exception($ex, $plugin = null) {
 /**
  * Adds log entry into upgrade_log table
  *
- * @global object
- * @global object
- * @global object
  * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
- * @param string $plugin plugin or null if main
+ * @param string $plugin frankenstyle component name
  * @param string $info short description text of log entry
  * @param string $details long problem description
  * @param string $backtrace string used for errors only
@@ -1000,54 +997,80 @@ function upgrade_handle_exception($ex, $plugin = null) {
 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
     global $DB, $USER, $CFG;
 
-    $plugin = ($plugin==='moodle') ? null : $plugin;
+    if (empty($plugin)) {
+        $plugin = 'core';
+    }
+
+    list($plugintype, $pluginname) = normalize_component($plugin);
+    $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
 
     $backtrace = format_backtrace($backtrace, true);
 
-    $version = null;
+    $currentversion = null;
+    $targetversion  = null;
 
     //first try to find out current version number
-    if (empty($plugin) or $plugin === 'moodle') {
+    if ($plugintype === 'core') {
         //main
-        $version = $CFG->version;
+        $currentversion = $CFG->version;
 
-    } else if ($plugin === 'local') {
-        //customisation
-        $version = $CFG->local_version;
+        $version = null;
+        include("$CFG->dirroot/version.php");
+        $targetversion = $version;
 
-    } else if (strpos($plugin, 'mod/') === 0) {
+    } else if ($plugintype === 'mod') {
         try {
-            $modname = substr($plugin, strlen('mod/'));
-            $version = $DB->get_field('modules', 'version', array('name'=>$modname));
-            $version = ($version === false) ? null : $version;
+            $currentversion = $DB->get_field('modules', 'version', array('name'=>$pluginname));
+            $currentversion = ($currentversion === false) ? null : $currentversion;
         } catch (Exception $ignored) {
         }
+        $cd = get_component_directory($component);
+        if (file_exists("$cd/version.php")) {
+            $module = new stdClass();
+            $module->version = null;
+            include("$cd/version.php");
+            $targetversion = $module->version;
+        }
 
-    } else if (strpos($plugin, 'block/') === 0) {
+    } else if ($plugintype === 'block') {
         try {
-            $blockname = substr($plugin, strlen('block/'));
-            if ($block = $DB->get_record('block', array('name'=>$blockname))) {
-                $version = $block->version;
+            if ($block = $DB->get_record('block', array('name'=>$pluginname))) {
+                $currentversion = $block->version;
             }
         } catch (Exception $ignored) {
         }
+        $cd = get_component_directory($component);
+        if (file_exists("$cd/version.php")) {
+            $plugin = new stdClass();
+            $plugin->version = null;
+            include("$cd/version.php");
+            $targetversion = $plugin->version;
+        }
 
     } else {
-        $pluginversion = get_config(str_replace('/', '_', $plugin), 'version');
+        $pluginversion = get_config($component, 'version');
         if (!empty($pluginversion)) {
-            $version = $pluginversion;
+            $currentversion = $pluginversion;
+        }
+        $cd = get_component_directory($component);
+        if (file_exists("$cd/version.php")) {
+            $plugin = new stdClass();
+            $plugin->version = null;
+            include("$cd/version.php");
+            $targetversion = $plugin->version;
         }
     }
 
     $log = new stdClass();
-    $log->type         = $type;
-    $log->plugin       = $plugin;
-    $log->version      = $version;
-    $log->info         = $info;
-    $log->details      = $details;
-    $log->backtrace    = $backtrace;
-    $log->userid       = $USER->id;
-    $log->timemodified = time();
+    $log->type          = $type;
+    $log->plugin        = $component;
+    $log->version       = $currentversion;
+    $log->targetversion = $targetversion;
+    $log->info          = $info;
+    $log->details       = $details;
+    $log->backtrace     = $backtrace;
+    $log->userid        = $USER->id;
+    $log->timemodified  = time();
     try {
         $DB->insert_record('upgrade_log', $log);
     } catch (Exception $ignored) {
index d893cb3..87f509e 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version = 2011020200;  // YYYYMMDD   = date of the last version bump
+$version = 2011020200.01;  // YYYYMMDD   = date of the last version bump
                         //         XX = daily increments
 
 $release = '2.0.1+ (Build: 20110204)';  // Human-friendly version name