3 namespace Friendica\Core;
5 use Friendica\Database\DBStructure;
10 * Automatic database updates
12 public static function run()
14 $build = Config::get('system', 'build');
16 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
17 $build = DB_UPDATE_VERSION - 1;
18 Config::set('system', 'build', $build);
21 if ($build != DB_UPDATE_VERSION) {
22 require_once 'update.php';
24 $stored = intval($build);
25 $current = intval(DB_UPDATE_VERSION);
26 if ($stored < $current) {
27 Config::load('database');
29 // Compare the current structure with the defined structure
30 if (Lock::acquire('dbupdate')) {
32 // run the pre_update_nnnn functions in update.php
33 for ($x = $stored + 1; $x <= $current; $x++) {
34 $r = self::runUpdateFunction($x, 'pre_update');
40 // update the structure in one call
41 $retval = DBStructure::update(false, true);
43 DBStructure::updateFail(
47 Lock::release('dbupdate');
50 Config::set('database', 'last_successful_update', time());
53 // run the update_nnnn functions in update.php
54 for ($x = $stored + 1; $x <= $current; $x++) {
55 $r = self::runUpdateFunction($x, 'update');
61 Lock::release('dbupdate');
68 * Executes a specific update function
70 * @param int $x the DB version number of the function
71 * @param string $prefix the prefix of the function (update, pre_update)
73 * @return bool true, if the update function worked
75 public static function runUpdateFunction($x, $prefix)
77 $funcname = $prefix . '_' . $x;
79 if (function_exists($funcname)) {
80 // There could be a lot of processes running or about to run.
81 // We want exactly one process to run the update command.
82 // So store the fact that we're taking responsibility
83 // after first checking to see if somebody else already has.
84 // If the update fails or times-out completely you may need to
85 // delete the config entry to try again.
87 if (Lock::acquire('dbupdate_function')) {
89 // call the specific update
90 $retval = $funcname();
93 //send the administrator an e-mail
94 DBStructure::updateFail(
96 L10n::t('Update %s failed. See error logs.', $x)
98 Lock::release('dbupdate_function');
101 Config::set('database', 'last_successful_update_function', $funcname);
102 Config::set('database', 'last_successful_update_function_time', time());
104 if ($prefix == 'update') {
105 Config::set('system', 'build', $x);
108 Lock::release('dbupdate_function');
113 logger('Skipping \'' . $funcname . '\' without executing', LOGGER_DEBUG);
115 Config::set('database', 'last_successful_update_function', $funcname);
116 Config::set('database', 'last_successful_update_function_time', time());
118 if ($prefix == 'update') {
119 Config::set('system', 'build', $x);