]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Fix Issue #2816 - MySQL version typo
[friendica.git] / include / dbstructure.php
index 21545153b89f2013f5be7b8472b910fd9fe04497..9e0020fdbe21f5d0bb94780263ae99c30a3bb691 100644 (file)
@@ -78,6 +78,10 @@ function table_structure($table) {
                        if ($index["Index_type"] == "FULLTEXT")
                                continue;
 
+                       if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
+                               $indexdata[$index["Key_name"]] = array('UNIQUE');
+                       }
+
                        $column = $index["Column_name"];
                        // On utf8mb4 a varchar index can only have a length of 191
                        // To avoid the need to add this to every index definition we just ignore it here.
@@ -126,6 +130,9 @@ function print_structure($database, $charset) {
 function update_structure($verbose, $action, $tables=null, $definition=null) {
        global $a, $db;
 
+       if ($action)
+               set_config('system', 'maintenance', 1);
+
        if (isset($a->config["system"]["db_charset"]))
                $charset = $a->config["system"]["db_charset"];
        else
@@ -151,6 +158,19 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
        if (is_null($definition))
                $definition = db_definition($charset);
 
+       // Ensure index conversion to unique removes duplicates
+       $sql_config = "SET session old_alter_table=1;";
+       if ($verbose)
+               echo $sql_config."\n";
+       if ($action)
+               @$db->q($sql_config);
+
+       // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
+       if (version_compare($db->getdb()->server_info, '5.7.4') >= 0) {
+               $ignore = '';
+       }else {
+               $ignore = ' IGNORE';
+       }
 
        // Compare it
        foreach ($definition AS $name => $structure) {
@@ -220,7 +240,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                        $sql2=db_create_index($indexname, $fieldnames);
                                        if ($sql2 != "") {
                                                if ($sql3 == "")
-                                                       $sql3 = "ALTER TABLE `".$name."` ".$sql2;
+                                                       $sql3 = "ALTER" . $ignore . " TABLE `".$name."` ".$sql2;
                                                else
                                                        $sql3 .= ", ".$sql2;
                                        }
@@ -241,6 +261,9 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                }
        }
 
+       if ($action)
+               set_config('system', 'maintenance', 0);
+
        return $errors;
 }
 
@@ -324,6 +347,11 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
                killme();
        }
 
+       if ($fieldnames[0] == "UNIQUE") {
+               array_shift($fieldnames);
+               $method .= ' UNIQUE';
+       }
+
        $names = "";
        foreach ($fieldnames AS $fieldname) {
                if ($names != "")
@@ -336,7 +364,6 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
        }
 
        if ($indexname == "PRIMARY") {
-               //return sprintf("%s PRIMARY KEY(`%s`)", $method, implode("`,`", $fieldnames));
                return sprintf("%s PRIMARY KEY(%s)", $method, $names);
        }
 
@@ -452,7 +479,7 @@ function db_definition($charset) {
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
-                                       "cat_k" => array("cat(30)","k(30)"),
+                                       "cat_k" => array("UNIQUE", "cat(30)","k(30)"),
                                        )
                        );
        $database["contact"] = array(
@@ -1061,7 +1088,7 @@ function db_definition($charset) {
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
-                                       "uid_cat_k" => array("uid","cat(30)","k(30)"),
+                                       "uid_cat_k" => array("UNIQUE", "uid","cat(30)","k(30)"),
                                        )
                        );
        $database["photo"] = array(
@@ -1485,6 +1512,9 @@ function dbstructure_run(&$argv, &$argc) {
 
        if ($argc==2) {
                switch ($argv[1]) {
+                       case "dryrun":
+                               update_structure(true, false);
+                               return;
                        case "update":
                                update_structure(true, true);
                                set_config('system','build',DB_UPDATE_VERSION);
@@ -1501,7 +1531,8 @@ function dbstructure_run(&$argv, &$argc) {
        // print help
        echo $argv[0]." <command>\n";
        echo "\n";
-       echo "commands:\n";
+       echo "Commands:\n";
+       echo "dryrun            show database update schema queries without running them\n";
        echo "update            update database schema\n";
        echo "dumpsql           dump database schema\n";
        return;