3 namespace Friendica\Core;
5 use Friendica\Database\DBStructure;
13 * Automatic database updates
15 public static function run()
17 $build = Config::get('system', 'build');
19 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
20 $build = DB_UPDATE_VERSION - 1;
21 Config::set('system', 'build', $build);
24 if ($build != DB_UPDATE_VERSION) {
25 require_once 'update.php';
27 $stored = intval($build);
28 $current = intval(DB_UPDATE_VERSION);
29 if ($stored < $current) {
30 Config::load('database');
32 // Compare the current structure with the defined structure
33 if (Lock::acquire('dbupdate')) {
35 // run the pre_update_nnnn functions in update.php
36 for ($x = $stored + 1; $x <= $current; $x++) {
37 $r = self::runUpdateFunction($x, 'pre_update');
43 // update the structure in one call
44 $retval = DBStructure::update(false, true);
46 DBStructure::updateFail(
50 Lock::release('dbupdate');
53 Config::set('database', 'last_successful_update', $current);
54 Config::set('database', 'last_successful_update_time', time());
57 // run the update_nnnn functions in update.php
58 for ($x = $stored + 1; $x <= $current; $x++) {
59 $r = self::runUpdateFunction($x, 'update');
65 Lock::release('dbupdate');
72 * Executes a specific update function
74 * @param int $x the DB version number of the function
75 * @param string $prefix the prefix of the function (update, pre_update)
77 * @return bool true, if the update function worked
79 public static function runUpdateFunction($x, $prefix)
81 $funcname = $prefix . '_' . $x;
83 if (function_exists($funcname)) {
84 // There could be a lot of processes running or about to run.
85 // We want exactly one process to run the update command.
86 // So store the fact that we're taking responsibility
87 // after first checking to see if somebody else already has.
88 // If the update fails or times-out completely you may need to
89 // delete the config entry to try again.
91 if (Lock::acquire('dbupdate_function')) {
93 // call the specific update
94 $retval = $funcname();
97 //send the administrator an e-mail
98 DBStructure::updateFail(
100 L10n::t('Update %s failed. See error logs.', $x)
102 Lock::release('dbupdate_function');
105 Config::set('database', 'last_successful_update_function', $funcname);
106 Config::set('database', 'last_successful_update_function_time', time());
108 if ($prefix == 'update') {
109 Config::set('system', 'build', $x);
112 Lock::release('dbupdate_function');
117 logger('Skipping \'' . $funcname . '\' without executing', LOGGER_DEBUG);
119 Config::set('database', 'last_successful_update_function', $funcname);
120 Config::set('database', 'last_successful_update_function_time', time());
122 if ($prefix == 'update') {
123 Config::set('system', 'build', $x);