Extension database update

stan911

New Member
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.
 
@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 ;)
 
Back
Top