Extension database update

Discussion in 'Extensions' started by stan911, May 21, 2018.

  1. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    How do you run the required database update, when installing/enabling a new extension. And upon uninstalling it, how do you rollback or drop those changes. Not looking for SQL code :p

    I need to create 2 tables and drop them if the extensions gets uninstalled.
     
  2. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    10,254
    Likes Received:
    2,382
    @stan911 - Here's what we do for the backup manager extension:
    PHP:
    // create the database table
    public function beforeEnable()
    {
        
    Yii::app()->getDb()->createCommand('
        CREATE TABLE IF NOT EXISTS `{{backup_manager_snapshot}}` (
          `snapshot_id` INT NOT NULL AUTO_INCREMENT,
          `name` VARCHAR(50) NOT NULL,
          `path` VARCHAR(255) NOT NULL,
          `size` INT(11) NOT NULL,
          `meta_data` BLOB NULL,
          `date_added` DATETIME NOT NULL,
          PRIMARY KEY (`snapshot_id`))
        ENGINE=InnoDB CHARSET=utf8;
        '
    )->execute();
        return 
    true;
    }

    // drop the table
    public function beforeDelete()
    {
        
    Yii::import('ext-backup-manager.common.models.*');
        
    BackupManagerSnapshot::model()->deleteAllBackups();
        
    Yii::app()->getDb()->createCommand('DROP TABLE IF EXISTS `{{backup_manager_snapshot}}`')->execute();
        return 
    true;
    }
    Those methods are in the extension class itself. Check out /apps/common/components/init/ExtensionInit.php to see what methods each extension inherits ;)
     
  3. stan911

    stan911 New Member

    Joined:
    May 15, 2018
    Messages:
    13
    Likes Received:
    1
    S.E:
    Expired
    L.T:
    Regular
    L.C:
    1
    This works just fine. Thank you!
     

Share This Page