]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
[Embed][DB] Renaming the 'file_oembed' table to 'file_embed' on upgrade
authorMiguel Dantas <biodantasgs@gmail.com>
Sat, 6 Jul 2019 15:52:30 +0000 (16:52 +0100)
committerDiogo Cordeiro <diogo@fc.up.pt>
Sat, 3 Aug 2019 16:48:28 +0000 (17:48 +0100)
lib/schema.php
plugins/Embed/EmbedPlugin.php
plugins/Embed/classes/File_embed.php

index ccdd0e42867b4221cefdcc3b4593c9f5e3c3fc4d..bba6d1781c325757af650486f8f45293614aa81b 100644 (file)
@@ -1066,6 +1066,24 @@ class Schema
         return $out;
     }
 
+    public function renameTable(string $old_name, string $new_name) : bool
+    {
+        try {
+            $this->getTableDef($old_name);
+            try {
+                $this->getTableDef($new_name);
+                // New table exists, can't work
+                throw new ServerException("Both table {$old_name} and {$new_name} exist. You're on your own");
+            } catch(SchemaTableMissingException $e) {
+                // New table doesn't exist, carry on
+            }
+        } catch(SchemaTableMissingException $e) {
+            // Already renamed, or no previous table, so we're done
+            return true;
+        }
+        return $this->runSqlSet(["ALTER TABLE {$old_name} RENAME TO {$new_name};"]);
+    }
+
 }
 
 class SchemaTableMissingException extends Exception
index 7557fc0adc3d43f95a6826e1d2dbed6e4409207e..f8d0d14d8a868e2c6f1800f25f85d8d0c9d5c443 100644 (file)
@@ -73,11 +73,19 @@ class EmbedPlugin extends Plugin
      */
     public function onCheckSchema()
     {
+        $this->onEndUpgrade(); // Ensure rename
+
         $schema = Schema::get();
-        $schema->ensureTable('file_oembed', File_oembed::schemaDef());
+        $schema->ensureTable('file_embed', File_embed::schemaDef());
         return true;
     }
 
+    public function onEndUpgrade()
+    {
+        $schema = Schema::get();
+        return $schema->renameTable('file_oembed', 'file_embed');
+    }
+
     /**
      * This code executes when GNU social creates the page routing, and we hook
      * on this event to add our action handler for Embed.
index f05bf09889d3ad0e6f36f998fe55ecd3987a8989..8dc008bbdfb2e76191fa9bdca9990ed0e12577e0 100644 (file)
 defined('GNUSOCIAL') || die();
 
 /**
- * Table Definition for file_oembed
+ * Table Definition for file_embed
  *
  * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  */
-class File_oembed extends Managed_DataObject
+class File_embed extends Managed_DataObject
 {
-    public $__table = 'file_oembed';                     // table name
+    public $__table = 'file_embed';                     // table name
     public $file_id;                         // int(4)  primary_key not_null
     public $version;                         // varchar(20)
     public $type;                            // varchar(20)
@@ -72,7 +72,7 @@ class File_oembed extends Managed_DataObject
             ),
             'primary key' => array('file_id'),
             'foreign keys' => array(
-                'file_oembed_file_id_fkey' => array('file', array('file_id' => 'id')),
+                'file_embed_file_id_fkey' => array('file', array('file_id' => 'id')),
             ),
         );
     }