]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Merge pull request #9362 from annando/post-update-term
[friendica.git] / src / Database / DBStructure.php
index dcb59fc9f2a68cf170c649abe5a9cceb520cbd83..bdd8cc208efe2e829183ef3e2ab6ab74d75c802a 100644 (file)
@@ -48,6 +48,22 @@ class DBStructure
         */
        private static $definition = [];
 
+       /**
+        * Set a database version to trigger update functions
+        *
+        * @param string $version
+        * @return void
+        */
+       public static function setDatabaseVersion(string $version)
+       {
+               if (!is_numeric($version)) {
+                       throw new \Asika\SimpleConsole\CommandArgsException('The version number must be numeric');
+               }
+
+               DI::config()->set('system', 'build', $version);
+               echo DI::l10n()->t('The database version had been set to %s.', $version);
+       }
+
        /**
         * Converts all tables from MyISAM/InnoDB Antelope to InnoDB Barracuda
         */
@@ -483,8 +499,8 @@ class DBStructure
                                        }
                                }
 
-                               foreach ($existing_foreign_keys as $constraint => $param) {
-                                       $sql2 = self::dropForeignKey($constraint);
+                               foreach ($existing_foreign_keys as $param) {
+                                       $sql2 = self::dropForeignKey($param['CONSTRAINT_NAME']);
 
                                        if ($sql3 == "") {
                                                $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2;
@@ -693,8 +709,8 @@ class DBStructure
 
                if (DBA::isResult($foreign_keys)) {
                        foreach ($foreign_keys as $foreign_key) {
-                               $constraint = $foreign_key['CONSTRAINT_NAME'];
-                               unset($foreign_key['CONSTRAINT_NAME']); 
+                               $parameters = ['foreign' => [$foreign_key['REFERENCED_TABLE_NAME'] => $foreign_key['REFERENCED_COLUMN_NAME']]];
+                               $constraint = self::getConstraintName($table, $foreign_key['COLUMN_NAME'], $parameters);
                                $foreigndata[$constraint] = $foreign_key;
                        }
                }
@@ -732,8 +748,8 @@ class DBStructure
                                        $fielddata[$field['COLUMN_NAME']]['not null'] = true;
                                }
 
-                               if (isset($field['COLUMN_DEFAULT'])) {
-                                       $fielddata[$field['COLUMN_NAME']]['default'] = $field['COLUMN_DEFAULT'];
+                               if (isset($field['COLUMN_DEFAULT']) && ($field['COLUMN_DEFAULT'] != 'NULL')) {
+                                       $fielddata[$field['COLUMN_NAME']]['default'] = trim($field['COLUMN_DEFAULT'], "'");
                                }
 
                                if (!empty($field['EXTRA'])) {
@@ -783,10 +799,7 @@ class DBStructure
                $foreign_table = array_keys($parameters['foreign'])[0];
                $foreign_field = array_values($parameters['foreign'])[0];
 
-               $constraint = self::getConstraintName($tablename, $fieldname, $parameters);
-
-               $sql = "CONSTRAINT `" . $constraint . "` FOREIGN KEY (`" . $fieldname . "`)" .
-                       " REFERENCES `" . $foreign_table . "` (`" . $foreign_field . "`)";
+               $sql = "FOREIGN KEY (`" . $fieldname . "`) REFERENCES `" . $foreign_table . "` (`" . $foreign_field . "`)";
 
                if (!empty($parameters['foreign']['on update'])) {
                        $sql .= " ON UPDATE " . strtoupper($parameters['foreign']['on update']);
@@ -1007,6 +1020,12 @@ class DBStructure
         */
        public static function checkInitialValues()
        {
+               if (self::existsTable('verb') && !DBA::exists('verb', ['id' => 1])) {
+                       foreach (Item::ACTIVITIES as $index => $activity) {
+                               DBA::insert('verb', ['id' => $index + 1, 'name' => $activity], true);
+                       }
+               }
+
                if (self::existsTable('contact') && !DBA::exists('contact', ['id' => 0])) {
                        DBA::insert('contact', ['nurl' => '']);
                        $lastid = DBA::lastInsertId();
@@ -1079,7 +1098,7 @@ class DBStructure
 
                while ($process = DBA::fetch($processes)) {
                        $parts = explode(' ', $process['info']);
-                       if (strtolower(array_shift($parts)) == 'alter') {
+                       if (in_array(strtolower(array_shift($parts)), ['alter', 'create', 'drop', 'rename'])) {
                                $isUpdate = true;
                        }
                }