Extension Development: How to automatically update Database?
This is an open discussion with 4 replies, filed under Extensions.
Search
You can implement update($previousVersion) function in extension driver and run database queries there (pseudo code):
function update($previousVersion) { if (floatval($previousVersion) < 1.2) { $db = Administration::instance()->Database; $db->query('ALTER TABLE example ADD column type default value'); } }
:).
ahwayakchih is most correct. As an example, this is the update function from the latest version of the Select Box Link field:
public function update($previousVersion){ if(version_compare($previousVersion, '1.6', '<')){ $this->_Parent->Database->query("ALTER TABLE `sym_fields_selectbox_link` ADD `limit` INT(4) UNSIGNED NOT NULL DEFAULT '20'"); $this->_Parent->Database->query("ALTER TABLE `sym_fields_selectbox_link` CHANGE `related_field_id` `related_field_id` VARCHAR(255) NOT NULL"); } return true; }
Just realised I need to update $this->_Parent->Database->query(
to be Administration::instance()->Database->query(
, but either will work.
Thank you guys!
Create an account or sign in to comment.
This is a question to all extension developers:
If I'd like to automatically update the database after a new version of an extension is installed, how do I do this?