Plugin upgrade code

tenants

Well-known member
I'm still new to the xenForo plug-in system. When a plug-in is upgraded, it seems to re-run the installation code, which is fine in most cases, since you can just insert if row doesnt already exist / unique idx isnt present / update if idx is present.

But I was wondering what people do to prevent modified tables re-inserting all of the default data again on upgrading when the row no longer exists

For instance, as part of my install code, I create a table and then insert lots of default rows into the table.

Code:
INSERT IGNORE INTO `table` (`col1`, `col2`) VALUES
        (1, 'lots of rows1'), (2, 'lots of rows2'), etc...

But, users will modify this table and drop some of the rows they don't want. Obviously, if they upgrade, these default rows will re-insert (not what they want)

For now, I could run a check to see if the table already exists,

Code:
    public static function tableExists($db, $tableName)
   {
        if ($db->query('SHOW TABLES WHERE Tables_in_xenforo  = "'.$tableName.'"'))
        {
            return true;
        }
        return false;
    }

if it already exists, dont insert the default data.

But there doesn't seem to be any call back in the ACP specifically for "upgrade code". Are there any plans for a separate ACP "upgrade code" call back?
 
So, the above tableExists function isn't very universal, not everyone's schema will be called "xenforo" (a stupid oversight that I spotted when trying to upgrade my own database)

I'm trying this for now, I'll let you know if many people shout at me:

Code:
    public static function tableExists($db, $tableName)
    {
      if ($db->query('SELECT table_name FROM information_schema.tables where table_name = "'.$tableName.'"'))
        {
            return true;
        }
        return false;
    }
 
Top Bottom