Merge pull request #5830 from nupplaphil/update_from_boot

update_db & run_update_function to Friendica\Core\Update
This commit is contained in:
Hypolite Petovan 2018-10-31 11:13:59 -04:00 committed by GitHub
commit 50ba028094
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 322 additions and 277 deletions

View File

@ -7,6 +7,7 @@
use Friendica\App; use Friendica\App;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Core\Update;
// Get options // Get options
$shortopts = 'sn'; $shortopts = 'sn';
@ -30,7 +31,7 @@ require_once "boot.php";
$a = new App(dirname(__DIR__)); $a = new App(dirname(__DIR__));
// Check the database structure and possibly fixes it // Check the database structure and possibly fixes it
check_db(true); Update::check(true);
// Quit when in maintenance // Quit when in maintenance
if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) { if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) {

158
boot.php
View File

@ -28,9 +28,9 @@ use Friendica\Core\L10n;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Update;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\Conversation; use Friendica\Model\Conversation;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -43,13 +43,6 @@ define('FRIENDICA_VERSION', '2018.12-dev');
define('DFRN_PROTOCOL_VERSION', '2.23'); define('DFRN_PROTOCOL_VERSION', '2.23');
define('NEW_UPDATE_ROUTINE_VERSION', 1170); define('NEW_UPDATE_ROUTINE_VERSION', 1170);
/**
* @brief Constants for the database update check
*/
const DB_UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
const DB_UPDATE_SUCCESSFUL = 1; // Database check was successful
const DB_UPDATE_FAILED = 2; // Database check failed
/** /**
* @brief Constant with a HTML line break. * @brief Constant with a HTML line break.
* *
@ -119,18 +112,6 @@ define('REGISTER_OPEN', 2);
* @} * @}
*/ */
/**
* @name Update
*
* DB update return values
* @{
*/
define('UPDATE_SUCCESS', 0);
define('UPDATE_FAILED', 1);
/**
* @}
*/
/** /**
* @name CP * @name CP
* *
@ -432,143 +413,6 @@ function defaults() {
return $return; return $return;
} }
/**
* @brief Function to check if request was an AJAX (xmlhttprequest) request.
*
* @param boolean $via_worker boolean Is the check run via the worker?
*/
function check_db($via_worker)
{
$build = Config::get('system', 'build');
if (empty($build)) {
Config::set('system', 'build', DB_UPDATE_VERSION - 1);
$build = DB_UPDATE_VERSION - 1;
}
// We don't support upgrading from very old versions anymore
if ($build < NEW_UPDATE_ROUTINE_VERSION) {
die('You try to update from a version prior to database version 1170. The direct upgrade path is not supported. Please update to version 3.5.4 before updating to this version.');
}
if ($build < DB_UPDATE_VERSION) {
// When we cannot execute the database update via the worker, we will do it directly
if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
update_db();
}
}
}
/**
* @brief Automatic database updates
* @param object $a App
*/
function update_db()
{
$build = Config::get('system', 'build');
if (empty($build) || ($build > DB_UPDATE_VERSION)) {
$build = DB_UPDATE_VERSION - 1;
Config::set('system', 'build', $build);
}
if ($build != DB_UPDATE_VERSION) {
require_once 'update.php';
$stored = intval($build);
$current = intval(DB_UPDATE_VERSION);
if ($stored < $current) {
Config::load('database');
// Compare the current structure with the defined structure
$t = Config::get('database', 'dbupdate_' . DB_UPDATE_VERSION);
if (!is_null($t)) {
return;
}
// run the pre_update_nnnn functions in update.php
for ($x = $stored + 1; $x <= $current; $x++) {
$r = run_update_function($x, 'pre_update');
if (!$r) {
break;
}
}
Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, time());
// update the structure in one call
$retval = DBStructure::update(false, true);
if ($retval) {
DBStructure::updateFail(
DB_UPDATE_VERSION,
$retval
);
return;
} else {
Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
}
// run the update_nnnn functions in update.php
for ($x = $stored + 1; $x <= $current; $x++) {
$r = run_update_function($x, 'update');
if (!$r) {
break;
}
}
}
}
return;
}
function run_update_function($x, $prefix)
{
$funcname = $prefix . '_' . $x;
if (function_exists($funcname)) {
// 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.
$t = Config::get('database', $funcname);
if (!is_null($t)) {
return false;
}
Config::set('database', $funcname, time());
// call the specific update
$retval = $funcname();
if ($retval) {
//send the administrator an e-mail
DBStructure::updateFail(
$x,
L10n::t('Update %s failed. See error logs.', $x)
);
return false;
} else {
Config::set('database', $funcname, 'success');
if ($prefix == 'update') {
Config::set('system', 'build', $x);
}
return true;
}
} else {
Config::set('database', $funcname, 'success');
if ($prefix == 'update') {
Config::set('system', 'build', $x);
}
return true;
}
}
/** /**
* @brief Used to end the current process, after saving session state. * @brief Used to end the current process, after saving session state.
* @deprecated * @deprecated

View File

@ -16,6 +16,7 @@ use Friendica\Core\L10n;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Theme; use Friendica\Core\Theme;
use Friendica\Core\Update;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
@ -864,10 +865,10 @@ function admin_page_summary(App $a)
} }
} }
if (Config::get('system', 'dbupdate', DB_UPDATE_NOT_CHECKED) == DB_UPDATE_NOT_CHECKED) { if (Config::get('system', 'dbupdate', DBStructure::UPDATE_NOT_CHECKED) == DBStructure::UPDATE_NOT_CHECKED) {
DBStructure::update(false, true); DBStructure::update(false, true);
} }
if (Config::get('system', 'dbupdate') == DB_UPDATE_FAILED) { if (Config::get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) {
$showwarning = true; $showwarning = true;
$warningtext[] = L10n::t('The database update failed. Please run "php bin/console.php dbstructure update" from the command line and have a look at the errors that might appear.'); $warningtext[] = L10n::t('The database update failed. Please run "php bin/console.php dbstructure update" from the command line and have a look at the errors that might appear.');
} }
@ -1595,7 +1596,8 @@ function admin_page_dbsync(App $a)
$retval = DBStructure::update(false, true); $retval = DBStructure::update(false, true);
if ($retval === '') { if ($retval === '') {
$o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />"; $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success'); Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);
Config::set('database', 'last_successful_update_time', time());
} else { } else {
$o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />"; $o .= L10n::t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
} }
@ -1612,9 +1614,9 @@ function admin_page_dbsync(App $a)
if (function_exists($func)) { if (function_exists($func)) {
$retval = $func(); $retval = $func();
if ($retval === UPDATE_FAILED) { if ($retval === Update::FAILED) {
$o .= L10n::t("Executing %s failed with error: %s", $func, $retval); $o .= L10n::t("Executing %s failed with error: %s", $func, $retval);
} elseif ($retval === UPDATE_SUCCESS) { } elseif ($retval === Update::SUCCESS) {
$o .= L10n::t('Update %s was successfully applied.', $func); $o .= L10n::t('Update %s was successfully applied.', $func);
Config::set('database', $func, 'success'); Config::set('database', $func, 'success');
} else { } else {

View File

@ -1682,7 +1682,7 @@ class App
$this->module = 'maintenance'; $this->module = 'maintenance';
} else { } else {
$this->checkURL(); $this->checkURL();
check_db(false); Core\Update::check(false);
Core\Addon::loadAddons(); Core\Addon::loadAddons();
Core\Hook::loadHooks(); Core\Hook::loadHooks();
} }

View File

@ -19,6 +19,7 @@ class Cache extends \Friendica\BaseObject
const QUARTER_HOUR = 900; const QUARTER_HOUR = 900;
const FIVE_MINUTES = 300; const FIVE_MINUTES = 300;
const MINUTE = 60; const MINUTE = 60;
const INFINITE = 0;
/** /**
* @var Cache\ICacheDriver * @var Cache\ICacheDriver

View File

@ -40,7 +40,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
*/ */
public function get($key) public function get($key)
{ {
$cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]); $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
if (DBA::isResult($cache)) { if (DBA::isResult($cache)) {
$cached = $cache['v']; $cached = $cache['v'];
@ -62,11 +62,19 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
*/ */
public function set($key, $value, $ttl = Cache::FIVE_MINUTES) public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{ {
$fields = [ if ($ttl > 0) {
'v' => serialize($value), $fields = [
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'), 'v' => serialize($value),
'updated' => DateTimeFormat::utcNow() 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
]; 'updated' => DateTimeFormat::utcNow()
];
} else {
$fields = [
'v' => serialize($value),
'expires' => -1,
'updated' => DateTimeFormat::utcNow()
];
}
return DBA::update('cache', $fields, ['k' => $key], true); return DBA::update('cache', $fields, ['k' => $key], true);
} }

View File

@ -34,7 +34,7 @@ interface ICacheDriver
* *
* @param string $key The cache key * @param string $key The cache key
* @param mixed $value The value to store * @param mixed $value The value to store
* @param integer $ttl The cache lifespan, must be one of the Cache constants * @param integer $ttl The cache lifespan, must be one of the Cache constants
* *
* @return bool * @return bool
*/ */

View File

@ -28,7 +28,7 @@ interface IMemoryCacheDriver extends ICacheDriver
* @param string $key The cache key * @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache * @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set * @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants * @param int $ttl The cache lifespan, must be one of the Cache constants
* *
* @return bool * @return bool
*/ */

View File

@ -3,6 +3,7 @@
namespace Friendica\Core\Console; namespace Friendica\Core\Console;
use Friendica\Core; use Friendica\Core;
use Friendica\Core\Update;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
use RuntimeException; use RuntimeException;
@ -24,7 +25,7 @@ class DatabaseStructure extends \Asika\SimpleConsole\Console
$help = <<<HELP $help = <<<HELP
console dbstructure - Performs database updates console dbstructure - Performs database updates
Usage Usage
bin/console dbstructure <command> [-h|--help|-?] [-v] bin/console dbstructure <command> [-h|--help|-?] |-f|--force] [-v]
Commands Commands
dryrun Show database update schema queries without running them dryrun Show database update schema queries without running them
@ -35,14 +36,13 @@ Commands
Options Options
-h|--help|-? Show help information -h|--help|-? Show help information
-v Show more debug information. -v Show more debug information.
-f|--force Force the command in case of "update" (Ignore failed updates/running updates)
HELP; HELP;
return $help; return $help;
} }
protected function doExecute() protected function doExecute()
{ {
$a = get_app();
if ($this->getOption('v')) { if ($this->getOption('v')) {
$this->out('Class: ' . __CLASS__); $this->out('Class: ' . __CLASS__);
$this->out('Arguments: ' . var_export($this->args, true)); $this->out('Arguments: ' . var_export($this->args, true));
@ -69,34 +69,8 @@ HELP;
$output = DBStructure::update(true, false); $output = DBStructure::update(true, false);
break; break;
case "update": case "update":
$build = Core\Config::get('system', 'build'); $force = $this->getOption(['f', 'force'], false);
if (empty($build)) { $output = Update::run($force, true, false);
Core\Config::set('system', 'build', DB_UPDATE_VERSION);
$build = DB_UPDATE_VERSION;
}
$stored = intval($build);
$current = intval(DB_UPDATE_VERSION);
// run the pre_update_nnnn functions in update.php
for ($x = $stored; $x < $current; $x ++) {
$r = run_update_function($x, 'pre_update');
if (!$r) {
break;
}
}
$output = DBStructure::update(true, true);
// run the update_nnnn functions in update.php
for ($x = $stored; $x < $current; $x ++) {
$r = run_update_function($x, 'update');
if (!$r) {
break;
}
}
Core\Config::set('system', 'build', DB_UPDATE_VERSION);
break; break;
case "dumpsql": case "dumpsql":
ob_start(); ob_start();

View File

@ -111,12 +111,13 @@ class Lock
* *
* @param string $key Name of the lock * @param string $key Name of the lock
* @param integer $timeout Seconds until we give up * @param integer $timeout Seconds until we give up
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
* *
* @return boolean Was the lock successful? * @return boolean Was the lock successful?
*/ */
public static function acquire($key, $timeout = 120) public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{ {
return self::getDriver()->acquireLock($key, $timeout); return self::getDriver()->acquireLock($key, $timeout, $ttl);
} }
/** /**

266
src/Core/Update.php Normal file
View File

@ -0,0 +1,266 @@
<?php
namespace Friendica\Core;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
class Update
{
const SUCCESS = 0;
const FAILED = 1;
/**
* @brief Function to check if the Database structure needs an update.
*
* @param boolean $via_worker boolean Is the check run via the worker?
*/
public static function check($via_worker)
{
$build = Config::get('system', 'build');
if (empty($build)) {
Config::set('system', 'build', DB_UPDATE_VERSION - 1);
$build = DB_UPDATE_VERSION - 1;
}
// We don't support upgrading from very old versions anymore
if ($build < NEW_UPDATE_ROUTINE_VERSION) {
die('You try to update from a version prior to database version 1170. The direct upgrade path is not supported. Please update to version 3.5.4 before updating to this version.');
}
if ($build < DB_UPDATE_VERSION) {
// When we cannot execute the database update via the worker, we will do it directly
if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
self::run();
}
}
}
/**
* Automatic database updates
*
* @param bool $force Force the Update-Check even if the lock is set
* @param bool $verbose Run the Update-Check verbose
* @param bool $sendMail Sends a Mail to the administrator in case of success/failure
*
* @return string Empty string if the update is successful, error messages otherwise
*/
public static function run($force = false, $verbose = false, $sendMail = true)
{
// In force mode, we release the dbupdate lock first
// Necessary in case of an stuck update
if ($force) {
Lock::release('dbupdate');
}
$build = Config::get('system', 'build');
if (empty($build) || ($build > DB_UPDATE_VERSION)) {
$build = DB_UPDATE_VERSION - 1;
Config::set('system', 'build', $build);
}
if ($build != DB_UPDATE_VERSION) {
require_once 'update.php';
$stored = intval($build);
$current = intval(DB_UPDATE_VERSION);
if ($stored < $current) {
Config::load('database');
Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - starting', Logger::DEBUG);
// Compare the current structure with the defined structure
// If the Lock is acquired, never release it automatically to avoid double updates
if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
// run the pre_update_nnnn functions in update.php
for ($x = $stored + 1; $x <= $current; $x++) {
$r = self::runUpdateFunction($x, 'pre_update');
if (!$r) {
break;
}
}
// update the structure in one call
$retval = DBStructure::update($verbose, true);
if ($retval) {
if ($sendMail) {
self::updateFailed(
DB_UPDATE_VERSION,
$retval
);
}
Logger::log('ERROR: Update from \'' . $stored . '\' to \'' . $current . '\' - failed: ' - $retval, Logger::ALL);
Lock::release('dbupdate');
return $retval;
} else {
Config::set('database', 'last_successful_update', $current);
Config::set('database', 'last_successful_update_time', time());
Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - finished', Logger::DEBUG);
}
// run the update_nnnn functions in update.php
for ($x = $stored + 1; $x <= $current; $x++) {
$r = self::runUpdateFunction($x, 'update');
if (!$r) {
break;
}
}
Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - successful', Logger::DEBUG);
if ($sendMail) {
self::updateSuccessfull($stored, $current);
}
Lock::release('dbupdate');
}
}
}
return '';
}
/**
* Executes a specific update function
*
* @param int $x the DB version number of the function
* @param string $prefix the prefix of the function (update, pre_update)
*
* @return bool true, if the update function worked
*/
public static function runUpdateFunction($x, $prefix)
{
$funcname = $prefix . '_' . $x;
Logger::log('Update function \'' . $funcname . '\' - start', Logger::DEBUG);
if (function_exists($funcname)) {
// 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 (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
// call the specific update
$retval = $funcname();
if ($retval) {
//send the administrator an e-mail
self::updateFailed(
$x,
L10n::t('Update %s failed. See error logs.', $x)
);
Logger::log('ERROR: Update function \'' . $funcname . '\' - failed: ' . $retval, Logger::ALL);
Lock::release('dbupdate_function');
return false;
} else {
Config::set('database', 'last_successful_update_function', $funcname);
Config::set('database', 'last_successful_update_function_time', time());
if ($prefix == 'update') {
Config::set('system', 'build', $x);
}
Lock::release('dbupdate_function');
Logger::log('Update function \'' . $funcname . '\' - finished', Logger::DEBUG);
return true;
}
}
} else {
Logger::log('Skipping \'' . $funcname . '\' without executing', Logger::DEBUG);
Config::set('database', 'last_successful_update_function', $funcname);
Config::set('database', 'last_successful_update_function_time', time());
if ($prefix == 'update') {
Config::set('system', 'build', $x);
}
return true;
}
}
/**
* send the email and do what is needed to do on update fails
*
* @param int $update_id number of failed update
* @param string $error_message error message
*/
private static function updateFailed($update_id, $error_message) {
//send the administrators an e-mail
$admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
$adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
// No valid result?
if (!DBA::isResult($adminlist)) {
Logger::log(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), Logger::INFO);
// Don't continue
return;
}
// every admin could had different language
foreach ($adminlist as $admin) {
$lang = (($admin['language'])?$admin['language']:'en');
L10n::pushLang($lang);
$preamble = deindent(L10n::t("
The friendica developers released update %s recently,
but when I tried to install it, something went terribly wrong.
This needs to be fixed soon and I can't do it alone. Please contact a
friendica developer if you can not help me on your own. My database might be invalid.",
$update_id));
$body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
notification([
'uid' => $admin['uid'],
'type' => SYSTEM_EMAIL,
'to_email' => $admin['email'],
'preamble' => $preamble,
'body' => $body,
'language' => $lang]
);
L10n::popLang();
}
//try the logger
Logger::log("CRITICAL: Database structure update failed: " . $error_message);
}
private static function updateSuccessfull($from_build, $to_build)
{
//send the administrators an e-mail
$admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
$adminlist = DBA::select('user', ['uid', 'language', 'email'], ['`email` IN (%s)', $admin_mail_list]);
if (DBA::isResult($adminlist)) {
// every admin could had different language
foreach ($adminlist as $admin) {
$lang = (($admin['language']) ? $admin['language'] : 'en');
L10n::pushLang($lang);
$preamble = deindent(L10n::t("
The friendica database was successfully updated from %s to %s.",
$from_build, $to_build));
notification([
'uid' => $admin['uid'],
'type' => SYSTEM_EMAIL,
'to_email' => $admin['email'],
'preamble' => $preamble,
'body' => $preamble,
'language' => $lang]
);
L10n::popLang();
}
}
//try the logger
Logger::log("Database structure update successful.", Logger::TRACE);
}
}

