From d2effbdb539f3c70c4b9cec3193bf50dfb880d57 Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Sat, 3 Apr 2021 23:51:25 +0200 Subject: [PATCH] Fix boolean columns nullable Signed-off-by: Jonas Rittershofer --- .../Version010200Date20200323141300.php | 6 +- .../Version020300Date20210403214012.php | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 lib/Migration/Version020300Date20210403214012.php diff --git a/lib/Migration/Version010200Date20200323141300.php b/lib/Migration/Version010200Date20200323141300.php index f58c616fb..e916dd0af 100644 --- a/lib/Migration/Version010200Date20200323141300.php +++ b/lib/Migration/Version010200Date20200323141300.php @@ -114,11 +114,11 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op 'comment' => 'unix-timestamp', ]); $table->addColumn('is_anonymous', self::TYPE_BOOLEAN, [ - 'notnull' => true, + 'notnull' => false, 'default' => 0, ]); $table->addColumn('submit_once', self::TYPE_BOOLEAN, [ - 'notnull' => true, + 'notnull' => false, 'default' => 0, ]); $table->setPrimaryKey(['id']); @@ -143,7 +143,7 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op 'length' => 256, ]); $table->addColumn('mandatory', self::TYPE_BOOLEAN, [ - 'notnull' => true, + 'notnull' => false, 'default' => 0, ]); $table->addColumn('text', self::TYPE_STRING, [ diff --git a/lib/Migration/Version020300Date20210403214012.php b/lib/Migration/Version020300Date20210403214012.php new file mode 100644 index 000000000..5be9aa9dd --- /dev/null +++ b/lib/Migration/Version020300Date20210403214012.php @@ -0,0 +1,64 @@ + + * + * @author Joas Schilling + * @author Jonas Rittershofer + * + * @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 Version020300Date20210403214012 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(); + + $result = $this->ensureColumnIsNullable($schema, 'forms_v2_forms', 'is_anonymous'); + $result |= $this->ensureColumnIsNullable($schema, 'forms_v2_forms', 'submit_once'); + $result |= $this->ensureColumnIsNullable($schema, 'forms_v2_questions', 'mandatory'); + + return $result ? $schema : null; + } + + protected function ensureColumnIsNullable(ISchemaWrapper $schema, string $tableName, string $columnName): bool { + $table = $schema->getTable($tableName); + $column = $table->getColumn($columnName); + + if ($column->getNotnull()) { + $column->setNotnull(false); + return true; + } + + return false; + } +}