From 352ee2565a3456554c45ba11e599913eec30e3d5 Mon Sep 17 00:00:00 2001 From: Christian Hartmann <16020496+Chartman123@users.noreply.github.com> Date: Tue, 6 Apr 2021 14:56:32 +0200 Subject: [PATCH] Add schema migration from mandatory to isRequired Signed-off-by: Christian Hartmann --- .../Version020300Date20210406114130.php | 78 +++++++++++++++++++ .../Version020300Date20210406133704.php | 55 +++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 lib/Migration/Version020300Date20210406114130.php create mode 100644 lib/Migration/Version020300Date20210406133704.php diff --git a/lib/Migration/Version020300Date20210406114130.php b/lib/Migration/Version020300Date20210406114130.php new file mode 100644 index 00000000..cada58d4 --- /dev/null +++ b/lib/Migration/Version020300Date20210406114130.php @@ -0,0 +1,78 @@ + + * + * @author Christian Hartmann + * + * @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\Forms\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version020300Date20210406114130 extends SimpleMigrationStep { + /** @var IDBConnection */ + protected $connection; + + /** + * @param IDBConnection $connection + */ + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + private const TYPE_BOOLEAN = 'boolean'; + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $table = $schema->getTable('forms_v2_questions'); + + if (!$table->hasColumn('is_required')) { + $table->addColumn('is_required', self::TYPE_BOOLEAN, [ + 'notnull' => false, + 'default' => 0, + ]); + + return $schema; + } + + return null; + } + + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { + $qb_update = $this->connection->getQueryBuilder(); + + $qb_update->update('forms_v2_questions') + ->set('is_required', 'mandatory'); + $qb_update->execute(); + } +} diff --git a/lib/Migration/Version020300Date20210406133704.php b/lib/Migration/Version020300Date20210406133704.php new file mode 100644 index 00000000..91ee0787 --- /dev/null +++ b/lib/Migration/Version020300Date20210406133704.php @@ -0,0 +1,55 @@ + + * + * @author Christian Hartmann + * + * @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\Forms\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version020300Date20210406133704 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $table = $schema->getTable('forms_v2_questions'); + + if ($table->hasColumn('mandatory') && $table->hasColumn('is_required')) { + $table->dropColumn('mandatory'); + + return $schema; + } + + return null; + } +}