View File

@ -23,6 +23,10 @@ require_once 'include/text.php';
*/ */
class DBStructure class DBStructure
{ {
const UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
const UPDATE_SUCCESSFUL = 1; // Database check was successful
const UPDATE_FAILED = 2; // Database check failed
/** /**
* Database structure definition loaded from config/dbstructure.php * Database structure definition loaded from config/dbstructure.php
* *
@ -53,58 +57,6 @@ class DBStructure
} }
} }
/*
* send the email and do what is needed to do on update fails
*
* @param update_id (int) number of failed update
* @param error_message (str) error message
*/
public static function updateFail($update_id, $error_message) {
$a = get_app();
//send the administrators an e-mail
$admin_mail_list = "'".implode("','", array_map(['Friendica\Database\DBA', 'escape'], explode(",", str_replace(" ", "", Config::get('config', 'admin_email')))))."'";
$adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)",
$admin_mail_list
);
// No valid result?
if (!DBA::isResult($adminlist)) {
Logger::log(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), Logger::INFO);
// Don't continue
return;
}
// every admin could had different language
foreach ($adminlist as $admin) {
$lang = (($admin['language'])?$admin['language']:'en');
L10n::pushLang($lang);
$preamble = deindent(L10n::t("
The friendica developers released update %s recently,
but when I tried to install it, something went terribly wrong.
This needs to be fixed soon and I can't do it alone. Please contact a
friendica developer if you can not help me on your own. My database might be invalid.",
$update_id));
$body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
notification([
'uid' => $admin['uid'],
'type' => SYSTEM_EMAIL,
'to_email' => $admin['email'],
'preamble' => $preamble,
'body' => $body,
'language' => $lang]
);
L10n::popLang();
}
//try the logger
Logger::log("CRITICAL: Database structure update failed: ".$error_message);
}
private static function tableStructure($table) { private static function tableStructure($table) {
$structures = q("DESCRIBE `%s`", $table); $structures = q("DESCRIBE `%s`", $table);
@ -535,9 +487,9 @@ class DBStructure
Config::set('system', 'maintenance_reason', ''); Config::set('system', 'maintenance_reason', '');
if ($errors) { if ($errors) {
Config::set('system', 'dbupdate', DB_UPDATE_FAILED); Config::set('system', 'dbupdate', self::UPDATE_FAILED);
} else { } else {
Config::set('system', 'dbupdate', DB_UPDATE_SUCCESSFUL); Config::set('system', 'dbupdate', self::UPDATE_SUCCESSFUL);
} }
} }

