]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Merge pull request #9146 from tobiasd/2020.09-CHANGELOG
[friendica.git] / src / Database / DBStructure.php
index 352e8b0f8791af06b93a7bf11e2ddf9b84db55cb..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
         */
@@ -291,13 +307,11 @@ class DBStructure
         */
        public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
        {
-               if (!$install) {
+               if ($action && !$install) {
                        if (self::isUpdating()) {
                                return DI::l10n()->t('Another database update is currently running.');
                        }
-               }
 
-               if ($action && !$install) {
                        DI::config()->set('system', 'maintenance', 1);
                        DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: Database update', DateTimeFormat::utcNow() . ' ' . date('e')));
                }
@@ -485,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;
@@ -695,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;
                        }
                }
@@ -734,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'])) {
@@ -785,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']);
@@ -1009,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();
@@ -1081,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;
                        }
                }