3 namespace Friendica\Core;
5 use Friendica\Database\DBA;
6 use Friendica\Database\DBStructure;
7 use Friendica\Util\Strings;
15 * @brief Function to check if the Database structure needs an update.
17 * @param string $basePath The base path of this application
18 * @param boolean $via_worker boolean Is the check run via the worker?
19 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
21 public static function check($basePath, $via_worker)
23 if (!DBA::connected()) {
27 $build = Config::get('system', 'build');
30 Config::set('system', 'build', DB_UPDATE_VERSION - 1);
31 $build = DB_UPDATE_VERSION - 1;
34 // We don't support upgrading from very old versions anymore
35 if ($build < NEW_UPDATE_ROUTINE_VERSION) {
36 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.');
39 if ($build < DB_UPDATE_VERSION) {
41 // Calling the database update directly via the worker enables us to perform database changes to the workerqueue table itself.
42 // This is a fallback, since normally the database update will be performed by a worker job.
43 // This worker job doesn't work for changes to the "workerqueue" table itself.
46 Worker::add(PRIORITY_CRITICAL, 'DBUpdate');
52 * Automatic database updates
54 * @param string $basePath The base path of this application
55 * @param bool $force Force the Update-Check even if the database version doesn't match
56 * @param bool $override Overrides any running/stuck updates
57 * @param bool $verbose Run the Update-Check verbose
58 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
60 * @return string Empty string if the update is successful, error messages otherwise
61 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
63 public static function run($basePath, $force = false, $override = false, $verbose = false, $sendMail = true)
65 // In force mode, we release the dbupdate lock first
66 // Necessary in case of an stuck update
68 Lock::release('dbupdate', true);
71 $build = Config::get('system', 'build', null, true);
73 if (empty($build) || ($build > DB_UPDATE_VERSION)) {
74 $build = DB_UPDATE_VERSION - 1;
75 Config::set('system', 'build', $build);
78 if ($build != DB_UPDATE_VERSION || $force) {
79 require_once 'update.php';
81 $stored = intval($build);
82 $current = intval(DB_UPDATE_VERSION);
83 if ($stored < $current || $force) {
84 Config::load('database');
86 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - starting', Logger::DEBUG);
88 // Compare the current structure with the defined structure
89 // If the Lock is acquired, never release it automatically to avoid double updates
90 if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
92 // Checks if the build changed during Lock acquiring (so no double update occurs)
93 $retryBuild = Config::get('system', 'build', null, true);
94 if ($retryBuild !== $build) {
95 Lock::release('dbupdate');
99 // run the pre_update_nnnn functions in update.php
100 for ($x = $stored + 1; $x <= $current; $x++) {
101 $r = self::runUpdateFunction($x, 'pre_update');
107 // update the structure in one call
108 $retval = DBStructure::update($basePath, $verbose, true);
109 if (!empty($retval)) {
116 Logger::log('ERROR: Update from \'' . $stored . '\' to \'' . $current . '\' - failed: ' - $retval, Logger::ALL);
117 Lock::release('dbupdate');
120 Config::set('database', 'last_successful_update', $current);
121 Config::set('database', 'last_successful_update_time', time());
122 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - finished', Logger::DEBUG);
125 // run the update_nnnn functions in update.php
126 for ($x = $stored + 1; $x <= $current; $x++) {
127 $r = self::runUpdateFunction($x, 'update');
133 Logger::log('Update from \'' . $stored . '\' to \'' . $current . '\' - successful', Logger::DEBUG);
135 self::updateSuccessfull($stored, $current);
138 Lock::release('dbupdate');
147 * Executes a specific update function
149 * @param int $x the DB version number of the function
150 * @param string $prefix the prefix of the function (update, pre_update)
152 * @return bool true, if the update function worked
153 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
155 public static function runUpdateFunction($x, $prefix)
157 $funcname = $prefix . '_' . $x;
159 Logger::log('Update function \'' . $funcname . '\' - start', Logger::DEBUG);
161 if (function_exists($funcname)) {
162 // There could be a lot of processes running or about to run.
163 // We want exactly one process to run the update command.
164 // So store the fact that we're taking responsibility
165 // after first checking to see if somebody else already has.
166 // If the update fails or times-out completely you may need to
167 // delete the config entry to try again.
169 if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
171 // call the specific update
172 $retval = $funcname();
175 //send the administrator an e-mail
178 L10n::t('Update %s failed. See error logs.', $x)
180 Logger::log('ERROR: Update function \'' . $funcname . '\' - failed: ' . $retval, Logger::ALL);
181 Lock::release('dbupdate_function');
184 Config::set('database', 'last_successful_update_function', $funcname);
185 Config::set('database', 'last_successful_update_function_time', time());
187 if ($prefix == 'update') {
188 Config::set('system', 'build', $x);
191 Lock::release('dbupdate_function');
192 Logger::log('Update function \'' . $funcname . '\' - finished', Logger::DEBUG);
197 Logger::log('Skipping \'' . $funcname . '\' without executing', Logger::DEBUG);
199 Config::set('database', 'last_successful_update_function', $funcname);
200 Config::set('database', 'last_successful_update_function_time', time());
202 if ($prefix == 'update') {
203 Config::set('system', 'build', $x);
211 * send the email and do what is needed to do on update fails
213 * @param int $update_id number of failed update
214 * @param string $error_message error message
215 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217 private static function updateFailed($update_id, $error_message) {
218 //send the administrators an e-mail
219 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
220 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
223 if (!DBA::isResult($adminlist)) {
224 Logger::log(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), Logger::INFO);
232 // every admin could had different language
233 while ($admin = DBA::fetch($adminlist)) {
234 if (in_array($admin['email'], $sent)) {
237 $sent[] = $admin['email'];
239 $lang = (($admin['language'])?$admin['language']:'en');
240 L10n::pushLang($lang);
242 $preamble = Strings::deindent(L10n::t("
243 The friendica developers released update %s recently,
244 but when I tried to install it, something went terribly wrong.
245 This needs to be fixed soon and I can't do it alone. Please contact a
246 friendica developer if you can not help me on your own. My database might be invalid.",
248 $body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
251 'uid' => $admin['uid'],
252 'type' => SYSTEM_EMAIL,
253 'to_email' => $admin['email'],
254 'preamble' => $preamble,
262 Logger::log("CRITICAL: Database structure update failed: " . $error_message);
265 private static function updateSuccessfull($from_build, $to_build)
267 //send the administrators an e-mail
268 $condition = ['email' => explode(",", str_replace(" ", "", Config::get('config', 'admin_email'))), 'parent-uid' => 0];
269 $adminlist = DBA::select('user', ['uid', 'language', 'email'], $condition, ['order' => ['uid']]);
271 if (DBA::isResult($adminlist)) {
274 // every admin could had different language
275 while ($admin = DBA::fetch($adminlist)) {
276 if (in_array($admin['email'], $sent)) {
279 $sent[] = $admin['email'];
281 $lang = (($admin['language']) ? $admin['language'] : 'en');
282 L10n::pushLang($lang);
284 $preamble = Strings::deindent(L10n::t("
285 The friendica database was successfully updated from %s to %s.",
286 $from_build, $to_build));
289 'uid' => $admin['uid'],
290 'type' => SYSTEM_EMAIL,
291 'to_email' => $admin['email'],
292 'preamble' => $preamble,
301 Logger::log("Database structure update successful.", Logger::TRACE);