Upgrades
Releasing updates and upgrades to custom modules is likely something that is needed to be done at some point. If those modules need to modify the database structure or perform other functions that would be handled in the
_activate
function, then you need some way of handling that.With the Addon Modules system, this is a breeze with the upgrade function. The upgrade function is called the first time a module is accessed following an update. The update is detected by a change of version number in the
_config
array of the module. The upgrade function is passed the previous version number so that the module can then decide what updates to run within that function. This allows the module to bring it up to date with your latest version.Addon Upgrade Access URL:
https://your-domain.com/admin/addons/youraddonname/upgrade
Upgrade Function:
An example of how this function can be used is demonstrated below:
<?php
function yourmodule_upgrade($params=[]) {
// Create custom tables and schema required by your module
try {
$InstalledVersion = $vars['version'];
// Perform SQL schema changes required by the upgrade to version 1.1 of your module
if($InstalledVersion < 1.1)
{
Method 1:
$dbCommands[] = "ALTER TABLE mod_example_table ADD email varchar(40) NOT NULL AFTER id;";
foreach ($dbCommands as $cmd)
{
$created = full_query($cmd);
}
Method 2:
Capsule::schema()
->create(
'mod_example_table',
function ($table) {
$table->text('email');
}
);
}
//Perform SQL schema changes required by the upgrade to version 1.2 of your module
if($InstalledVersion < 1.2)
{
Method 1:
$dbCommands[] = "ALTER TABLE mod_example_table ADD demo2 varchar(40) NOT NULL AFTER email;";
foreach ($dbCommands as $cmd)
{
$created = full_query($cmd);
}
Method 2:
Capsule::schema()
->create(
'mod_example_table',
function ($table) {
$table->text('demo2');
}
);
}
} catch (\Exception $e) {
return [
// Supported values here include: success, error or info
'status' => "error",
'description' => 'Unable to create mod_example_table: ' . $e->getMessage(),
];
}
}