]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/View.php
Merge pull request #10809 from annando/small-as
[friendica.git] / src / Database / View.php
index 51d051786dc89b1e4de0ea89b53cb887d033e388..9af4de0fce79de994e64133f6398e89d1276b7e3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -77,6 +77,20 @@ class View
 
        public static function create(bool $verbose, bool $action)
        {
+               // Delete previously used views that aren't used anymore
+               foreach(['post-view', 'post-thread-view'] as $view) {
+                       if (self::isView($view)) {
+                               $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($view));
+                               if (!empty($sql) && $verbose) {
+                                       echo $sql . ";\n";
+                               }
+               
+                               if (!empty($sql) && $action) {
+                                       DBA::e($sql);
+                               }
+                       }
+               }
+
                $definition = self::definition();
 
                foreach ($definition as $name => $structure) {
@@ -88,7 +102,7 @@ class View
        {
                $database = self::definition($basePath, false);
 
-               foreach ($database AS $name => $structure) {
+               foreach ($database as $name => $structure) {
                        echo "--\n";
                        echo "-- VIEW $name\n";
                        echo "--\n";
@@ -103,7 +117,7 @@ class View
                $r = true;
 
                $sql_rows = [];
-               foreach ($structure["fields"] AS $fieldname => $origin) {
+               foreach ($structure["fields"] as $fieldname => $origin) {
                        if (is_string($origin)) {
                                $sql_rows[] = $origin . " AS `" . DBA::escape($fieldname) . "`";
                        } elseif (is_array($origin) && (sizeof($origin) == 2)) {
@@ -112,16 +126,16 @@ class View
                }
 
                if (self::isView($name)) {
-                       $sql = sprintf("DROP VIEW `%s`", DBA::escape($name));
+                       $sql = sprintf("DROP VIEW IF EXISTS `%s`", DBA::escape($name));
                } elseif (self::isTable($name)) {
-                       $sql = sprintf("DROP TABLE `%s`", DBA::escape($name));
+                       $sql = sprintf("DROP TABLE IF EXISTS `%s`", DBA::escape($name));
                }
 
-               if ($verbose) {
+               if (!empty($sql) && $verbose) {
                        echo $sql . ";\n";
                }
 
-               if ($action) {
+               if (!empty($sql) && $action) {
                        DBA::e($sql);
                }
 
@@ -158,7 +172,7 @@ class View
        }
 
        /**
-        * Check if the given table/view is a view
+        * Check if the given table/view is a table
         *
         * @param string $table
         * @return boolean "true" if it's a table