]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Resubmit all profiles on directory change
[friendica.git] / include / dbstructure.php
index 15e16b34f58c69213e0c0f8f2178bb625687a7ad..59f19e9bbc5be0e2c1c10764adb14938677c8651 100644 (file)
@@ -158,12 +158,27 @@ function print_structure($database) {
        }
 }
 
+/**
+ * @brief Print out database error messages
+ *
+ * @param object $db Database object
+ * @param string $message Message to be added to the error message
+ *
+ * @return string Error message
+ */
+function print_update_error($db, $message) {
+       echo sprintf(t("\nError %d occured during database update:\n%s\n"),
+               $db->errorno, $db->error);
+
+       return t('Errors encountered performing database changes: ').$message.EOL;
+}
+
 function update_structure($verbose, $action, $tables=null, $definition=null) {
        global $a, $db;
 
        if ($action) {
                Config::set('system', 'maintenance', 1);
-               Config::set('system', 'maintenance_reason', 'Database update');
+               Config::set('system', 'maintenance_reason', sprintf(t(': Database update'), dbm::date().' '.date('e')));
        }
 
        $errors = false;
@@ -207,7 +222,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                if (!isset($database[$name])) {
                        $r = db_create_table($name, $structure["fields"], $verbose, $action, $structure['indexes']);
                        if (!dbm::is_result($r)) {
-                               $errors .=  t('Errors encountered creating database tables.').$name.EOL;
+                               $errors .= print_update_error($db, $name);
                        }
                        $is_new_table = True;
                } else {
@@ -263,16 +278,19 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                        }
                                } else {
                                        // Compare the field definition
-                                       // At first remove the collation from the array that is about to be compared
                                        $field_definition = $database[$name]["fields"][$fieldname];
-                                       $collation = $field_definition['Collation'];
-                                       unset($field_definition['Collation']);
+
+                                       // Define the default collation if not given
+                                       if (!isset($parameters['Collation']) AND !is_null($field_definition['Collation'])) {
+                                               $parameters['Collation'] = 'utf8mb4_general_ci';
+                                       } else {
+                                               $parameters['Collation'] = null;
+                                       }
 
                                        $current_field_definition = implode(",", $field_definition);
                                        $new_field_definition = implode(",", $parameters);
-                                       if (($current_field_definition != $new_field_definition) OR
-                                               (!is_null($collation) AND ($collation != 'utf8mb4_general_ci'))) {
-                                               $sql2 = db_modify_table_field($fieldname, $parameters, $collation);
+                                       if ($current_field_definition != $new_field_definition) {
+                                               $sql2 = db_modify_table_field($fieldname, $parameters);
                                                if ($sql3 == "") {
                                                        $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
                                                } else {
@@ -315,7 +333,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
 
                        if (isset($database[$name]["table_status"]["Collation"])) {
                                if ($database[$name]["table_status"]["Collation"] != 'utf8mb4_general_ci') {
-                                       $sql2 = "DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
+                                       $sql2 = "DEFAULT COLLATE utf8mb4_general_ci";
 
                                        if ($sql3 == "") {
                                                $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
@@ -352,6 +370,8 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                        }
 
                        if ($action) {
+                               Config::set('system', 'maintenance_reason', sprintf(t('%s: updating %s table.'), dbm::date().' '.date('e'), $name));
+
                                // Ensure index conversion to unique removes duplicates
                                if ($is_unique) {
                                        if ($ignore != "") {
@@ -359,33 +379,33 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                        } else {
                                                $r = $db->q("CREATE TABLE `".$temp_name."` LIKE `".$name."`;");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= t('Errors encountered performing database changes.').$sql3.EOL;
+                                                       $errors .= print_update_error($db, $sql3);
                                                        return $errors;
                                                }
                                        }
                                }
 
                                $r = @$db->q($sql3);
-                               if (!dbm::is_result($r))
-                                       $errors .= t('Errors encountered performing database changes.').$sql3.EOL;
-
+                               if (!dbm::is_result($r)) {
+                                       $errors .= print_update_error($db, $sql3);
+                               }
                                if ($is_unique) {
                                        if ($ignore != "") {
                                                $db->q("SET session old_alter_table=0;");
                                        } else {
                                                $r = $db->q("INSERT INTO `".$temp_name."` SELECT * FROM `".$name."`".$group_by.";");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= t('Errors encountered performing database changes.').$sql3.EOL;
+                                                       $errors .= print_update_error($db, $sql3);
                                                        return $errors;
                                                }
                                                $r = $db->q("DROP TABLE `".$name."`;");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= t('Errors encountered performing database changes.').$sql3.EOL;
+                                                       $errors .= print_update_error($db, $sql3);
                                                        return $errors;
                                                }
                                                $r = $db->q("RENAME TABLE `".$temp_name."` TO `".$name."`;");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= t('Errors encountered performing database changes.').$sql3.EOL;
+                                                       $errors .= print_update_error($db, $sql3);
                                                        return $errors;
                                                }
                                        }
@@ -405,6 +425,10 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
 function db_field_command($parameters, $create = true) {
        $fieldstruct = $parameters["type"];
 
+       if (!is_null($parameters["Collation"])) {
+               $fieldstruct .= " COLLATE ".$parameters["Collation"];
+       }
+
        if ($parameters["not null"])
                $fieldstruct .= " NOT NULL";
 
@@ -449,7 +473,7 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
 
        $sql = implode(",\n\t", $sql_rows);
 
-       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8mb4";
+       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT COLLATE utf8mb4_general_ci";
        if ($verbose)
                echo $sql.";\n";
 
@@ -464,13 +488,8 @@ function db_add_table_field($fieldname, $parameters) {
        return($sql);
 }
 
-function db_modify_table_field($fieldname, $parameters, $collation) {
+function db_modify_table_field($fieldname, $parameters) {
        $sql = sprintf("MODIFY `%s` %s", dbesc($fieldname), db_field_command($parameters, false));
-
-       if (!is_null($collation) AND ($collation != 'utf8mb4_general_ci')) {
-               $sql .= " CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
-       }
-
        return($sql);
 }
 
@@ -694,7 +713,7 @@ function db_definition() {
                                        "writable" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "forum" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "prv" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
-                                       "contact-type" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
+                                       "contact-type" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
                                        "hidden" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "archive" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "pending" => array("type" => "tinyint(1)", "not null" => "1", "default" => "1"),
@@ -704,7 +723,7 @@ function db_definition() {
                                        "info" => array("type" => "mediumtext"),
                                        "profile-id" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
                                        "bdyear" => array("type" => "varchar(4)", "not null" => "1", "default" => ""),
-                                       "bd" => array("type" => "date", "not null" => "1", "default" => "0000-00-00"),
+                                       "bd" => array("type" => "date", "not null" => "1", "default" => "0001-01-01"),
                                        "notify_new_posts" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "fetch_further_information" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "ffi_keyword_blacklist" => array("type" => "text"),
@@ -863,7 +882,7 @@ function db_definition() {
                                        "about" => array("type" => "text"),
                                        "keywords" => array("type" => "text"),
                                        "gender" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
-                                       "birthday" => array("type" => "varchar(32)", "not null" => "1", "default" => "0000-00-00"),
+                                       "birthday" => array("type" => "varchar(32)", "not null" => "1", "default" => "0001-01-01"),
                                        "community" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "contact-type" => array("type" => "tinyint(1)", "not null" => "1", "default" => "-1"),
                                        "hide" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
@@ -1327,7 +1346,7 @@ function db_definition() {
                                        "hide-friends" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "pdesc" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "dob" => array("type" => "varchar(32)", "not null" => "1", "default" => "0000-00-00"),
+                                       "dob" => array("type" => "varchar(32)", "not null" => "1", "default" => "0001-01-01"),
                                        "address" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "locality" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "region" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),