View File

@ -6,17 +6,12 @@
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Update;
class DBUpdate class DBUpdate
{ {
public static function execute() public static function execute()
{ {
$a = \Friendica\BaseObject::getApp(); Update::run();
// We are deleting the latest dbupdate entry.
// This is done to avoid endless loops because the update was interupted.
Config::delete('database', 'dbupdate_'.DB_UPDATE_VERSION);
update_db($a);
} }
} }

View File

@ -4,6 +4,7 @@ use Friendica\Core\Addon;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Update;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Contact; use Friendica\Model\Contact;
@ -65,7 +66,7 @@ function update_1179() {
// Update the central item storage with uid=0 // Update the central item storage with uid=0
Worker::add(PRIORITY_LOW, "threadupdate"); Worker::add(PRIORITY_LOW, "threadupdate");
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1181() { function update_1181() {
@ -73,7 +74,7 @@ function update_1181() {
// Fill the new fields in the term table. // Fill the new fields in the term table.
Worker::add(PRIORITY_LOW, "TagUpdate"); Worker::add(PRIORITY_LOW, "TagUpdate");
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1189() { function update_1189() {
@ -84,7 +85,7 @@ function update_1189() {
Config::delete('system','directory_submit_url'); Config::delete('system','directory_submit_url');
} }
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1191() { function update_1191() {
@ -144,7 +145,7 @@ function update_1191() {
Config::set('system', 'maintenance', 0); Config::set('system', 'maintenance', 0);
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1203() { function update_1203() {
@ -165,19 +166,19 @@ function update_1244() {
// Logged in users are forcibly logged out // Logged in users are forcibly logged out
DBA::delete('session', ['1 = 1']); DBA::delete('session', ['1 = 1']);
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1245() { function update_1245() {
$rino = Config::get('system', 'rino_encrypt'); $rino = Config::get('system', 'rino_encrypt');
if (!$rino) { if (!$rino) {
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
Config::set('system', 'rino_encrypt', 1); Config::set('system', 'rino_encrypt', 1);
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1247() { function update_1247() {
@ -226,13 +227,13 @@ function update_1260() {
SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0"); SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
Config::set('system', 'maintenance', 0); Config::set('system', 'maintenance', 0);
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1261() { function update_1261() {
// This fixes the results of an issue in the develop branch of 2018-05. // This fixes the results of an issue in the develop branch of 2018-05.
DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]); DBA::update('contact', ['blocked' => false, 'pending' => false], ['uid' => 0, 'blocked' => true, 'pending' => true]);
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1278() { function update_1278() {
@ -244,7 +245,7 @@ function update_1278() {
Config::set('system', 'maintenance', 0); Config::set('system', 'maintenance', 0);
return UPDATE_SUCCESS; return Update::SUCCESS;
} }
function update_1288() { function update_1288() {
@ -253,5 +254,5 @@ function update_1288() {
DBA::e("UPDATE `item-activity` INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id` SET `item-activity`.`uri-id` = `item`.`uri-id` WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0"); DBA::e("UPDATE `item-activity` INNER JOIN `item` ON `item`.`iaid` = `item-activity`.`id` SET `item-activity`.`uri-id` = `item`.`uri-id` WHERE `item-activity`.`uri-id` IS NULL OR `item-activity`.`uri-id` = 0");
DBA::e("UPDATE `item-content` INNER JOIN `item` ON `item`.`icid` = `item-content`.`id` SET `item-content`.`uri-id` = `item`.`uri-id` WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0"); DBA::e("UPDATE `item-content` INNER JOIN `item` ON `item`.`icid` = `item-content`.`id` SET `item-content`.`uri-id` = `item`.`uri-id` WHERE `item-content`.`uri-id` IS NULL OR `item-content`.`uri-id` = 0");
return UPDATE_SUCCESS; return Update::SUCCESS;
} }