Jump to content
WHMUp

Update module version

Recommended Posts

Hello,

I've a problem to update the module version even if the real code works (eg update tables, create table). Everything works fine except that the version number still says 1.0

// 	########################  MODULE CONFIGURATION  ########################
	function my_mod_config() 
	{
		$configarray = array(
			"name" => "My Module",
			"description" => "Description goes here....",
			"version" => "1.0",
			"author" => "ChrisTERiS",
			"language" => "english",
			"fields" => array(
		));
		return $configarray;
	}
// 	##########################  ACTIVATE MODULE  ###########################
	function my_mod_activate()
	{
		// Code to create Tables. Works !!!			
	}
// 	#########################  DEACTIVATE MODULE  ##########################
	function my_mod_deactivate()
	{
		// Code to remove Tables. Works !!!
	}
// 	###########################  UPGRADE MODULE  ##########################	
	function my_mod_upgrade($vars) 
	{
		$currentlyInstalledVersion = $vars['version'];

		if ($currentlyInstalledVersion < 1.3)
		{
			// Code to upgrade table to version 1.3. Works !!!
		}	

		if ($currentlyInstalledVersion < 2.0)
		{
			// Code to upgrade table to version 2.0. Works !!!
		}
		return;	
	}

Any advice is highly appreciated.

Have a Happy New Year,

Chris

Share this post


Link to post
Share on other sites
Posted (edited)

Happy New Year.

You need to change the version number in the `$configarray`.

"version" => "1.0",

 

Edited by pRieStaKos

Share this post


Link to post
Share on other sites
1 hour ago, pRieStaKos said:

Happy New Year.

You need to change the version number in the `$configarray`.


"version" => "1.0",

 

I did this, and it's true that it worked when upgrading an active installation. But the problem is that in a fresh installation, it executes only the part of code in function my_mod_upgrade and ignores the code of the main function my_mod_activate.

Have tried it twice, I'll give one more try to see will acts the same.

 

Share this post


Link to post
Share on other sites
Posted (edited)

I could be wrong (I'm not using standard module functions in my projects) but your my_mod_activate() function should be based on the latest version of your module. Let's say you released the following versions:

  • 1.0.0
  • 1.0.1 - You changed this: UPDATE sometable SET animal = 'Dog'
  • 1.0.2 - You changed this: UPDATE sometable SET animal = 'Cat'

In my_mod_upgrade() you have the following:

function my_mod_upgrade($vars) 
{
	$currentlyInstalledVersion = $vars['version'];

	if ($currentlyInstalledVersion < 1.0.1)
	{
		// UPDATE sometable SET animal = 'Dog'
	}	

	if ($currentlyInstalledVersion < 1.0.2)
	{
		// UPDATE sometable SET animal = 'Cat'
	}
	return;	
}

my_mod_activate() need to be like this:

function my_mod_activate()
{
	// CREATE sometable (animal VARCHAR(30) NOT NULL)
	// INSERT INTO sometable (animal) VALUS ('Cat')
}

As you can the the activation uses "Cat" (the value of the latest version). That said, your my_mod_config() needs to go with version 1.0.3. In other words fresh installation do not trigger upgrade function.

function my_mod_config() 
{
	$configarray = array(
		"name" => "My Module",
		"description" => "Description goes here....",
		"version" => "1.0.3",
		"author" => "ChrisTERiS",
		"language" => "english",
		"fields" => array(
	));
	return $configarray;
}

Edit: please, ignore my versioning (the 1.0.0). I suspect that WHMCS supports only 1.0 format.

Edited by Kian

Share this post


Link to post
Share on other sites

@Kian Interesting solution, thank you. I'll try it tomorrow morning as is very late now.

For the moment I was able to solve (somehow) this issue, by setting version 2.0 in config and if ($currentlyInstalledVersion < 2.1) in upgrade function. With if ($currentlyInstalledVersion < 2.0) upgrade does not works.

Share this post


Link to post
Share on other sites
Posted (edited)

That's so weird. Even a version update with a query does not works. Eg the code below works as for the database table modifications, but the query to update tbladdonmodules does not works without to give any error.

 

if ($currentlyInstalledVersion < 2.0)
{
	// Articles
	mysqli_query($db,"ALTER TABLE `mod_legalterms_articles` ADD `language` VARCHAR(100) DEFAULT 'english' NOT NULL");
	// Settings
	mysqli_query($db,"ALTER TABLE `mod_legalterms_settings` ADD `licensekey` VARCHAR(100) DEFAULT '' NOT NULL");

	// Update Version
	try {
		Capsule::table('tbladdonmodules')
				->where('module', '=', 'legalterms')
				->where('setting', '=', 'version')
				->update(
					[
						'value' => '2.0'
					]
				);
	} catch (\Exception $e) {
		echo "I couldn't update Version. {$e->getMessage()}";
	}
}

 

Edited by WHMUp
Typo

Share this post


Link to post
Share on other sites

Finally, the old good way, works fine.

// Update Version
mysqli_query($db,"UPDATE tbladdonmodules SET value='2.0' WHERE module='legalterms' AND setting='version'");

 

Share this post


Link to post
Share on other sites

Double check your code. Dependency files, everything. Try running them through a debugger 1 by 1. 10:1, if the upgrade function isn't working, it's because something, somewhere , is broken inside code, and it can't work.

I've seen this way, way too many times over the years

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated