Page 1 of 1

My module upgrade does not work when upgrading from the module manager

Posted: Mon Apr 05, 2010 11:13 am
by leolivier
Hi,
I wrote my first module for cmsms recently (HostedVideoAlbums).
I tried to release the 1.1 version a few days ago and looked at other modules to find out how to write the upgrade code (see below).

When I upgrade to this new version manually (by unzipping the module or by installing with the xml file from the "Module" page) then it works perfectly, I can find my new elements in the db tables.
But when I upgrade through the full automatic upgrade process of the "Module Manager" extension, the module version is correctly installed (new files are ok) but the upgrade code is not executed (there is no log in the journal and the new lines are not inserted in the tables).
Can anybody tell me why?
Can also anybody explain to me how the upgrade code is called? I found that some modules had an Upgrade method and some others had a "method.ugrade.php" file. What should I really do?

My upgrade code is as follows:
I ended up in writing an Upgrade method in my method :

Code: Select all

function Upgrade($oldversion, $newversion)
{
	$current_version = $oldversion;
	global $gCms;
	require "method.upgrade.php";
}
and the following code in method.upgrade.php:

Code: Select all

<?php
if (!isset($gCms)) exit;
$db =& $this->GetDb();
switch($current_version) {
	case "1.0":
		// insert site for local divx stuff
		$tablename = cms_db_prefix()."module_HVA_fieldoptions";
		$query = "INSERT INTO ".$tablename." SET id=?, field=?, name=?, item_order=?";
		$siteid = $db->GenID($tablename."_seq");
		$db->Execute($query,array($siteid,"video_site","local-divx",$siteid));
		// update default template
		$fnbase=dirname(__FILE__).DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR;
		$fn = $fnbase.'default_embed_video.tpl';
		if( file_exists( $fn ) ) {
			$template = @file_get_contents($fn);
			$this->SetTemplate("EmbedVideo",$template,$this->GetName());
			$this->SetPreference("embedtemplate","EmbedVideo");
		}
  		$current_version = "1.1";
		// put mention into the admin log
		$this->Audit( 0, $this->Lang('friendlyname'), $this->Lang('installed',$current_version));
	case "1.1":
	default:
	     break;
}
?>

Re: My module upgrade does not work when upgrading from the module manager

Posted: Sat Apr 10, 2010 5:50 pm
by Jos
Take a look at how this is done with the Skeleton module
http://viewsvn.cmsmadesimple.org/listin ... 3be466fa8a

It has only a method.upgrade.php with this code:

Code: Select all

<?php
#-------------------------------------------------------------------------
# Module: Skeleton - a pedantic "starting point" module
# Version: 1.5, SjG
# Method: Upgrade
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2008 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
# The module's homepage is: http://dev.cmsmadesimple.org/projects/skeleton/
#
#-------------------------------------------------------------------------

/**
 * For separated methods, you'll always want to start with the following
 * line which check to make sure that method was called from the module
 * API, and that everything's safe to continue:
*/ 
if (!isset($gCms)) exit;


/**
 * After this, the code is identical to the code that would otherwise be
 * wrapped in the Upgrade() method in the module body.
 */

$current_version = $oldversion;
switch($current_version)
{
  // we are now 1.0 and want to upgrade to latest
 case "1.0":
   //do magic
 case "1.1":
   //and this is here for the next version
 case "1.2":
 case "1.3":
 case "1.4":
 case "1.5":
 case "1.6":
 case "1.7":
}

// put mention into the admin log
$this->Audit( 0, 
              $this->Lang('friendlyname'), 
              $this->Lang('upgraded', $this->GetVersion()));

//note: module api handles sending generic event of module upgraded here
?>
So no need of a function Upgrade. Also note that if you introduce new methods in a new version, they are not available for the upgrade yet.

Re: My module upgrade does not work when upgrading from the module manager

Posted: Sun Apr 11, 2010 10:08 am
by leolivier
Thanks for your reply Jos.
Your last remark is the key I think.
As the Upgrade method did not already exist, it was not executed, so the

Code: Select all

$current_version = $oldversion;
neither and so the switch were not entered in method.upgrade.php...

Anyway, is there a documentation somewhere about this (apart from the skeleton module)?
Where, for instance, can I learn something about the Upgrade method and it's use?

Re: My module upgrade does not work when upgrading from the module manager

Posted: Sun Apr 11, 2010 3:55 pm
by Jos
Here is some short documentation: http://wiki.cmsmadesimple.org/index.php ... our_module

You could also get example code from existing modules like News