From 5e0cb1f92d00b8a605cb08cd92946cde7e0314b0 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 20 Dec 2018 21:35:13 -0100 Subject: [PATCH] changing field in 4 steps Signed-off-by: Maxence Lange --- .../Version0001Date20181204203147.php | 2 +- .../Version0001Date20181219000001.php | 133 ++++++++++++++++++ ....php => Version0001Date20181219000002.php} | 38 ++--- .../Version0001Date20181219000003.php | 128 +++++++++++++++++ .../Version0001Date20181219000004.php | 83 +++++++++++ 5 files changed, 359 insertions(+), 25 deletions(-) create mode 100644 lib/Migration/Version0001Date20181219000001.php rename lib/Migration/{Version0001Date20181211222142.php => Version0001Date20181219000002.php} (71%) create mode 100644 lib/Migration/Version0001Date20181219000003.php create mode 100644 lib/Migration/Version0001Date20181219000004.php diff --git a/lib/Migration/Version0001Date20181204203147.php b/lib/Migration/Version0001Date20181204203147.php index 46bf48eb..c98b319b 100644 --- a/lib/Migration/Version0001Date20181204203147.php +++ b/lib/Migration/Version0001Date20181204203147.php @@ -318,7 +318,7 @@ class Version0001Date20181204203147 extends SimpleMigrationStep { ]); $table->addColumn('author', 'string', [ 'notnull' => true, - 'length' => 1270, + 'length' => 127, ]); $table->addColumn('activity', 'string', [ 'notnull' => true, diff --git a/lib/Migration/Version0001Date20181219000001.php b/lib/Migration/Version0001Date20181219000001.php new file mode 100644 index 00000000..57eda132 --- /dev/null +++ b/lib/Migration/Version0001Date20181219000001.php @@ -0,0 +1,133 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Migration; + + +use Closure; +use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Types\Type; +use OCA\Social\Db\CoreRequestBuilder; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + + +/** + * Class Version0001Date20181219000001 + * + * @package OCA\Social\Migration + */ +class Version0001Date20181219000001 extends SimpleMigrationStep { + + + /** @var IDBConnection */ + private $connection; + + + /** @var array */ + public static $editToText = [ + [CoreRequestBuilder::TABLE_CACHE_ACTORS, 'source'], + [CoreRequestBuilder::TABLE_CACHE_ACTORS, 'summary'], + [CoreRequestBuilder::TABLE_CACHE_ACTORS, 'details'], + [CoreRequestBuilder::TABLE_REQUEST_QUEUE, 'activity'], + [CoreRequestBuilder::TABLE_SERVER_ACTORS, 'summary'], + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'content'], + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'summary'], + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'instances'], + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'source'] + ]; + + /** @var array */ + public static $editToChar4000 = [ + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'to_array'], + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'cc'], + [CoreRequestBuilder::TABLE_SERVER_NOTES, 'bcc'] + ]; + + + /** + * @param IDBConnection $connection + */ + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * + * @return ISchemaWrapper + * @throws SchemaException + * @throws DBALException + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options + ): ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + foreach (array_merge(self::$editToText, self::$editToChar4000) as $edit) { + list($tableName, $field) = $edit; + + $table = $schema->getTable($tableName); + if ($table->hasColumn($field . '_copy')) { + continue; + } + + $table->addColumn($field . '_copy', Type::TEXT, ['notnull' => false]); + } + + return $schema; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + + foreach (array_merge(self::$editToText, self::$editToChar4000) as $edit) { + list($tableName, $field) = $edit; + + $qb = $this->connection->getQueryBuilder(); + $qb->update($tableName) + ->set($field . '_copy', $field) + ->execute(); + } + } + +} + diff --git a/lib/Migration/Version0001Date20181211222142.php b/lib/Migration/Version0001Date20181219000002.php similarity index 71% rename from lib/Migration/Version0001Date20181211222142.php rename to lib/Migration/Version0001Date20181219000002.php index 93f1d59e..182a12cd 100644 --- a/lib/Migration/Version0001Date20181211222142.php +++ b/lib/Migration/Version0001Date20181219000002.php @@ -37,16 +37,17 @@ use Doctrine\DBAL\Schema\SchemaException; use Doctrine\DBAL\Types\Type; use OCA\Social\Db\CoreRequestBuilder; use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; /** - * Class Version0001Date20181211222142 + * Class Version0001Date20181219000002 * * @package OCA\Social\Migration */ -class Version0001Date20181211222142 extends SimpleMigrationStep { +class Version0001Date20181219000002 extends SimpleMigrationStep { /** @@ -56,38 +57,27 @@ class Version0001Date20181211222142 extends SimpleMigrationStep { * * @return ISchemaWrapper * @throws SchemaException - * @throws DBALException */ public function changeSchema(IOutput $output, Closure $schemaClosure, array $options ): ISchemaWrapper { /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); - $table = $schema->getTable(CoreRequestBuilder::TABLE_CACHE_ACTORS); - $table->changeColumn( - 'source', [ - 'notnull' => false, - 'type' => Type::getType(Type::TEXT) - ] + $edits = array_merge( + Version0001Date20181219000001::$editToText, + Version0001Date20181219000001::$editToChar4000 ); + foreach ($edits as $edit) { + list($tableName, $field) = $edit; - $table = $schema->getTable(CoreRequestBuilder::TABLE_SERVER_NOTES); - $table->changeColumn( - 'source', [ - 'notnull' => false, - 'type' => Type::getType(Type::TEXT) - ] - ); - - $table = $schema->getTable(CoreRequestBuilder::TABLE_REQUEST_QUEUE); - $table->changeColumn( - 'activity', [ - 'notnull' => false, - 'type' => Type::getType(Type::TEXT) - ] - ); + $table = $schema->getTable($tableName); + if ($table->hasColumn($field) && $table->hasColumn($field . '_copy')) { + $table->dropColumn($field); + } + } return $schema; } + } diff --git a/lib/Migration/Version0001Date20181219000003.php b/lib/Migration/Version0001Date20181219000003.php new file mode 100644 index 00000000..4526d938 --- /dev/null +++ b/lib/Migration/Version0001Date20181219000003.php @@ -0,0 +1,128 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Migration; + + +use Closure; +use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + + +/** + * Class Version0001Date20181219000003 + * + * @package OCA\Social\Migration + */ +class Version0001Date20181219000003 extends SimpleMigrationStep { + + + /** @var IDBConnection */ + private $connection; + + + /** + * @param IDBConnection $connection + */ + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * + * @return ISchemaWrapper + * @throws SchemaException + * @throws DBALException + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options + ): ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + // -> TEXT + foreach (Version0001Date20181219000001::$editToText as $edit) { + list($tableName, $field) = $edit; + + $table = $schema->getTable($tableName); + if ($table->hasColumn($field)) { + continue; + } + + $table->addColumn($field, Type::TEXT, ['notnull' => false]); + } + + // -> VARCHAR(4000) + foreach (Version0001Date20181219000001::$editToChar4000 as $edit) { + list($tableName, $field) = $edit; + + $table = $schema->getTable($tableName); + if ($table->hasColumn($field)) { + continue; + } + + $table->addColumn($field, Type::STRING, ['notnull' => false, 'length' => 4000]); + } + + return $schema; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + + $edits = array_merge( + Version0001Date20181219000001::$editToText, + Version0001Date20181219000001::$editToChar4000 + ); + foreach ($edits as $edit) { + list($table, $field) = $edit; + + $qb = $this->connection->getQueryBuilder(); + $qb->update($table) + ->set($field, $field . '_copy') + ->execute(); + } + } + +} + diff --git a/lib/Migration/Version0001Date20181219000004.php b/lib/Migration/Version0001Date20181219000004.php new file mode 100644 index 00000000..5af222a6 --- /dev/null +++ b/lib/Migration/Version0001Date20181219000004.php @@ -0,0 +1,83 @@ + + * @copyright 2018, Maxence Lange + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + + +namespace OCA\Social\Migration; + + +use Closure; +use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + + +/** + * Class Version0001Date20181219000004 + * + * @package OCA\Social\Migration + */ +class Version0001Date20181219000004 extends SimpleMigrationStep { + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * + * @return ISchemaWrapper + * @throws SchemaException + * @throws DBALException + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options + ): ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $edits = array_merge( + Version0001Date20181219000001::$editToText, + Version0001Date20181219000001::$editToChar4000 + ); + foreach ($edits as $edit) { + list($tableName, $field) = $edit; + + $table = $schema->getTable($tableName); + if ($table->hasColumn($field) && $table->hasColumn($field . '_copy')) { + $table->dropColumn($field . '_copy'); + } + } + + return $schema; + } + +} +