From 5e0cb1f92d00b8a605cb08cd92946cde7e0314b0 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 20 Dec 2018 21:35:13 -0100 Subject: changing field in 4 steps Signed-off-by: Maxence Lange --- lib/Migration/Version0001Date20181204203147.php | 2 +- lib/Migration/Version0001Date20181211222142.php | 93 ----------------- lib/Migration/Version0001Date20181219000001.php | 133 ++++++++++++++++++++++++ lib/Migration/Version0001Date20181219000002.php | 83 +++++++++++++++ lib/Migration/Version0001Date20181219000003.php | 128 +++++++++++++++++++++++ lib/Migration/Version0001Date20181219000004.php | 83 +++++++++++++++ 6 files changed, 428 insertions(+), 94 deletions(-) delete mode 100644 lib/Migration/Version0001Date20181211222142.php create mode 100644 lib/Migration/Version0001Date20181219000001.php create mode 100644 lib/Migration/Version0001Date20181219000002.php 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/Version0001Date20181211222142.php b/lib/Migration/Version0001Date20181211222142.php deleted file mode 100644 index 93f1d59e..00000000 --- a/lib/Migration/Version0001Date20181211222142.php +++ /dev/null @@ -1,93 +0,0 @@ - - * @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\Migration\IOutput; -use OCP\Migration\SimpleMigrationStep; - - -/** - * Class Version0001Date20181211222142 - * - * @package OCA\Social\Migration - */ -class Version0001Date20181211222142 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(); - - $table = $schema->getTable(CoreRequestBuilder::TABLE_CACHE_ACTORS); - $table->changeColumn( - 'source', [ - 'notnull' => false, - 'type' => Type::getType(Type::TEXT) - ] - ); - - $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) - ] - ); - - return $schema; - } -} - 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/Version0001Date20181219000002.php b/lib/Migration/Version0001Date20181219000002.php new file mode 100644 index 00000000..182a12cd --- /dev/null +++ b/lib/Migration/Version0001Date20181219000002.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 OCA\Social\Db\CoreRequestBuilder; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + + +/** + * Class Version0001Date20181219000002 + * + * @package OCA\Social\Migration + */ +class Version0001Date20181219000002 extends SimpleMigrationStep { + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * + * @return ISchemaWrapper + * @throws SchemaException + */ + 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); + } + } + + 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; + } + +} + -- cgit v1.2.3