]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Issue 3857: There is the possibility of a bad handling of dislikes
[friendica.git] / include / dbstructure.php
index 4d615a2f14ba16dcd8929cb3da4fac6c8507de45..0b15805b4df8fd3ee26878a925810625a02ee43b 100644 (file)
@@ -17,10 +17,8 @@ const DB_UPDATE_FAILED = 2;      // Database check failed
  * Converts all tables from MyISAM to InnoDB
  */
 function convert_to_innodb() {
-       global $db;
-
        $r = q("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `engine` = 'MyISAM' AND `table_schema` = '%s'",
-               dbesc($db->database_name()));
+               dbesc(dba::database_name()));
 
        if (!dbm::is_result($r)) {
                echo t('There are no tables on MyISAM.')."\n";
@@ -33,7 +31,7 @@ function convert_to_innodb() {
 
                $result = dba::e($sql);
                if (!dbm::is_result($result)) {
-                       print_update_error($db, $sql);
+                       print_update_error($sql);
                }
        }
 }
@@ -132,10 +130,8 @@ function table_structure($table) {
                        }
 
                        $column = $index["Column_name"];
-                       // On utf8mb4 a varchar index can only have a length of 191
-                       // The "show index" command sometimes returns this value although this value wasn't added manually.
-                       // Because we don't want to add this number to every index, we ignore bigger numbers
-                       if (($index["Sub_part"] != "") && (($index["Sub_part"] < 191) || ($index["Key_name"] == "PRIMARY"))) {
+
+                       if (($index["Sub_part"] != "")) {
                                $column .= "(".$index["Sub_part"].")";
                        }
 
@@ -188,20 +184,19 @@ 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) {
+function print_update_error($message) {
        echo sprintf(t("\nError %d occurred during database update:\n%s\n"),
-               $db->errorno, $db->error);
+               dba::errorNo(), dba::errorMessage());
 
        return t('Errors encountered performing database changes: ').$message.EOL;
 }
 
 function update_structure($verbose, $action, $tables=null, $definition=null) {
-       global $a, $db;
+       global $a;
 
        if ($action) {
                Config::set('system', 'maintenance', 1);
@@ -234,8 +229,8 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
        }
 
        // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements
-       if ((version_compare($db->server_info(), '5.7.4') >= 0) &&
-               !(strpos($db->server_info(), 'MariaDB') !== false)) {
+       if ((version_compare(dba::server_info(), '5.7.4') >= 0) &&
+               !(strpos(dba::server_info(), 'MariaDB') !== false)) {
                $ignore = '';
        } else {
                $ignore = ' IGNORE';
@@ -249,7 +244,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 .= print_update_error($db, $name);
+                               $errors .= print_update_error($name);
                        }
                        $is_new_table = True;
                } else {
@@ -415,17 +410,18 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
 
                        if ($verbose) {
                                // Ensure index conversion to unique removes duplicates
-                               if ($is_unique) {
+                               if ($is_unique && ($temp_name != $name)) {
                                        if ($ignore != "") {
                                                echo "SET session old_alter_table=1;\n";
                                        } else {
+                                               echo "DROP TABLE IF EXISTS `".$temp_name."`;\n";
                                                echo "CREATE TABLE `".$temp_name."` LIKE `".$name."`;\n";
                                        }
                                }
 
                                echo $sql3."\n";
 
-                               if ($is_unique) {
+                               if ($is_unique && ($temp_name != $name)) {
                                        if ($ignore != "") {
                                                echo "SET session old_alter_table=0;\n";
                                        } else {
@@ -440,13 +436,19 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                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 ($is_unique && ($temp_name != $name)) {
                                        if ($ignore != "") {
                                                dba::e("SET session old_alter_table=1;");
                                        } else {
+                                               dba::e("DROP TABLE IF EXISTS `".$temp_name."`;");
+                                               if (!dbm::is_result($r)) {
+                                                       $errors .= print_update_error($sql3);
+                                                       return $errors;
+                                               }
+
                                                $r = dba::e("CREATE TABLE `".$temp_name."` LIKE `".$name."`;");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= print_update_error($db, $sql3);
+                                                       $errors .= print_update_error($sql3);
                                                        return $errors;
                                                }
                                        }
@@ -454,25 +456,25 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
 
                                $r = @dba::e($sql3);
                                if (!dbm::is_result($r)) {
-                                       $errors .= print_update_error($db, $sql3);
+                                       $errors .= print_update_error($sql3);
                                }
-                               if ($is_unique) {
+                               if ($is_unique && ($temp_name != $name)) {
                                        if ($ignore != "") {
                                                dba::e("SET session old_alter_table=0;");
                                        } else {
                                                $r = dba::e("INSERT INTO `".$temp_name."` SELECT ".$field_list." FROM `".$name."`".$group_by.";");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= print_update_error($db, $sql3);
+                                                       $errors .= print_update_error($sql3);
                                                        return $errors;
                                                }
                                                $r = dba::e("DROP TABLE `".$name."`;");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= print_update_error($db, $sql3);
+                                                       $errors .= print_update_error($sql3);
                                                        return $errors;
                                                }
                                                $r = dba::e("RENAME TABLE `".$temp_name."` TO `".$name."`;");
                                                if (!dbm::is_result($r)) {
-                                                       $errors .= print_update_error($db, $sql3);
+                                                       $errors .= print_update_error($sql3);
                                                        return $errors;
                                                }
                                        }
@@ -522,7 +524,7 @@ function db_field_command($parameters, $create = true) {
 }
 
 function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
-       global $a, $db;
+       global $a;
 
        $r = true;
 
@@ -813,8 +815,8 @@ function db_definition() {
                                        "addr_uid" => array("addr(32)", "uid"),
                                        "nurl_uid" => array("nurl(32)", "uid"),
                                        "nick_uid" => array("nick(32)", "uid"),
-                                       "dfrn-id" => array("dfrn-id"),
-                                       "issued-id" => array("issued-id"),
+                                       "dfrn-id" => array("dfrn-id(64)"),
+                                       "issued-id" => array("issued-id(64)"),
                                        )
                        );
        $database["conv"] = array(
@@ -1158,11 +1160,11 @@ function db_definition() {
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
-                                       "guid" => array("guid"),
-                                       "uri" => array("uri"),
+                                       "guid" => array("guid(191)"),
+                                       "uri" => array("uri(191)"),
                                        "parent" => array("parent"),
-                                       "parent-uri" => array("parent-uri"),
-                                       "extid" => array("extid"),
+                                       "parent-uri" => array("parent-uri(191)"),
+                                       "extid" => array("extid(191)"),
                                        "uid_id" => array("uid","id"),
                                        "uid_contactid_id" => array("uid","contact-id","id"),
                                        "uid_created" => array("uid","created"),
@@ -1175,7 +1177,7 @@ function db_definition() {
                                        "authorid_created" => array("author-id","created"),
                                        "ownerid" => array("owner-id"),
                                        "uid_uri" => array("uid", "uri(190)"),
-                                       "resource-id" => array("resource-id"),
+                                       "resource-id" => array("resource-id(191)"),
                                        "contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"), //
                                        "uid_type_changed" => array("uid","type(190)","changed"),
                                        "contactid_verb" => array("contact-id","verb(190)"),
@@ -1197,7 +1199,7 @@ function db_definition() {
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
                                        "uid" => array("uid"),
-                                       "sid" => array("sid"),
+                                       "sid" => array("sid(32)"),
                                        "service" => array("service(32)"),
                                        "iid" => array("iid"),
                                        )
@@ -1239,7 +1241,7 @@ function db_definition() {
                                        "convid" => array("convid"),
                                        "uri" => array("uri(64)"),
                                        "parent-uri" => array("parent-uri(64)"),
-                                       "contactid" => array("contact-id"),
+                                       "contactid" => array("contact-id(32)"),
                                        )
                        );
        $database["mailacct"] = array(
@@ -1590,7 +1592,7 @@ function db_definition() {
                                        "uid" => array("uid"),
                                        "spam" => array("spam"),
                                        "ham" => array("ham"),
-                                       "term" => array("term"),
+                                       "term" => array("term(32)"),
                                        )
                        );
        $database["term"] = array(
@@ -1711,7 +1713,6 @@ function db_definition() {
                                        "account_expired" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "account_expires_on" => array("type" => "datetime", "not null" => "1", "default" => NULL_DATE),
                                        "expire_notification_sent" => array("type" => "datetime", "not null" => "1", "default" => NULL_DATE),
-                                       "service_class" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
                                        "def_gid" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
                                        "allow_cid" => array("type" => "mediumtext"),
                                        "allow_gid" => array("type" => "mediumtext"),
@@ -1761,18 +1762,16 @@ function db_definition() {
  * run from command line
  */
 function dbstructure_run(&$argv, &$argc) {
-       global $a, $db;
+       global $a;
 
-       if (is_null($a)) {
+       if (empty($a)) {
                $a = new App(dirname(__DIR__));
        }
 
-       if (is_null($db)) {
-               @include ".htconfig.php";
-               require_once "include/dba.php";
-               $db = new dba($db_host, $db_user, $db_pass, $db_data);
-               unset($db_host, $db_user, $db_pass, $db_data);
-       }
+       @include ".htconfig.php";
+       require_once "include/dba.php";
+       dba::connect($db_host, $db_user, $db_pass, $db_data);
+       unset($db_host, $db_user, $db_pass, $db_data);
 
        if ($argc == 2) {
                switch ($argv[1]) {