3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Database;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Logger;
28 use Friendica\Util\DateTimeFormat;
30 require_once __DIR__ . '/../../include/dba.php';
33 * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
37 const UPDATE_NOT_CHECKED = 0; // Database check wasn't executed before
38 const UPDATE_SUCCESSFUL = 1; // Database check was successful
39 const UPDATE_FAILED = 2; // Database check failed
41 const RENAME_COLUMN = 0;
42 const RENAME_PRIMARY_KEY = 1;
45 * Database structure definition loaded from config/dbstructure.config.php
49 private static $definition = [];
52 * Converts all tables from MyISAM to InnoDB
54 public static function convertToInnoDB()
56 $tables = DBA::selectToArray(
57 ['information_schema' => 'tables'],
59 ['engine' => 'MyISAM', 'table_schema' => DBA::databaseName()]
62 if (!DBA::isResult($tables)) {
63 echo DI::l10n()->t('There are no tables on MyISAM.') . "\n";
67 foreach ($tables AS $table) {
68 $sql = "ALTER TABLE " . DBA::quoteIdentifier($table['table_name']) . " engine=InnoDB;";
71 $result = DBA::e($sql);
72 if (!DBA::isResult($result)) {
73 self::printUpdateError($sql);
79 * Print out database error messages
81 * @param string $message Message to be added to the error message
83 * @return string Error message
85 private static function printUpdateError($message)
87 echo DI::l10n()->t("\nError %d occurred during database update:\n%s\n",
88 DBA::errorNo(), DBA::errorMessage());
90 return DI::l10n()->t('Errors encountered performing database changes: ') . $message . EOL;
93 public static function printStructure($basePath)
95 $database = self::definition($basePath, false);
97 echo "-- ------------------------------------------\n";
98 echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n";
99 echo "-- DB_UPDATE_VERSION " . DB_UPDATE_VERSION . "\n";
100 echo "-- ------------------------------------------\n\n\n";
101 foreach ($database AS $name => $structure) {
103 echo "-- TABLE $name\n";
105 self::createTable($name, $structure, true, false);
112 * Loads the database structure definition from the config/dbstructure.config.php file.
113 * On first pass, defines DB_UPDATE_VERSION constant.
115 * @see static/dbstructure.config.php
116 * @param boolean $with_addons_structure Whether to tack on addons additional tables
117 * @param string $basePath The base path of this application
121 public static function definition($basePath, $with_addons_structure = true)
123 if (!self::$definition) {
125 $filename = $basePath . '/static/dbstructure.config.php';
127 if (!is_readable($filename)) {
128 throw new Exception('Missing database structure config file static/dbstructure.config.php');
131 $definition = require $filename;
134 throw new Exception('Corrupted database structure config file static/dbstructure.config.php');
137 self::$definition = $definition;
139 $definition = self::$definition;
142 if ($with_addons_structure) {
143 Hook::callAll('dbstructure_definition', $definition);
149 private static function createTable($name, $structure, $verbose, $action)
157 foreach ($structure["fields"] AS $fieldname => $field) {
158 $sql_rows[] = "`" . DBA::escape($fieldname) . "` " . self::FieldCommand($field);
159 if (!empty($field['primary'])) {
160 $primary_keys[] = $fieldname;
164 if (!empty($structure["indexes"])) {
165 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
166 $sql_index = self::createIndex($indexname, $fieldnames, "");
167 if (!is_null($sql_index)) {
168 $sql_rows[] = $sql_index;
173 if (isset($structure["engine"])) {
174 $engine = " ENGINE=" . $structure["engine"];
177 if (isset($structure["comment"])) {
178 $comment = " COMMENT='" . DBA::escape($structure["comment"]) . "'";
181 $sql = implode(",\n\t", $sql_rows);
183 $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", DBA::escape($name)) . $sql .
184 "\n)" . $engine . " DEFAULT COLLATE utf8mb4_general_ci" . $comment;
196 private static function FieldCommand($parameters, $create = true)
198 $fieldstruct = $parameters["type"];
200 if (isset($parameters["Collation"])) {
201 $fieldstruct .= " COLLATE " . $parameters["Collation"];
204 if (isset($parameters["not null"])) {
205 $fieldstruct .= " NOT NULL";
208 if (isset($parameters["default"])) {
209 if (strpos(strtolower($parameters["type"]), "int") !== false) {
210 $fieldstruct .= " DEFAULT " . $parameters["default"];
212 $fieldstruct .= " DEFAULT '" . $parameters["default"] . "'";
215 if (isset($parameters["extra"])) {
216 $fieldstruct .= " " . $parameters["extra"];
219 if (isset($parameters["comment"])) {
220 $fieldstruct .= " COMMENT '" . DBA::escape($parameters["comment"]) . "'";
223 /*if (($parameters["primary"] != "") && $create)
224 $fieldstruct .= " PRIMARY KEY";*/
226 return ($fieldstruct);
229 private static function createIndex($indexname, $fieldnames, $method = "ADD")
231 $method = strtoupper(trim($method));
232 if ($method != "" && $method != "ADD") {
233 throw new Exception("Invalid parameter 'method' in self::createIndex(): '$method'");
236 if (in_array($fieldnames[0], ["UNIQUE", "FULLTEXT"])) {
237 $index_type = array_shift($fieldnames);
238 $method .= " " . $index_type;
242 foreach ($fieldnames AS $fieldname) {
247 if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
248 $names .= "`" . DBA::escape($matches[1]) . "`(" . intval($matches[2]) . ")";
250 $names .= "`" . DBA::escape($fieldname) . "`";
254 if ($indexname == "PRIMARY") {
255 return sprintf("%s PRIMARY KEY(%s)", $method, $names);
259 $sql = sprintf("%s INDEX `%s` (%s)", $method, DBA::escape($indexname), $names);
264 * Updates DB structure and returns eventual errors messages
266 * @param string $basePath The base path of this application
267 * @param bool $verbose
268 * @param bool $action Whether to actually apply the update
269 * @param bool $install Is this the initial update during the installation?
270 * @param array $tables An array of the database tables
271 * @param array $definition An array of the definition tables
272 * @return string Empty string if the update is successful, error messages otherwise
275 public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
277 if ($action && !$install) {
278 DI::config()->set('system', 'maintenance', 1);
279 DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: Database update', DateTimeFormat::utcNow() . ' ' . date('e')));
284 Logger::log('updating structure', Logger::DEBUG);
286 // Get the current structure
289 if (is_null($tables)) {
290 $tables = q("SHOW TABLES");
293 if (DBA::isResult($tables)) {
294 foreach ($tables AS $table) {
295 $table = current($table);
297 Logger::log(sprintf('updating structure for table %s ...', $table), Logger::DEBUG);
298 $database[$table] = self::tableStructure($table);
302 // Get the definition
303 if (is_null($definition)) {
304 $definition = self::definition($basePath);
307 // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
308 if ((version_compare(DBA::serverInfo(), '5.7.4') >= 0) &&
309 !(strpos(DBA::serverInfo(), 'MariaDB') !== false)) {
316 foreach ($definition AS $name => $structure) {
317 $is_new_table = false;
322 if (!isset($database[$name])) {
323 $r = self::createTable($name, $structure, $verbose, $action);
324 if (!DBA::isResult($r)) {
325 $errors .= self::printUpdateError($name);
327 $is_new_table = true;
329 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
330 if (isset($database[$name]["indexes"][$indexname])) {
331 $current_index_definition = implode(",", $database[$name]["indexes"][$indexname]);
333 $current_index_definition = "__NOT_SET__";
335 $new_index_definition = implode(",", $fieldnames);
336 if ($current_index_definition != $new_index_definition) {
337 if ($fieldnames[0] == "UNIQUE") {
340 $temp_name = "temp-" . $name;
347 * Drop the index if it isn't present in the definition
348 * or the definition differ from current status
349 * and index name doesn't start with "local_"
351 foreach ($database[$name]["indexes"] as $indexname => $fieldnames) {
352 $current_index_definition = implode(",", $fieldnames);
353 if (isset($structure["indexes"][$indexname])) {
354 $new_index_definition = implode(",", $structure["indexes"][$indexname]);
356 $new_index_definition = "__NOT_SET__";
358 if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
359 $sql2 = self::dropIndex($indexname);
361 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
363 $sql3 .= ", " . $sql2;
367 // Compare the field structure field by field
368 foreach ($structure["fields"] AS $fieldname => $parameters) {
369 if (!isset($database[$name]["fields"][$fieldname])) {
370 $sql2 = self::addTableField($fieldname, $parameters);
372 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
374 $sql3 .= ", " . $sql2;
377 // Compare the field definition
378 $field_definition = $database[$name]["fields"][$fieldname];
380 // Remove the relation data that is used for the referential integrity
381 unset($parameters['relation']);
383 // We change the collation after the indexes had been changed.
384 // This is done to avoid index length problems.
385 // So here we always ensure that there is no need to change it.
386 unset($parameters['Collation']);
387 unset($field_definition['Collation']);
389 // Only update the comment when it is defined
390 if (!isset($parameters['comment'])) {
391 $parameters['comment'] = "";
394 $current_field_definition = DBA::cleanQuery(implode(",", $field_definition));
395 $new_field_definition = DBA::cleanQuery(implode(",", $parameters));
396 if ($current_field_definition != $new_field_definition) {
397 $sql2 = self::modifyTableField($fieldname, $parameters);
399 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
401 $sql3 .= ", " . $sql2;
409 * Create the index if the index don't exists in database
410 * or the definition differ from the current status.
411 * Don't create keys if table is new
413 if (!$is_new_table) {
414 foreach ($structure["indexes"] AS $indexname => $fieldnames) {
415 if (isset($database[$name]["indexes"][$indexname])) {
416 $current_index_definition = implode(",", $database[$name]["indexes"][$indexname]);
418 $current_index_definition = "__NOT_SET__";
420 $new_index_definition = implode(",", $fieldnames);
421 if ($current_index_definition != $new_index_definition) {
422 $sql2 = self::createIndex($indexname, $fieldnames);
424 // Fetch the "group by" fields for unique indexes
425 $group_by = self::groupBy($fieldnames);
428 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
430 $sql3 .= ", " . $sql2;
436 if (isset($database[$name]["table_status"]["Comment"])) {
437 $structurecomment = $structure["comment"] ?? '';
438 if ($database[$name]["table_status"]["Comment"] != $structurecomment) {
439 $sql2 = "COMMENT = '" . DBA::escape($structurecomment) . "'";
442 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
444 $sql3 .= ", " . $sql2;
449 if (isset($database[$name]["table_status"]["Engine"]) && isset($structure['engine'])) {
450 if ($database[$name]["table_status"]["Engine"] != $structure['engine']) {
451 $sql2 = "ENGINE = '" . DBA::escape($structure['engine']) . "'";
454 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
456 $sql3 .= ", " . $sql2;
461 if (isset($database[$name]["table_status"]["Collation"])) {
462 if ($database[$name]["table_status"]["Collation"] != 'utf8mb4_general_ci') {
463 $sql2 = "DEFAULT COLLATE utf8mb4_general_ci";
466 $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
468 $sql3 .= ", " . $sql2;
477 // Now have a look at the field collations
478 // Compare the field structure field by field
479 foreach ($structure["fields"] AS $fieldname => $parameters) {
480 // Compare the field definition
481 $field_definition = ($database[$name]["fields"][$fieldname] ?? '') ?: ['Collation' => ''];
483 // Define the default collation if not given
484 if (!isset($parameters['Collation']) && !empty($field_definition['Collation'])) {
485 $parameters['Collation'] = 'utf8mb4_general_ci';
487 $parameters['Collation'] = null;
490 if ($field_definition['Collation'] != $parameters['Collation']) {
491 $sql2 = self::modifyTableField($fieldname, $parameters);
492 if (($sql3 == "") || (substr($sql3, -2, 2) == "; ")) {
493 $sql3 .= "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
495 $sql3 .= ", " . $sql2;
502 if (substr($sql3, -2, 2) != "; ") {
507 if ($is_unique && $ignore == '') {
508 foreach ($database[$name]["fields"] AS $fieldname => $parameters) {
509 $field_list .= 'ANY_VALUE(`' . $fieldname . '`),';
511 $field_list = rtrim($field_list, ',');
515 // Ensure index conversion to unique removes duplicates
516 if ($is_unique && ($temp_name != $name)) {
518 echo "SET session old_alter_table=1;\n";
520 echo "DROP TABLE IF EXISTS `" . $temp_name . "`;\n";
521 echo "CREATE TABLE `" . $temp_name . "` LIKE `" . $name . "`;\n";
527 if ($is_unique && ($temp_name != $name)) {
529 echo "SET session old_alter_table=0;\n";
531 echo "INSERT INTO `" . $temp_name . "` SELECT " . DBA::anyValueFallback($field_list) . " FROM `" . $name . "`" . $group_by . ";\n";
532 echo "DROP TABLE `" . $name . "`;\n";
533 echo "RENAME TABLE `" . $temp_name . "` TO `" . $name . "`;\n";
540 DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: updating %s table.', DateTimeFormat::utcNow() . ' ' . date('e'), $name));
543 // Ensure index conversion to unique removes duplicates
544 if ($is_unique && ($temp_name != $name)) {
546 DBA::e("SET session old_alter_table=1;");
548 $r = DBA::e("DROP TABLE IF EXISTS `" . $temp_name . "`;");
549 if (!DBA::isResult($r)) {
550 $errors .= self::printUpdateError($sql3);
554 $r = DBA::e("CREATE TABLE `" . $temp_name . "` LIKE `" . $name . "`;");
555 if (!DBA::isResult($r)) {
556 $errors .= self::printUpdateError($sql3);
563 if (!DBA::isResult($r)) {
564 $errors .= self::printUpdateError($sql3);
566 if ($is_unique && ($temp_name != $name)) {
568 DBA::e("SET session old_alter_table=0;");
570 $r = DBA::e("INSERT INTO `" . $temp_name . "` SELECT " . $field_list . " FROM `" . $name . "`" . $group_by . ";");
571 if (!DBA::isResult($r)) {
572 $errors .= self::printUpdateError($sql3);
575 $r = DBA::e("DROP TABLE `" . $name . "`;");
576 if (!DBA::isResult($r)) {
577 $errors .= self::printUpdateError($sql3);
580 $r = DBA::e("RENAME TABLE `" . $temp_name . "` TO `" . $name . "`;");
581 if (!DBA::isResult($r)) {
582 $errors .= self::printUpdateError($sql3);
591 if ($action && !$install) {
592 DI::config()->set('system', 'maintenance', 0);
593 DI::config()->set('system', 'maintenance_reason', '');
596 DI::config()->set('system', 'dbupdate', self::UPDATE_FAILED);
598 DI::config()->set('system', 'dbupdate', self::UPDATE_SUCCESSFUL);
605 private static function tableStructure($table)
607 $structures = q("DESCRIBE `%s`", $table);
609 $full_columns = q("SHOW FULL COLUMNS FROM `%s`", $table);
611 $indexes = q("SHOW INDEX FROM `%s`", $table);
613 $table_status = q("SHOW TABLE STATUS WHERE `name` = '%s'", $table);
615 if (DBA::isResult($table_status)) {
616 $table_status = $table_status[0];
624 if (DBA::isResult($indexes)) {
625 foreach ($indexes AS $index) {
626 if ($index["Key_name"] != "PRIMARY" && $index["Non_unique"] == "0" && !isset($indexdata[$index["Key_name"]])) {
627 $indexdata[$index["Key_name"]] = ["UNIQUE"];
630 if ($index["Index_type"] == "FULLTEXT" && !isset($indexdata[$index["Key_name"]])) {
631 $indexdata[$index["Key_name"]] = ["FULLTEXT"];
634 $column = $index["Column_name"];
636 if ($index["Sub_part"] != "") {
637 $column .= "(" . $index["Sub_part"] . ")";
640 $indexdata[$index["Key_name"]][] = $column;
643 if (DBA::isResult($structures)) {
644 foreach ($structures AS $field) {
645 // Replace the default size values so that we don't have to define them
646 $search = ['tinyint(1)', 'tinyint(3) unsigned', 'tinyint(4)', 'smallint(5) unsigned', 'smallint(6)', 'mediumint(8) unsigned', 'mediumint(9)', 'bigint(20)', 'int(10) unsigned', 'int(11)'];
647 $replace = ['boolean', 'tinyint unsigned', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint unsigned', 'mediumint', 'bigint', 'int unsigned', 'int'];
648 $field["Type"] = str_replace($search, $replace, $field["Type"]);
650 $fielddata[$field["Field"]]["type"] = $field["Type"];
651 if ($field["Null"] == "NO") {
652 $fielddata[$field["Field"]]["not null"] = true;
655 if (isset($field["Default"])) {
656 $fielddata[$field["Field"]]["default"] = $field["Default"];
659 if ($field["Extra"] != "") {
660 $fielddata[$field["Field"]]["extra"] = $field["Extra"];
663 if ($field["Key"] == "PRI") {
664 $fielddata[$field["Field"]]["primary"] = true;
668 if (DBA::isResult($full_columns)) {
669 foreach ($full_columns AS $column) {
670 $fielddata[$column["Field"]]["Collation"] = $column["Collation"];
671 $fielddata[$column["Field"]]["comment"] = $column["Comment"];
675 return ["fields" => $fielddata, "indexes" => $indexdata, "table_status" => $table_status];
678 private static function dropIndex($indexname)
680 $sql = sprintf("DROP INDEX `%s`", DBA::escape($indexname));
684 private static function addTableField($fieldname, $parameters)
686 $sql = sprintf("ADD `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters));
690 private static function modifyTableField($fieldname, $parameters)
692 $sql = sprintf("MODIFY `%s` %s", DBA::escape($fieldname), self::FieldCommand($parameters, false));
697 * Constructs a GROUP BY clause from a UNIQUE index definition.
699 * @param array $fieldnames
702 private static function groupBy(array $fieldnames)
704 if ($fieldnames[0] != "UNIQUE") {
708 array_shift($fieldnames);
711 foreach ($fieldnames AS $fieldname) {
716 if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
717 $names .= "`" . DBA::escape($matches[1]) . "`";
719 $names .= "`" . DBA::escape($fieldname) . "`";
723 $sql = sprintf(" GROUP BY %s", $names);
728 * Renames columns or the primary key of a table
730 * @todo You cannot rename a primary key if "auto increment" is set
732 * @param string $table Table name
733 * @param array $columns Columns Syntax for Rename: [ $old1 => [ $new1, $type1 ], $old2 => [ $new2, $type2 ], ... ]
734 * Syntax for Primary Key: [ $col1, $col2, ...]
735 * @param int $type The type of renaming (Default is Column)
737 * @return boolean Was the renaming successful?
740 public static function rename($table, $columns, $type = self::RENAME_COLUMN)
742 if (empty($table) || empty($columns)) {
746 if (!is_array($columns)) {
750 $table = DBA::escape($table);
752 $sql = "ALTER TABLE `" . $table . "`";
754 case self::RENAME_COLUMN:
755 if (!self::existsColumn($table, array_keys($columns))) {
758 $sql .= implode(',', array_map(
759 function ($to, $from) {
760 return " CHANGE `" . $from . "` `" . $to[0] . "` " . $to[1];
766 case self::RENAME_PRIMARY_KEY:
767 if (!self::existsColumn($table, $columns)) {
770 $sql .= " DROP PRIMARY KEY, ADD PRIMARY KEY(`" . implode('`, `', $columns) . "`)";
778 $stmt = DBA::p($sql);
780 if (is_bool($stmt)) {
792 * Check if the columns of the table exists
794 * @param string $table Table name
795 * @param array $columns Columns to check ( Syntax: [ $col1, $col2, .. ] )
797 * @return boolean Does the table exist?
800 public static function existsColumn($table, $columns = [])
806 if (is_null($columns) || empty($columns)) {
807 return self::existsTable($table);
810 $table = DBA::escape($table);
812 foreach ($columns AS $column) {
813 $sql = "SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "';";
815 $stmt = DBA::p($sql);
817 if (is_bool($stmt)) {
820 $retval = (DBA::numRows($stmt) > 0);
834 * Check if a table exists
836 * @param string|array $table Table name
838 * @return boolean Does the table exist?
841 public static function existsTable($table)
847 if (is_array($table)) {
848 $condition = ['table_schema' => key($table), 'table_name' => current($table)];
850 $condition = ['table_schema' => DBA::databaseName(), 'table_name' => $table];
853 $result = DBA::exists(['information_schema' => 'tables'], $condition);
859 * Returns the columns of a table
861 * @param string $table Table name
863 * @return array An array of the table columns
866 public static function getColumns($table)
868 $stmtColumns = DBA::p("SHOW COLUMNS FROM `" . $table . "`");
869 return DBA::toArray($stmtColumns);