XF 2.0 Setup.php Install Versus Upgrade

Sadik B

Well-known member
In XF 1.0, in my Addon's setup.php I checked for the Installed Version as follows,

PHP:
$installedVersion = is_array($installedAddon) ? $installedAddon['version_id'] : 0;

After this it was a simple task of running code through a less than version check.

PHP:
if ($installedVersion < [ADDON_VERSION])
{
    // RUN STEPS
}

This worked because irrespective of whichever version the user was on, the less than comparison operator made sure, incremental setup code ran.

Now in XenForo version 2, unless I am mistaken, Setup.php is divided into installStepX and UpgradeVersionYStepX.

AM I right in thinking that installStepX ONLY runs on new installs when the addon isn't already installed on a site and UpgradeVersionYStepX, runs by checking user's installed version Y and only running larger version Y numbers?

OR is installStepX also run on upgrades when the addon exists?

Does UpgradeVersionYStepX not run if installed version number is less than Y?

Are there any recommended standards to follow for install versus upgrade?

Thanks
 
AM I right in thinking that installStepX ONLY runs on new installs when the addon isn't already installed on a site and UpgradeVersionYStepX, runs by checking user's installed version Y and only running larger version Y numbers?
That’s correct. Install steps for installs only and upgrade steps for upgrades etc.

The installer only functions like that because of the trait classes which are used by default. Your setup class doesn’t need to use those if you prefer a different approach or the old fashioned way. Without using those traits you just need to include install(), upgrade() and uninstall() methods. This does still separate the logic for installs and upgrades etc. but you can use logic similar to the XF1 installers still by checking the version ID etc.
 
That’s correct. Install steps for installs only and upgrade steps for upgrades etc.

The installer only functions like that because of the trait classes which are used by default. Your setup class doesn’t need to use those if you prefer a different approach or the old fashioned way. Without using those traits you just need to include install(), upgrade() and uninstall() methods. This does still separate the logic for installs and upgrades etc. but you can use logic similar to the XF1 installers still by checking the version ID etc.
Hey @Chris D I hope it wouldn't do any harm If I ask a related question here, from you.


The user has installed the version 1000011
Then, the next release (1000012) adds a new installStep4()

So I executes that step from upgrade1000012Step1() too
like this:
PHP:
    public function upgrade1000012Step1()
    {
        $this->installStep4();
    }

and if another previous upgrade1000011Step1() already had $this->installStep3(); like this:
PHP:
public function upgrade1000011Step1()

{
    $this->installStep3();
}

Do I have to execute both steps in final upgrade step like this?
PHP:
    public function upgrade1000012Step1()
    {
        $this->installStep3();
        $this->installStep4();
    }
or dont? because will xenforo execute all larger upgrade steps from current installed version one by one?

OR

Do I need to put every steps in every upgrade steps like this?
PHP:
    public function upgrade1000011Step1()
    {
        $this->installStep3();
        $this->installStep4();
    }
    public function upgrade1000012Step1()
    {
        $this->installStep3();
        $this->installStep4();
    }
 
XF will step through all upgrade steps so you don't need to call the same code from multiple upgrade steps. Essentially the same way XF upgrades work.
 
and if another previous upgrade1000011Step1() already had $this->installStep3(); like this:
PHP:
public function upgrade1000011Step1()

{
    $this->installStep3();
}

I wouldn't recommend this approach. If you ever come back and alter installStep3, suddenly you will have an inconsistent upgrade path, as it relied on the assumption that your code is in the same state that it was when you first implemented upgrade1000011. In this case, there's really no harm to just copying the method.
 
I wouldn't recommend this approach. If you ever come back and alter installStep3, suddenly you will have an inconsistent upgrade path, as it relied on the assumption that your code is in the same state that it was when you first implemented upgrade1000011. In this case, there's really no harm to just copying the method.
ah yeah, makes sense. thanks for the heads up @Lukas W.

@Chris
php cmd.php xf-addon:upgrade-step --id=Earl/PhoneTest --version=1000012 --step=1 returns Console Tool string in terminal.
And it doesn't do anything.
even php cmd.php xf-addon:upgrade-step --version doesn't show anything but that strange string Console Tool
7xhNgpO.png

is that a bug? or incorrect syntax?
 
The syntax is wrong since you've passed in the required arguments using syntax for command options. Command options are specified with the double dashes whereas arguments come after the command name, need to be separated by spaces and also need to be in a specific order.

As such, the correct syntax would be php cmd.php xf-addon:upgrade-step Earl/PhoneTest 1000012 1.
 
The syntax is wrong since you've passed in the required arguments using syntax for command options. Command options are specified with the double dashes whereas arguments come after the command name, need to be separated by spaces and also need to be in a specific order.

As such, the correct syntax would be php cmd.php xf-addon:upgrade-step Earl/PhoneTest 1000012 1.
Thank you very much! That worked
 
Back
Top Bottom