• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Implemented Development: Add On Upgrade Method

Jeremy

Well-known member
#1
Hey,

The method for handling install / uninstall on XenForo is amazing and powerful. However, I feel that handling 'upgrades' are kind of lacking... I was talking to Jaxel who is using IF NOT EXISTS and other such mySQL queries to insert / create tables. However, I propose a new setting for add-ons: Upgrade Code. Where this code is only run when you upgrade via the Add ons -> Add on -> Controls -> Upgrade Addon and is passed the version number of the current add on (that you're over writing) and you place it on developers to handle upgrading. Something like this:

PHP:
class addOn
{
	public static function upgrade($version)
	{
		if($version < 2)
		{
			// This stuff was added in version id 2
		}
		if($version >= 2 && $version < 5)
		{
			// This stuff was added in version id 2, but removed in version id 5, so we don't want to try to remove it... etc...
		}
	}
}
This will be powerful, and more than just 'cascading' in a sense. The second if() is something I see as extremely useful. So for version ID 2, I add something, but in version ID 5, I decide to move it. If I upgrade from version ID 3, I want to remove the table, but if I'm upgrading from version ID 6, its not there any more, so we don't want to do it.

It'll help create cleaner / more organized SQL and stuff for install() and uninstall(), separating upgrade functionality.
 
R

ragtek

Guest
#3
It would be cool, to have an automatic install code generator.

We had a Add-on in vB, which "checked" the db for new tables and files.
There we could asign the new elements to add-ons.
For example:

Addon: foobar Version 1.0.0

New tables: foo, bar, baz
New fields: table permissions=> canFoo, canBaz

The add-on generated the sql code and inserted it into the install & deinstall code field.
So we hadn't to take care of this:)

This was whery comfortable:)
 

g0rn

Well-known member
#4
When an add-on is upgraded an install callback is being executed as well. It also passes an array with old (before upgrade) add-on info to your callback, so you can use something like
PHP:
public static function install($addon)
{
	if ($addon['versionid'] == 2)
	{
		// This stuff was added in version id 2
	}
	if ($addon['versionid'] >= 2 && $addon['versionid'] < 5)
	{
		// This stuff was added in version id 2, but removed in version id 5, so we don't want to try to remove it... etc...
	}
}
You can also check if $addon is empty and run install code in this case, otherwise check version id and run appropriate upgrade code.