]> 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 0ff97d0c6c3685aa162b733b9b2b766902e332f7..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
         */
@@ -292,6 +308,10 @@ class DBStructure
        public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
        {
                if ($action && !$install) {
+                       if (self::isUpdating()) {
+                               return DI::l10n()->t('Another database update is currently running.');
+                       }
+
                        DI::config()->set('system', 'maintenance', 1);
                        DI::config()->set('system', 'maintenance_reason', DI::l10n()->t('%s: Database update', DateTimeFormat::utcNow() . ' ' . date('e')));
                }
@@ -479,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;
@@ -689,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;
                        }
                }
@@ -728,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'])) {
@@ -779,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']);
@@ -1003,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();
@@ -1060,4 +1083,28 @@ class DBStructure
                        DBA::close($tokens);
                }
        }
+
+       /**
+        * Checks if a database update is currently running
+        *
+        * @return boolean
+        */
+       private static function isUpdating()
+       {
+               $isUpdate = false;
+
+               $processes = DBA::select(['information_schema' => 'processlist'], ['info'],
+                       ['db' => DBA::databaseName(), 'command' => ['Query', 'Execute']]);
+
+               while ($process = DBA::fetch($processes)) {
+                       $parts = explode(' ', $process['info']);
+                       if (in_array(strtolower(array_shift($parts)), ['alter', 'create', 'drop', 'rename'])) {
+                               $isUpdate = true;
+                       }
+               }
+
+               DBA::close($processes);
+
+               return $isUpdate;
+       }
 }