Have an addon upgrade not affect database?

Jarvis

Member
If I want the upgrade of an addon to not run anything on the database, can I manage that with the install callback?

Currently I have:

PHP:
// This is the function to create a table in the database so our addon will work.
public static function install($addon)
{
    if ($addon['version_id'] < 575) {
        $db = XenForo_Application::getDb();
        // add tables from first release
    } else if ($addon['version_id'] == 575) {
        $db = XenForo_Application::getDb();
        // add new table included in version 575
    } else {
        $db = XenForo_Application::getDb();
        // create tables
    }
}

Is there anything such as a boolean for if the user is upgrading or installing an addon? As I'd like to have the upgrade not affect data already entered into the tables as currently any upgrade wipes table data.
 
Simply put, the presence of $addon['version_id'] means the user is upgrading. The absence means they are installing.

What you show in your sample shouldn't do anything with the existing data so long as your table creation queries are all CREATE TABLE IF NOT EXISTS types.

If you're adding or deleting data from a table in a routine is the only thing that might change existing data.
 
Last edited:
The presence of $addon['version_id'] means the user is upgrading. The absence means they are installing.

What you show in your sample shouldn't do anything with the existing data so long as your table creation queries are all CREATE TABLE IF NOT EXISTS types.

If you're adding or deleting data from a table in a routine is the only thing that might change existing data.
Well that's what I thought. Logically it makes sense. This is an example table that is created in install.php

PHP:
'createRRDLogs' => 'CREATE TABLE IF NOT EXISTS `xf_ct_rrd_logs` (             
        `log_id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT,
        `enlistment_id` INT( 10 ) UNSIGNED NOT NULL,
        `user_id` INT ( 10 ) NOT NULL ,
        `username` VARCHAR (50) NOT NULL ,
        `log_date` BIGINT ( 20 ) NOT NULL,
        `action_taken` VARCHAR (250) NOT NULL,
        PRIMARY KEY (`log_id`)
        )
    ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;',
'dropRRDLogs' => 'DROP TABLE IF EXISTS `xf_ct_rrd_logs`',

then:

PHP:
public static function install($addon)
{
$db->query(self::$table['createRRDLogs']);
}

public static function uninstall()
{
    $db->query(self::$table['dropRRDLogs']);
}
 
I assume the array is assigned to $table there. I'm not a big fan of arrays for that purpose. They just increase the data segment of the memory stack for no good reason, but that's up to you. Personally I'd just put the query into it's own $db call without the array.

If the queries you have are running properly, the data would not be destroyed. If data is being destroyed, I think it may be somewhere else in the install routine.
 
I assume the array is assigned to $table there. I'm not a big fan of arrays for that purpose. They just increase the data segment of the memory stack for no good reason, but that's up to you. Personally I'd just put the query into it's own $db call without the array.

If the queries you have are running properly, the data would not be destroyed. If data is being destroyed, I think it may be somewhere else in the install routine.

I used the example found in a tutorial somewhere here. I'll definitely try your way though, sounds much better.

Hmm. The entire install file is just the $table, and the install and uninstall methods. I'm not deleting any rows in a controller.
 
I can honestly say I've never seen a CREATE TABLE IF NOT EXISTS kill any data in a table.

Either the table is being dropped and recreated for some reason, or something else is killing the data.

Maybe someone else has an idea, because I don't with the info provided.
 
There is one problem I do see...
PHP:
public static function install($addon)
{
  if ($addon['version_id'] < 575) {
    $db = XenForo_Application::getDb();
  // add tables from first release
  } else if ($addon['version_id'] == 575) {
    $db = XenForo_Application::getDb();
  // add new table included in version 575
  } else {
    $db = XenForo_Application::getDb();
  // create tables
  }
}
The else if will only take place after version 575 is installed and after running the install xml a second time. $addon is the currently installed version, not the version you are installing.

$addon will be either NULL or empty on a new install (forgot which and I'm too lazy to look right now :D)
$addon will contain the currently installed version if the add-on is already installed.

So this would be a more reasonable if...
PHP:
public static function install($addon)
{
  if (!$addon) {
    $db = XenForo_Application::getDb();
  // add tables from first release
  // AND FROM CURRENT VERSION
  } else if ($addon['version_id'] <= 574) {
    $db = XenForo_Application::getDb();
  // add new table included in version 575
  } else {
    $db = XenForo_Application::getDb();
  // create tables
  }
}
Although I don't think the final ELSE is needed. But I don't know what you're doing there.

And actually if you just do separate CREATE TABLE IF NOT EXISTS db queries, the if isn't needed at all. The only time you need to check the version with an IF is if you're changing the structure of a table. And even then, you don't HAVE to do it that way since it can be done with a db query that checks if the column exists already.
 
Last edited:
The else if will only take place after version 575 is installed and after running the install xml a second time. $addon is the currently installed version, not the version you are installing.
Ah I see. So it passes the current version, not the versionID being upgraded to.

Although I don't think the final ELSE is needed. But I don't know what you're doing there.
This was more of just a temp expression to make sure if the addon wasn't less than or equal to 575 that it would still actually install the addon, however i'm guessing the comparison only runs in the first place if it's an upgrade.

Can I have an expression to manage first time installations?

Something like:

Code:
if freshInstall
   // create tables
else checkVersionID
   if id <= 4
      // Do upgrade for version 5
   else if >= 5
      // ignore and move on
   end
end

Edit: I've just read your post again. Clearly I was still sleepy when I replied. $addon will be Null when fresh. Thanks!
 
Last edited:
You're overthinking the whole thing. ;)

There's no need for the check if it's a fresh install or if the version is greater than 5.

PHP:
public static function install($addon)
{
   $versionId = is_array($addon) ? $addon['version_id'] : 0;
   $db CREATE TABLE IF NOT EXISTS routines for initial table installs

   if($versionId <= 4)
   {
        // Do upgrade for version 5
   }
}

Then if you add a version 6...
PHP:
public static function install($addon)
{
   $versionId = is_array($addon) ? $addon['version_id'] : 0;
   $db CREATE TABLE IF NOT EXISTS routines for initial table installs

   if($versionId <= 4)
   {
        // Do upgrade for version 5
   }

   if($versionId <= 5)
   {
        // Do upgrade for version 6
   }
}
 
Back
Top Bottom