]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Merge pull request #3323 from Alkarex/ostatus-only_full_group_by
[friendica.git] / include / dbstructure.php
index 406ad6c93da52140e2a6659850aa66f69eccb35e..373d6ddb00ec1113ddf2def87cfd8191998cfc24 100644 (file)
@@ -78,8 +78,18 @@ function update_fail($update_id, $error_message){
 function table_structure($table) {
        $structures = q("DESCRIBE `%s`", $table);
 
+       $full_columns = q("SHOW FULL COLUMNS FROM `%s`", $table);
+
        $indexes = q("SHOW INDEX FROM `%s`", $table);
 
+       $table_status = q("SHOW TABLE STATUS WHERE `name` = '%s'", $table);
+
+       if (dbm::is_result($table_status)) {
+               $table_status = $table_status[0];
+       } else {
+               $table_status = array();
+       }
+
        $fielddata = array();
        $indexdata = array();
 
@@ -104,7 +114,6 @@ function table_structure($table) {
 
                        $indexdata[$index["Key_name"]][] = $column;
                }
-
        if (dbm::is_result($structures)) {
                foreach ($structures AS $field) {
                        $fielddata[$field["Field"]]["type"] = $field["Type"];
@@ -125,10 +134,16 @@ function table_structure($table) {
                        }
                }
        }
-       return(array("fields"=>$fielddata, "indexes"=>$indexdata));
+       if (dbm::is_result($full_columns)) {
+               foreach ($full_columns AS $column) {
+                       $fielddata[$column["Field"]]["Collation"] = $column["Collation"];
+               }
+       }
+
+       return array("fields" => $fielddata, "indexes" => $indexdata, "table_status" => $table_status);
 }
 
-function print_structure($database, $charset) {
+function print_structure($database) {
        echo "-- ------------------------------------------\n";
        echo "-- ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION." (".FRIENDICA_CODENAME,")\n";
        echo "-- DB_UPDATE_VERSION ".DB_UPDATE_VERSION."\n";
@@ -137,7 +152,7 @@ function print_structure($database, $charset) {
                echo "--\n";
                echo "-- TABLE $name\n";
                echo "--\n";
-               db_create_table($name, $structure['fields'], $charset, true, false, $structure["indexes"]);
+               db_create_table($name, $structure['fields'], true, false, $structure["indexes"]);
 
                echo "\n";
        }
@@ -148,13 +163,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
 
        if ($action) {
                Config::set('system', 'maintenance', 1);
-               Config::set('system', 'maintenance_reason', 'Database update');
-       }
-
-       if (isset($a->config["system"]["db_charset"])) {
-               $charset = $a->config["system"]["db_charset"];
-       } else {
-               $charset = "utf8";
+               Config::set('system', 'maintenance_reason', sprintf(t(': Database update'), dbm::date().' '.date('e')));
        }
 
        $errors = false;
@@ -168,16 +177,18 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                $tables = q("SHOW TABLES");
        }
 
-       foreach ($tables AS $table) {
-               $table = current($table);
+       if (dbm::is_result($tables)) {
+               foreach ($tables AS $table) {
+                       $table = current($table);
 
-               logger(sprintf('updating structure for table %s ...', $table), LOGGER_DEBUG);
-               $database[$table] = table_structure($table);
+                       logger(sprintf('updating structure for table %s ...', $table), LOGGER_DEBUG);
+                       $database[$table] = table_structure($table);
+               }
        }
 
        // Get the definition
        if (is_null($definition)) {
-               $definition = db_definition($charset);
+               $definition = db_definition();
        }
 
        // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
@@ -194,7 +205,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                $group_by = "";
                $sql3 = "";
                if (!isset($database[$name])) {
-                       $r = db_create_table($name, $structure["fields"], $charset, $verbose, $action, $structure['indexes']);
+                       $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;
                        }
@@ -252,17 +263,25 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                        }
                                } else {
                                        // Compare the field definition
-                                       $current_field_definition = implode(",",$database[$name]["fields"][$fieldname]);
-                                       $new_field_definition = implode(",",$parameters);
+                                       $field_definition = $database[$name]["fields"][$fieldname];
+
+                                       // 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) {
-                                               $sql2=db_modify_table_field($fieldname, $parameters);
+                                               $sql2 = db_modify_table_field($fieldname, $parameters);
                                                if ($sql3 == "") {
                                                        $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
                                                } else {
                                                        $sql3 .= ", ".$sql2;
                                                }
                                        }
-
                                }
                        }
                }
@@ -288,10 +307,23 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                                $group_by = db_group_by($indexname, $fieldnames);
                                        }
                                        if ($sql2 != "") {
-                                               if ($sql3 == "")
+                                               if ($sql3 == "") {
                                                        $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
-                                               else
+                                               } else {
                                                        $sql3 .= ", ".$sql2;
+                                               }
+                                       }
+                               }
+                       }
+
+                       if (isset($database[$name]["table_status"]["Collation"])) {
+                               if ($database[$name]["table_status"]["Collation"] != 'utf8mb4_general_ci') {
+                                       $sql2 = "DEFAULT COLLATE utf8mb4_general_ci";
+
+                                       if ($sql3 == "") {
+                                               $sql3 = "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2;
+                                       } else {
+                                               $sql3 .= ", ".$sql2;
                                        }
                                }
                        }
@@ -323,6 +355,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 != "") {
@@ -376,6 +410,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";
 
@@ -395,7 +433,7 @@ function db_field_command($parameters, $create = true) {
        return($fieldstruct);
 }
 
-function db_create_table($name, $fields, $charset, $verbose, $action, $indexes=null) {
+function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
        global $a, $db;
 
        $r = true;
@@ -420,7 +458,7 @@ function db_create_table($name, $fields, $charset, $verbose, $action, $indexes=n
 
        $sql = implode(",\n\t", $sql_rows);
 
-       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=".$charset;
+       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT COLLATE utf8mb4_general_ci";
        if ($verbose)
                echo $sql.";\n";
 
@@ -503,18 +541,7 @@ function db_group_by($indexname, $fieldnames) {
        return $sql;
 }
 
-function db_index_suffix($charset, $reduce = 0) {
-       if ($charset != "utf8mb4") {
-               return "";
-       }
-
-       // On utf8mb4 indexes can only have a length of 191
-       $indexlength = 191 - $reduce;
-
-       return "(".$indexlength.")";
-}
-
-function db_definition($charset) {
+function db_definition() {
 
        $database = array();
 
@@ -1658,9 +1685,7 @@ function dbstructure_run(&$argv, &$argc) {
                                set_config('system','build',DB_UPDATE_VERSION);
                                return;
                        case "dumpsql":
-                               // For the dump that is used to create the database.sql we always assume utfmb4
-                               $charset = "utf8mb4";
-                               print_structure(db_definition($charset), $charset);
+                               print_structure(db_definition());
                                return;
                }
        }