Refactor the install/upgrade code to be more flexible.

Add xxx_installer::upgrade($version) method so that upgrade stanzas
are separate from install stanzas.  In the old code, to do an upgrade
meant that you had to re-evolve everything from the initial install
because we'd step through each version's changes.  But what we really
want is for the initial install to start off in the perfect initial
state, and the upgrades to do the work behind the scenes.  So now the
install() function gets things set up properly the first time, and the
upgrade() function does any work to catch you up to the latest code.
See gallery_installer.php for a good example.
This commit is contained in:
Bharat Mediratta
2009-06-23 12:00:49 -07:00
parent 342d5e1186
commit bfca0c7903
21 changed files with 429 additions and 449 deletions

View File

@@ -107,7 +107,7 @@ class module_Core {
/**
* Install a module. This will call <module>_installer::install(), which is responsible for
* creating database tables, setting module variables and and calling module::set_version().
* creating database tables, setting module variables and calling module::set_version().
* Note that after installing, the module must be activated before it is available for use.
* @param string $module_name
*/
@@ -130,6 +130,38 @@ class module_Core {
"module", t("Installed module %module_name", array("module_name" => $module_name)));
}
/**
* Upgrade a module. This will call <module>_installer::upgrade(), which is responsible for
* modifying database tables, changing module variables and calling module::set_version().
* Note that after upgrading, the module must be activated before it is available for use.
* @param string $module_name
*/
static function upgrade($module_name) {
$kohana_modules = Kohana::config("core.modules");
array_unshift($kohana_modules, MODPATH . $module_name);
Kohana::config_set("core.modules", $kohana_modules);
$version_before = module::get_version($module_name);
$installer_class = "{$module_name}_installer";
if (method_exists($installer_class, "upgrade")) {
call_user_func_array(array($installer_class, "upgrade"), array($version_before));
}
module::load_modules();
// Now the module is upgraded but inactive, so don't leave it in the active path
array_shift($kohana_modules);
Kohana::config_set("core.modules", $kohana_modules);
$version_after = module::get_version($module_name);
if ($version_before != $version_after) {
log::success(
"module", t("Upgraded module %module_name from %version_before to %version_after",
array("module_name" => $module_name,
"version_before" => $version_before,
"version_after" => $version_after)));
}
}
/**
* Activate an installed module. This will call <module>_installer::activate() which should take
* any steps to make sure that the module is ready for use. This will also activate any