XF 2.2 Appropriate way to alter addon

chris p

Member
We went live with my addon last week and naturally, now I have a list of changes to apply to it for user taste.

I had one emergency change I needed to make and noticed on the production system where I installed the addon, the addon files were now read only (presumably supposed to be, not to make changes on the fly). I just used root and did it since it was a 1 character change. That seemed to work without repercussions but seemed rather like I was committing a sin of some sort. I expected something to kick out a config change warning but didn't see anything.

Now I want to make significant changes to the addon, meaning using our dev system and doing extensive testing before reinstalling it on the prod system. I can't remove and reinstall the addon because it would destroy critical db data from the addon's custom fields.

Is there anything special I have to do to create an update zip after completing and testing the changes to apply the addon to the production system without having to back it out and reinstall it? Or am I supposed to just to hack the changes in on the prod system when I know they are good?
 
If you install an addon that's already installed, it will put the files in place and you will get an option to upgrade to the new version (if you addon has upgrade steps, it will perform those... otherwise it will just rebuild things like templates and phrases).

Basically, you don't uninstall and reinstall to upgrade. You just "install" the newer versions without uninstalling or disabling.
 
If you install an addon that's already installed, it will put the files in place and you will get an option to upgrade to the new version (if you addon has upgrade steps, it will perform those... otherwise it will just rebuild things like templates and phrases).

Basically, you don't uninstall and reinstall to upgrade. You just "install" the newer versions without uninstalling or disabling.
Thank you for that. I presume it won't active the db column removal portion then to reinstall.
 
Correct... your Setup.php file contains all the stuff for install/upgrade/uninstall.

That being said, if your install code is how it should be (using the schemaManager to do things like create tables), you can run the setup multiple times (if you wanted to for whatever reason) and it's fine. Internally XenForo looks to see if the table already exists and if the instructions to add a new table is different than the one already in the database and does what it needs to do based on that info.

Now if you are going your own route and doing raw queries for things like CREATE TABLE or ALTER TABLE, you are on your own... you won't get XenForo abstracting that stuff out for you because well... you didn't want it to.
 
Correct... your Setup.php file contains all the stuff for install/upgrade/uninstall.

That being said, if your install code is how it should be (using the schemaManager to do things like create tables), you can run the setup multiple times (if you wanted to for whatever reason) and it's fine. Internally XenForo looks to see if the table already exists and if the instructions to add a new table is different than the one already in the database and does what it needs to do based on that info.

Now if you are going your own route and doing raw queries for things like CREATE TABLE or ALTER TABLE, you are on your own... you won't get XenForo abstracting that stuff out for you because well... you didn't want it to.
My Setup.php contains only dropColumn and addColumn methods and I will be adding an additional column for the change. I wasn't aware of the update method so it contains none (nor would these changes require an update). I've only added, and will be adding, columns, no tables. It's sounds like I'll be okay from what you describe. Thank you very much for the detailed information.
 
I finally did the addon update on our dev server. What I found was the table additions in Setup.php weren't made Would it have been more appropriate to manually run the cmd.php Installstep1 command after installing the addon update?
 
You would use upgrade functions for that.

PHP:
public function installStep1()
{
    //
}

That runs on new installs only.

PHP:
public function upgrade1000470Step1()
{
    //
}

That would run when upgrading to (or via) 1.0.4. So if you were going from 1.0.2 to 1.0.4, or 1.0.2 to 1.0.6, this would get run.


If you add new columns in an update though, you'd also want to add an install step so they also get added when the addon is freshly installed in future.
 
You would use upgrade functions for that.

PHP:
public function installStep1()
{
    //
}

That runs on new installs only.

PHP:
public function upgrade1000470Step1()
{
    //
}

That would run when upgrading to (or via) 1.0.4. So if you were going from 1.0.2 to 1.0.4, or 1.0.2 to 1.0.6, this would get run.


If you add new columns in an update though, you'd also want to add an install step so they also get added when the addon is freshly installed in future.
Thanks very much for the response. So the upgrade Step just contains a single $table->addColumn?

This will never be installed except as an upgrade which will completely replace the relevant code so versioning isn't necessary if it's possible to do it without. Can one just specify a method of upgradeStep1() in this case then alter that code in the future to only have the add the table additions for the upgrade? The addon is nothing any logical site admin would want so it won't be freshly installed then upgraded anywhere. If we reinstalled it would be because of some catastrophe and install it fresh.
 
The steps would contain whatever you need them to. You'd need to wrap an addColumn call in a schema manager function:

PHP:
public function installStep1()
{
    $this->schemaManager()->alterTable('xf_table_name', function (Alter $table) {
        $table->addColumn('column_name' .....
    });
}

You can see examples in other addons.

Upgrade steps need to have a version in the name. There's no reason not to change the version if making changes, it's one command to bump it up. If you only add install steps, they'll never be run when upgrading the addon so you'd have to run them manually every time, but if you add upgrade steps XF will run them for you.
 
The steps would contain whatever you need them to. You'd need to wrap an addColumn call in a schema manager function:

PHP:
public function installStep1()
{
    $this->schemaManager()->alterTable('xf_table_name', function (Alter $table) {
        $table->addColumn('column_name' .....
    });
}

You can see examples in other addons.

Upgrade steps need to have a version in the name. There's no reason not to change the version if making changes, it's one command to bump it up. If you only add install steps, they'll never be run when upgrading the addon so you'd have to run them manually every time, but if you add upgrade steps XF will run them for you.
Got it, thanks. I didn't correctly ask my question, I just wanted to make sure that the other addColumns from the original installStep didn't have to be included in the upgrade steps. The fuzziness was still my being concerned about tampering with the original Columns, and you've clarified that.

I'll futz with the versioning. It was a mystery I was hoping to not solve, but I will.

Thank you very much. You've clarified the path to fix my problem.
 
Top Bottom