From 7417ce590c113dc948b9bdb901978c0a85b1918c Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Tue, 29 Mar 2022 20:49:10 +0200 Subject: [PATCH 1/2] Move to NC-Internal Db-Types Signed-off-by: Jonas Rittershofer --- .../Version010200Date20200323141300.php | 57 +++++++++---------- .../Version020300Date20210406114130.php | 5 +- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/lib/Migration/Version010200Date20200323141300.php b/lib/Migration/Version010200Date20200323141300.php index 85f8f8adc..56d3387ba 100644 --- a/lib/Migration/Version010200Date20200323141300.php +++ b/lib/Migration/Version010200Date20200323141300.php @@ -26,6 +26,7 @@ use OCP\DB\ISchemaWrapper; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\DB\Types; use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\SimpleMigrationStep; @@ -54,10 +55,8 @@ class Version010200Date20200323141300 extends SimpleMigrationStep { 'dropdown' => 'multiple_unique' ]; - private const TYPE_BOOLEAN = 'boolean'; - private const TYPE_INTEGER = 'integer'; + // Adding Type_json, that is recommended in DBAL private const TYPE_JSON = 'json'; - private const TYPE_STRING = 'string'; /** * @param IDBConnection $connection @@ -81,43 +80,43 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op if (!$schema->hasTable('forms_v2_forms')) { $table = $schema->createTable('forms_v2_forms'); - $table->addColumn('id', self::TYPE_INTEGER, [ + $table->addColumn('id', Types::INTEGER, [ 'autoincrement' => true, 'notnull' => true, ]); - $table->addColumn('hash', self::TYPE_STRING, [ + $table->addColumn('hash', Types::STRING, [ 'notnull' => true, 'length' => 64, ]); - $table->addColumn('title', self::TYPE_STRING, [ + $table->addColumn('title', Types::STRING, [ 'notnull' => true, 'length' => 256, ]); - $table->addColumn('description', self::TYPE_STRING, [ + $table->addColumn('description', Types::STRING, [ 'notnull' => false, 'length' => 8192, ]); - $table->addColumn('owner_id', self::TYPE_STRING, [ + $table->addColumn('owner_id', Types::STRING, [ 'notnull' => true, 'length' => 64, ]); $table->addColumn('access_json', self::TYPE_JSON, [ 'notnull' => false, ]); - $table->addColumn('created', self::TYPE_INTEGER, [ + $table->addColumn('created', Types::INTEGER, [ 'notnull' => false, 'comment' => 'unix-timestamp', ]); - $table->addColumn('expires', self::TYPE_INTEGER, [ + $table->addColumn('expires', Types::INTEGER, [ 'notnull' => false, 'default' => 0, 'comment' => 'unix-timestamp', ]); - $table->addColumn('is_anonymous', self::TYPE_BOOLEAN, [ + $table->addColumn('is_anonymous', Types::BOOLEAN, [ 'notnull' => false, 'default' => 0, ]); - $table->addColumn('submit_once', self::TYPE_BOOLEAN, [ + $table->addColumn('submit_once', Types::BOOLEAN, [ 'notnull' => false, 'default' => 0, ]); @@ -127,26 +126,26 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op if (!$schema->hasTable('forms_v2_questions')) { $table = $schema->createTable('forms_v2_questions'); - $table->addColumn('id', self::TYPE_INTEGER, [ + $table->addColumn('id', Types::INTEGER, [ 'autoincrement' => true, 'notnull' => true, ]); - $table->addColumn('form_id', self::TYPE_INTEGER, [ + $table->addColumn('form_id', Types::INTEGER, [ 'notnull' => true, ]); - $table->addColumn('order', self::TYPE_INTEGER, [ + $table->addColumn('order', Types::INTEGER, [ 'notnull' => true, 'default' => 1, ]); - $table->addColumn('type', self::TYPE_STRING, [ + $table->addColumn('type', Types::STRING, [ 'notnull' => true, 'length' => 256, ]); - $table->addColumn('mandatory', self::TYPE_BOOLEAN, [ + $table->addColumn('mandatory', Types::BOOLEAN, [ 'notnull' => false, 'default' => 0, ]); - $table->addColumn('text', self::TYPE_STRING, [ + $table->addColumn('text', Types::STRING, [ 'notnull' => true, 'length' => 2048, ]); @@ -155,14 +154,14 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op if (!$schema->hasTable('forms_v2_options')) { $table = $schema->createTable('forms_v2_options'); - $table->addColumn('id', self::TYPE_INTEGER, [ + $table->addColumn('id', Types::INTEGER, [ 'autoincrement' => true, 'notnull' => true, ]); - $table->addColumn('question_id', self::TYPE_INTEGER, [ + $table->addColumn('question_id', Types::INTEGER, [ 'notnull' => true, ]); - $table->addColumn('text', self::TYPE_STRING, [ + $table->addColumn('text', Types::STRING, [ 'notnull' => true, 'length' => 1024, ]); @@ -171,18 +170,18 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op if (!$schema->hasTable('forms_v2_submissions')) { $table = $schema->createTable('forms_v2_submissions'); - $table->addColumn('id', self::TYPE_INTEGER, [ + $table->addColumn('id', Types::INTEGER, [ 'autoincrement' => true, 'notnull' => true, ]); - $table->addColumn('form_id', self::TYPE_INTEGER, [ + $table->addColumn('form_id', Types::INTEGER, [ 'notnull' => true, ]); - $table->addColumn('user_id', self::TYPE_STRING, [ + $table->addColumn('user_id', Types::STRING, [ 'notnull' => true, 'length' => 64, ]); - $table->addColumn('timestamp', self::TYPE_INTEGER, [ + $table->addColumn('timestamp', Types::INTEGER, [ 'notnull' => false, 'comment' => 'unix-timestamp', ]); @@ -191,17 +190,17 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op if (!$schema->hasTable('forms_v2_answers')) { $table = $schema->createTable('forms_v2_answers'); - $table->addColumn('id', self::TYPE_INTEGER, [ + $table->addColumn('id', Types::INTEGER, [ 'autoincrement' => true, 'notnull' => true, ]); - $table->addColumn('submission_id', self::TYPE_INTEGER, [ + $table->addColumn('submission_id', Types::INTEGER, [ 'notnull' => true, ]); - $table->addColumn('question_id', self::TYPE_INTEGER, [ + $table->addColumn('question_id', Types::INTEGER, [ 'notnull' => true, ]); - $table->addColumn('text', self::TYPE_STRING, [ + $table->addColumn('text', Types::STRING, [ 'notnull' => true, 'length' => 4096, ]); diff --git a/lib/Migration/Version020300Date20210406114130.php b/lib/Migration/Version020300Date20210406114130.php index e7773d722..a2a298d45 100644 --- a/lib/Migration/Version020300Date20210406114130.php +++ b/lib/Migration/Version020300Date20210406114130.php @@ -27,6 +27,7 @@ use Closure; use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; use OCP\IDBConnection; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; @@ -42,8 +43,6 @@ public function __construct(IDBConnection $connection) { $this->connection = $connection; } - private const TYPE_BOOLEAN = 'boolean'; - /** * @param IOutput $output * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` @@ -57,7 +56,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table = $schema->getTable('forms_v2_questions'); if (!$table->hasColumn('is_required')) { - $table->addColumn('is_required', self::TYPE_BOOLEAN, [ + $table->addColumn('is_required', Types::BOOLEAN, [ 'notnull' => false, 'default' => 0, ]); From 056692f4a72f501bf137add341e0493a228ffc81 Mon Sep 17 00:00:00 2001 From: Jonas Rittershofer Date: Sat, 2 Apr 2022 15:02:33 +0200 Subject: [PATCH 2/2] Migrate long text columns Signed-off-by: Jonas Rittershofer --- .../Version030000Date20220402100057.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/Migration/Version030000Date20220402100057.php diff --git a/lib/Migration/Version030000Date20220402100057.php b/lib/Migration/Version030000Date20220402100057.php new file mode 100644 index 000000000..b9c43a4ac --- /dev/null +++ b/lib/Migration/Version030000Date20220402100057.php @@ -0,0 +1,65 @@ + + * + * @author Jonas Rittershofer + * + * @license AGPL-3.0-or-later + * + * 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 Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version030000Date20220402100057 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(); + + $update_necesssary = false; + + // Change Type of Description from string to text. Necessary due to length restrictions. + $column = $schema->getTable('forms_v2_forms')->getColumn('description'); + if ($column->getType() === Type::getType(Types::STRING)) { + $column->setType(Type::getType(Types::TEXT)); + $update_necesssary = true; + } + + // Change Type of Answer-Text from string to text. Necessary due to length restrictions. + $column = $schema->getTable('forms_v2_answers')->getColumn('text'); + if ($column->getType() === Type::getType(Types::STRING)) { + $column->setType(Type::getType(Types::TEXT)); + $update_necesssary = true; + } + + return $update_necesssary ? $schema : null; + } +}