From aa6504765023cc0064d9bb3b935b3c9390c6bdd6 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 14 Feb 2024 11:55:35 +0100 Subject: [PATCH] feat(db): database structure for contexts Signed-off-by: Arthur Schiwon --- .../Version000800Date20240213123743.php | 133 ++++++++++++++++++ psalm.xml | 1 + 2 files changed, 134 insertions(+) create mode 100644 lib/Migration/Version000800Date20240213123743.php diff --git a/lib/Migration/Version000800Date20240213123743.php b/lib/Migration/Version000800Date20240213123743.php new file mode 100644 index 000000000..059fbbd23 --- /dev/null +++ b/lib/Migration/Version000800Date20240213123743.php @@ -0,0 +1,133 @@ + + * + * @author Arthur Schiwon + * + * @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\Tables\Migration; + +use Closure; +use Doctrine\DBAL\Schema\SchemaException; +use Doctrine\DBAL\Schema\Table; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version000800Date20240213123743 extends SimpleMigrationStep { + + protected const PREFIX = 'tables_contexts_'; + + /** + * @throws SchemaException + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + // Introduction of Contexts tables + + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $this->haveContextTable($schema); + $this->haveContextNodeRelationTable($schema); + $this->havePageTable($schema); + $this->havePageContentTable($schema); + $this->haveNavigationTable($schema); + + return $schema; + } + + protected function shouldAddTable(string $tableName, ISchemaWrapper $schema): ?Table { + return !$schema->hasTable($tableName) ? $schema->createTable($tableName) : null; + } + + /** + * @throws SchemaException + */ + protected function haveContextTable(ISchemaWrapper $schema): void { + if ($table = $this->shouldAddTable(self::PREFIX . 'context', $schema)) { + $table->addColumn('id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('name', Types::STRING, ['notnull' => true, 'length' => 200]); + $table->addColumn('icon', Types::STRING, ['notnull' => true, 'length' => 64]); + $table->addColumn('description', Types::TEXT); + $table->addColumn('owner_id', Types::STRING, ['notnull' => true, 'length' => 64]); + $table->addColumn('owner_type', Types::INTEGER, ['notnull' => true]); + + $table->setPrimaryKey(['id']); + } + } + + /** + * @throws SchemaException + */ + protected function haveContextNodeRelationTable(ISchemaWrapper $schema): void { + if ($table = $this->shouldAddTable(self::PREFIX . 'rel_context_node', $schema)) { + $table->addColumn('id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('context_id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('node_id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('node_type', Types::STRING, ['notnull' => true, 'length' => 50]); + $table->addColumn('permissions', Types::INTEGER, ['notnull' => true]); + + $table->setPrimaryKey(['id']); + } + } + + /** + * @throws SchemaException + */ + protected function havePageTable(ISchemaWrapper $schema): void { + if ($table = $this->shouldAddTable(self::PREFIX . 'page', $schema)) { + $table->addColumn('id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('context_id', Types::STRING, ['notnull' => true]); + $table->addColumn('page_type', Types::STRING, ['notnull' => true, 'length' => 32]); + + $table->setPrimaryKey(['id']); + } + } + + /** + * @throws SchemaException + */ + protected function havePageContentTable(ISchemaWrapper $schema): void { + if ($table = $this->shouldAddTable(self::PREFIX . 'page_content', $schema)) { + $table->addColumn('id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('page_id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('node_rel_id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('order', Types::INTEGER, ['notnull' => true]); + + $table->setPrimaryKey(['id']); + } + } + + /** + * @throws SchemaException + */ + protected function haveNavigationTable(ISchemaWrapper $schema): void { + if ($table = $this->shouldAddTable(self::PREFIX . 'navigation', $schema)) { + $table->addColumn('share_id', Types::INTEGER, ['notnull' => true]); + $table->addColumn('display_mode', Types::INTEGER, ['notnull' => true]); + $table->addColumn('user_id', Types::STRING, ['notnull' => true, 'length' => 64, 'default' => '']); + + $table->setPrimaryKey(['share_id', 'user_id']); + } + } +} diff --git a/psalm.xml b/psalm.xml index dc1fbe1d2..745419a77 100644 --- a/psalm.xml +++ b/psalm.xml @@ -34,6 +34,7 @@ +