[Embed][DB] Renaming the 'file_oembed' table to 'file_embed' on upgrade

This commit is contained in:
Miguel Dantas 2019-07-06 16:52:30 +01:00 committed by Diogo Cordeiro
parent 52819d39d9
commit 2a2b3f72fb
3 changed files with 31 additions and 5 deletions

View File

@ -1066,6 +1066,24 @@ class Schema
return $out; 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 class SchemaTableMissingException extends Exception

View File

@ -73,11 +73,19 @@ class EmbedPlugin extends Plugin
*/ */
public function onCheckSchema() public function onCheckSchema()
{ {
$this->onEndUpgrade(); // Ensure rename
$schema = Schema::get(); $schema = Schema::get();
$schema->ensureTable('file_oembed', File_oembed::schemaDef()); $schema->ensureTable('file_embed', File_embed::schemaDef());
return true; 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 * This code executes when GNU social creates the page routing, and we hook
* on this event to add our action handler for Embed. * on this event to add our action handler for Embed.

View File

@ -28,14 +28,14 @@
defined('GNUSOCIAL') || die(); defined('GNUSOCIAL') || die();
/** /**
* Table Definition for file_oembed * Table Definition for file_embed
* *
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @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 $file_id; // int(4) primary_key not_null
public $version; // varchar(20) public $version; // varchar(20)
public $type; // varchar(20) public $type; // varchar(20)
@ -72,7 +72,7 @@ class File_oembed extends Managed_DataObject
), ),
'primary key' => array('file_id'), 'primary key' => array('file_id'),
'foreign keys' => array( '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')),
), ),
); );
} }