From 124b4374d66d6ae22d0d9ec54b17fbcd4acb5bc7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 19 Sep 2017 12:10:48 +0200 Subject: Add a base migration for easy updating to bigints Signed-off-by: Joas Schilling --- lib/public/Migration/BigIntMigration.php | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/public/Migration/BigIntMigration.php (limited to 'lib/public/Migration') diff --git a/lib/public/Migration/BigIntMigration.php b/lib/public/Migration/BigIntMigration.php new file mode 100644 index 00000000000..91722671860 --- /dev/null +++ b/lib/public/Migration/BigIntMigration.php @@ -0,0 +1,66 @@ + + * + * @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 OCP\Migration; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\DBAL\Types\Type; + +/** + * @since 13.0.0 + */ +abstract class BigIntMigration extends SimpleMigrationStep { + + /** + * @return array Returns an array with the following structure + * ['table1' => ['column1', 'column2'], ...] + * @since 13.0.0 + */ + abstract protected function getColumnsByTable(); + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `Schema` + * @param array $options + * @return null|Schema + * @since 13.0.0 + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var Schema $schema */ + $schema = $schemaClosure(); + + $tables = $this->getColumnsByTable(); + + foreach ($tables as $tableName => $columns) { + $table = $schema->getTable($tableName); + + foreach ($columns as $columnName) { + $column = $table->getColumn($columnName); + if ($column->getType()->getName() !== Type::BIGINT) { + $column->setType(Type::getType(Type::BIGINT)); + $column->setOptions(['length' => 20]); + } + } + } + + return $schema; + } +} -- cgit v1.2.3