diff options
author | zotlabs <mike@macgirvin.com> | 2018-02-16 12:42:02 -0800 |
---|---|---|
committer | zotlabs <mike@macgirvin.com> | 2018-02-16 12:42:02 -0800 |
commit | f492f808f4861ae9937dcaf3bf8476513ae1c091 (patch) | |
tree | dba3179893ab7beae238568a87578f2a498f12aa /Zotlabs/Lib/DB_Upgrade.php | |
parent | 27cd26ec1e26b6c389a623395687af5052f0b5a1 (diff) | |
download | volse-hubzilla-f492f808f4861ae9937dcaf3bf8476513ae1c091.tar.gz volse-hubzilla-f492f808f4861ae9937dcaf3bf8476513ae1c091.tar.bz2 volse-hubzilla-f492f808f4861ae9937dcaf3bf8476513ae1c091.zip |
refactor of the DB update system. Updates are now stored individually in Zotlabs/Update/_nnnn.php and are objects; so only the pending updates need to be loaded and executed rather than all historical updates. There is one single number (DB_UPDATE_VERSION) representing the current version and it is EQUAL TO the last known update. A dummy update _1201 was created to address the difference in counting behaviour; it will be executed on the next change of DB_UPDATE_VERSION as well as the next update. The database config values are also loaded from disk on every update immediately before setting the update lock in order to reduce timing conflicts and race conditions.
Diffstat (limited to 'Zotlabs/Lib/DB_Upgrade.php')
-rw-r--r-- | Zotlabs/Lib/DB_Upgrade.php | 134 |
1 files changed, 60 insertions, 74 deletions
diff --git a/Zotlabs/Lib/DB_Upgrade.php b/Zotlabs/Lib/DB_Upgrade.php index 8f0488f6f..4038a2d53 100644 --- a/Zotlabs/Lib/DB_Upgrade.php +++ b/Zotlabs/Lib/DB_Upgrade.php @@ -10,22 +10,12 @@ class DB_Upgrade { function __construct($db_revision) { - $platform_name = System::get_platform_name(); + $this->config_name = 'db_version'; + $this->func_prefix = '_'; - $update_file = 'install/' . $platform_name . '/update.php'; - if(! file_exists($update_file)) { - $update_file = 'install/update.php'; - $this->config_name = 'db_version'; - $this->func_prefix = 'update_r'; - } - else { - $this->config_name = $platform_name . '_db_version'; - $this->func_prefix = $platform_name . '_update_'; - } - - $build = get_config('system', $this->config_name, 0); + $build = get_config('system', 'db_version', 0); if(! intval($build)) - $build = set_config('system', $this->config_name, $db_revision); + $build = set_config('system', 'db_version', $db_revision); if($build == $db_revision) { // Nothing to be done. @@ -40,82 +30,78 @@ class DB_Upgrade { $current = intval($db_revision); - if(($stored < $current) && file_exists($update_file)) { + if($stored < $current) { - Config::Load('database'); + // The last update we performed was $stored. + // Start at $stored + 1 and continue until we have completed $current - // We're reporting a different version than what is currently installed. - // Run any existing update scripts to bring the database up to current. - - require_once($update_file); + for($x = $stored + 1; $x <= $current; $x ++) { + $s = '_' . $x; + $cls = '\\Zotlabs\Update\\' . $s ; + if(! class_exists($cls)) { + return; + } - // make sure that boot.php and update.php are the same release, we might be - // updating from git right this very second and the correct version of the update.php - // file may not be here yet. This can happen on a very busy site. + // There could be a lot of processes running or about to run. + // We want exactly one process to run the update command. + // So store the fact that we're taking responsibility + // after first checking to see if somebody else already has. - if($db_revision == UPDATE_VERSION) { - for($x = $stored; $x < $current; $x ++) { - $func = $this->func_prefix . $x; - if(function_exists($func)) { - // There could be a lot of processes running or about to run. - // We want exactly one process to run the update command. - // So store the fact that we're taking responsibility - // after first checking to see if somebody else already has. + // If the update fails or times-out completely you may need to + // delete the config entry to try again. - // If the update fails or times-out completely you may need to - // delete the config entry to try again. + Config::Load('database'); - if(get_config('database', $func)) - break; - set_config('database',$func, '1'); - // call the specific update + if(get_config('database', $s)) + break; + set_config('database',$s, '1'); + - $retval = $func(); - if($retval) { + $c = new $cls(); + $retval = $c->run(); - // Prevent sending hundreds of thousands of emails by creating - // a lockfile. + if($retval != UPDATE_SUCCESS) { - $lockfile = 'store/[data]/mailsent'; + // Prevent sending hundreds of thousands of emails by creating + // a lockfile. - if ((file_exists($lockfile)) && (filemtime($lockfile) > (time() - 86400))) - return; - @unlink($lockfile); - //send the administrator an e-mail - file_put_contents($lockfile, $x); - - $r = q("select account_language from account where account_email = '%s' limit 1", - dbesc(\App::$config['system']['admin_email']) - ); - push_lang(($r) ? $r[0]['account_language'] : 'en'); + $lockfile = 'store/[data]/mailsent'; - z_mail( + if ((file_exists($lockfile)) && (filemtime($lockfile) > (time() - 86400))) + return; + @unlink($lockfile); + //send the administrator an e-mail + file_put_contents($lockfile, $x); + + $r = q("select account_language from account where account_email = '%s' limit 1", + dbesc(\App::$config['system']['admin_email']) + ); + push_lang(($r) ? $r[0]['account_language'] : 'en'); + z_mail( + [ + 'toEmail' => \App::$config['system']['admin_email'], + 'messageSubject' => sprintf( t('Update Error at %s'), z_root()), + 'textVersion' => replace_macros(get_intltext_template('update_fail_eml.tpl'), [ - 'toEmail' => \App::$config['system']['admin_email'], - 'messageSubject' => sprintf( t('Update Error at %s'), z_root()), - 'textVersion' => replace_macros(get_intltext_template('update_fail_eml.tpl'), - [ - '$sitename' => \App::$config['system']['sitename'], - '$siteurl' => z_root(), - '$update' => $x, - '$error' => sprintf( t('Update %s failed. See error logs.'), $x) - ] - ) + '$sitename' => \App::$config['system']['sitename'], + '$siteurl' => z_root(), + '$update' => $x, + '$error' => sprintf( t('Update %s failed. See error logs.'), $x) ] - ); - - //try the logger - logger('CRITICAL: Update Failed: ' . $x); - pop_lang(); - } - else { - set_config('database',$func, 'success'); - } - } + ) + ] + ); + + //try the logger + logger('CRITICAL: Update Failed: ' . $x); + pop_lang(); + } + else { + set_config('database',$s, 'success'); } - set_config('system', $this->config_name, $db_revision); } } + set_config('system', 'db_version', $db_revision); } } }
\ No newline at end of file |