XF 2.2 Job/Batch Update in Setup.php

mjda

Well-known member
I have an add-on with two different tables (Entities). One is for xf_addon_data and one for xf_addon_data_archive. I'm wanting to move all the records from xf_addon_data_archive to the xf_addon_data table in the Setup.php when I upgrade the add-on. However, I worry about doing a simple foreach because, in my case, there are well over 25,000 records in the archive and I'm worried it could time-out.

I know how to create a Job for that, and I could run it from the Tools->Rebuild caches page, but that doesn't seem like the right way to do it. I think it makes much more sense to do what I'm wanting to do during the upgrade so there isn't an additional step that must be done after the upgrade.

My question is, is there a way to run the Job from Setup.php, or is there a different/better way to do these in batches to prevent it from timing out?
 
You can queue the job in the postUpgrade method of your add-on and flag it as manual, that way it'll be run as part of the upgrade.

You can also look into a repeatable upgrade step if this is a one-time endeavor. All upgrade steps receive a $stepData object which you can return with information on how far your step has progressed and the installer will repeat running it until completed. The core software uses that to convert table columns from serialized to json.
 
You can queue the job in the postUpgrade method of your add-on and flag it as manual, that way it'll be run as part of the upgrade.

You can also look into a repeatable upgrade step if this is a one-time endeavor. All upgrade steps receive a $stepData object which you can return with information on how far your step has progressed and the installer will repeat running it until completed. The core software uses that to convert table columns from serialized to json.

Thank you. I'll look into that and see what I can come up with.
 
I was able to get this working using $this->app->jobManager()->enqueueUnique() in my Setup.php.

However, now I have another related issue I could use some help figuring out.

So now I have a few columns in xf_addon_data that I'm wanting to move to xf_addon_data_extra. Again, there are so many records that I don't want to do this with a single query for fear that it may time out. However, I can't use the enqueueUnique because right after the step that does it, I want to drop those columns from xf_addon_data. The issue I'm having is that the columns are being dropped in Setup.php before the job is ran, which leads to errors and the data not being able to be moved over.

I'm assuming this is something I might be able to do with the $stepData object that @Lukas W. mentioned, but I'm not able to figure out how to use it. Basically I want to pull all the records from xf_addon_data, and copy the data from a few of those columns into columns of the same name in xf_addon_data_extra, then delete the columns from xf_addon_data but without risking it timing out during the upgrade process.

Anyone who can help here, or lead me into the direction of where to look for more examples?
 
Top Bottom