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 $t = Config::get('database', 'dbupdate_' . DB_UPDATE_VERSION);
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 Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, time());
45 // update the structure in one call
46 $retval = DBStructure::update(false, true);
48 DBStructure::updateFail(
54 Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
57 // run the update_nnnn functions in update.php
58 for ($x = $stored + 1; $x <= $current; $x++) {
59 $r = self::runUpdateFunction($x, 'update');
69 * Executes a specific update function
71 * @param int $x the DB version number of the function
72 * @param string $prefix the prefix of the function (update, pre_update)
74 * @return bool true, if the update function worked
76 public static function runUpdateFunction($x, $prefix)
78 $funcname = $prefix . '_' . $x;
80 if (function_exists($funcname)) {
81 // There could be a lot of processes running or about to run.
82 // We want exactly one process to run the update command.
83 // So store the fact that we're taking responsibility
84 // after first checking to see if somebody else already has.
85 // If the update fails or times-out completely you may need to
86 // delete the config entry to try again.
88 $t = Config::get('database', $funcname);
92 Config::set('database', $funcname, time());
94 // call the specific update
95 $retval = $funcname();
98 //send the administrator an e-mail
99 DBStructure::updateFail(
101 L10n::t('Update %s failed. See error logs.', $x)
105 Config::set('database', $funcname, 'success');
107 if ($prefix == 'update') {
108 Config::set('system', 'build', $x);
114 Config::set('database', $funcname, 'success');
116 if ($prefix == 'update') {
117 Config::set('system', 'build', $x);