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

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
leolivier

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

Post 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;
}
?>
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

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

Post 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.
leolivier

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

Post 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?
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

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

Post 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
Last edited by Jos on Sun Apr 11, 2010 3:57 pm, edited 1 time in total.
Post Reply

Return to “Developers Discussion”