]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Merge pull request #2853 from annando/1610-bugfix-config
[friendica.git] / include / dbstructure.php
index 32d5986fe97f27398f51b0af1e0a0bd446c2e2ca..fdf09d90def8abd034d0801540fa02ca2ed744b4 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.
@@ -154,6 +158,20 @@ 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->server_info(), '5.7.4') >= 0) AND
+               !(strpos($db->server_info(), 'MariaDB') !== false)) {
+               $ignore = '';
+       }else {
+               $ignore = ' IGNORE';
+       }
 
        // Compare it
        foreach ($definition AS $name => $structure) {
@@ -179,7 +197,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
                                        $sql2=db_drop_index($indexname);
                                        if ($sql3 == "")
-                                               $sql3 = "ALTER TABLE `".$name."` ".$sql2;
+                                               $sql3 = "ALTER".$ignore." TABLE `".$name."` ".$sql2;
                                        else
                                                $sql3 .= ", ".$sql2;
                                }
@@ -223,7 +241,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;
                                        }
@@ -330,6 +348,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 != "")
@@ -457,7 +480,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(
@@ -1067,7 +1090,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(
@@ -1491,6 +1514,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);
 
@@ -1523,7 +1549,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;