From 2a2b3f72fb73ea9282d37223ffb3e0e36f078d69 Mon Sep 17 00:00:00 2001 From: Miguel Dantas Date: Sat, 6 Jul 2019 16:52:30 +0100 Subject: [PATCH] [Embed][DB] Renaming the 'file_oembed' table to 'file_embed' on upgrade --- lib/schema.php | 18 ++++++++++++++++++ plugins/Embed/EmbedPlugin.php | 10 +++++++++- plugins/Embed/classes/File_embed.php | 8 ++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/schema.php b/lib/schema.php index ccdd0e4286..bba6d1781c 100644 --- a/lib/schema.php +++ b/lib/schema.php @@ -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 diff --git a/plugins/Embed/EmbedPlugin.php b/plugins/Embed/EmbedPlugin.php index 7557fc0adc..f8d0d14d8a 100644 --- a/plugins/Embed/EmbedPlugin.php +++ b/plugins/Embed/EmbedPlugin.php @@ -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. diff --git a/plugins/Embed/classes/File_embed.php b/plugins/Embed/classes/File_embed.php index f05bf09889..8dc008bbdf 100644 --- a/plugins/Embed/classes/File_embed.php +++ b/plugins/Embed/classes/File_embed.php @@ -28,14 +28,14 @@ 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')), ), ); }