X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdbstructure.php;h=bd4a07eb56e73a9c63bb4cb468409da66d62ebfc;hb=1aa9b7242d65191fcee2804489d6b0beb4ea550c;hp=6224b434d819cb5267d34478d45e028661dadd1c;hpb=32f49b4de2ca39ac037a8600624e74d0ed52d8a8;p=friendica.git diff --git a/include/dbstructure.php b/include/dbstructure.php index 6224b434d8..bd4a07eb56 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -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 != "") @@ -420,6 +443,7 @@ function db_definition($charset) { "indexes" => array( "PRIMARY" => array("k".db_index_suffix($charset)), "updated" => array("updated"), + "expire_mode_updated" => array("expire_mode", "updated"), ) ); $database["challenge"] = array( @@ -457,7 +481,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( @@ -534,6 +558,7 @@ function db_definition($charset) { "indexes" => array( "PRIMARY" => array("id"), "uid" => array("uid"), + "addr_uid" => array("addr", "uid"), "nurl" => array("nurl"), ) ); @@ -562,6 +587,7 @@ function db_definition($charset) { ), "indexes" => array( "PRIMARY" => array("id"), + "cmd_item_contact" => array("UNIQUE", "cmd", "item", "contact"), ) ); $database["event"] = array( @@ -686,6 +712,7 @@ function db_definition($charset) { "gender" => array("type" => "varchar(32)", "not null" => "1", "default" => ""), "birthday" => array("type" => "varchar(32)", "not null" => "1", "default" => "0000-00-00"), "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"), "nsfw" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), "network" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), @@ -891,7 +918,7 @@ function db_definition($charset) { "ownerid_created" => array("owner-id","created"), "wall_body" => array("wall","body(6)"), "uid_visible_moderated_created" => array("uid","visible","moderated","created"), - "uid_uri" => array("uid","uri"), + "uid_uri" => array("uid", "uri"), "uid_wall_created" => array("uid","wall","created"), "resource-id" => array("resource-id"), "uid_type" => array("uid","type"), @@ -1012,6 +1039,8 @@ function db_definition($charset) { "seen" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), "verb" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "otype" => array("type" => "varchar(16)", "not null" => "1", "default" => ""), + "name_cache" => array("type" => "tinytext"), + "msg_cache" => array("type" => "mediumtext") ), "indexes" => array( "PRIMARY" => array("id"), @@ -1066,7 +1095,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( @@ -1096,7 +1125,9 @@ function db_definition($charset) { ), "indexes" => array( "PRIMARY" => array("id"), - "uid" => array("uid"), + "uid_contactid" => array("uid", "contact-id"), + "uid_profile" => array("uid", "profile"), + "uid_album_created" => array("uid", "album", "created"), "resource-id" => array("resource-id"), "guid" => array("guid"), ) @@ -1331,6 +1362,7 @@ function db_definition($charset) { "type_term" => array("type","term"), "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"), "otype_type_term_tid" => array("otype","type","term","tid"), + "uid_otype_type_url" => array("uid","otype","type","url"), "guid" => array("guid"), ) ); @@ -1490,8 +1522,27 @@ function dbstructure_run(&$argv, &$argc) { if ($argc==2) { switch ($argv[1]) { + case "dryrun": + update_structure(true, false); + return; case "update": update_structure(true, true); + + $build = get_config('system','build'); + if (!x($build)) { + set_config('system','build',DB_UPDATE_VERSION); + $build = DB_UPDATE_VERSION; + } + + $stored = intval($build); + $current = intval(DB_UPDATE_VERSION); + + // run any left update_nnnn functions in update.php + for($x = $stored; $x < $current; $x ++) { + $r = run_update_function($x); + if (!$r) break; + } + set_config('system','build',DB_UPDATE_VERSION); return; case "dumpsql": @@ -1506,7 +1557,8 @@ function dbstructure_run(&$argv, &$argc) { // print help echo $argv[0]." \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;