]> source.dussan.org Git - nextcloud-server.git/commitdiff
feat(dispatcher): Add typed event for "db:add-missing-columns"
authorJoas Schilling <coding@schilljs.com>
Wed, 19 Jul 2023 20:36:59 +0000 (22:36 +0200)
committerJoas Schilling <coding@schilljs.com>
Mon, 24 Jul 2023 12:51:32 +0000 (14:51 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/settings/lib/Controller/CheckSetupController.php
core/Command/Db/AddMissingColumns.php
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/public/DB/Events/AddMissingColumnsEvent.php [new file with mode: 0644]
lib/public/DB/Events/AddMissingIndicesEvent.php
lib/public/IDBConnection.php

index 07fb627dbd80ac13157435b08cb6ff3ee028a224..170c6a3870a9c732bebd88862dd35fec31392d85 100644 (file)
@@ -74,6 +74,7 @@ use OCP\AppFramework\Http\Attribute\IgnoreOpenAPI;
 use OCP\AppFramework\Http\DataDisplayResponse;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\AppFramework\Http\RedirectResponse;
+use OCP\DB\Events\AddMissingColumnsEvent;
 use OCP\DB\Events\AddMissingIndicesEvent;
 use OCP\DB\Types;
 use OCP\EventDispatcher\IEventDispatcher;
@@ -583,12 +584,28 @@ Raw output
        }
 
        protected function hasMissingColumns(): array {
-               $indexInfo = new MissingColumnInformation();
+               $columnInfo = new MissingColumnInformation();
                // Dispatch event so apps can also hint for pending index updates if needed
-               $event = new GenericEvent($indexInfo);
+               $event = new GenericEvent($columnInfo);
                $this->dispatcher->dispatch(IDBConnection::CHECK_MISSING_COLUMNS_EVENT, $event);
 
-               return $indexInfo->getListOfMissingColumns();
+               $event = new AddMissingColumnsEvent();
+               $this->eventDispatcher->dispatchTyped($event);
+               $missingColumns = $event->getMissingColumns();
+
+               if (!empty($missingColumns)) {
+                       $schema = new SchemaWrapper(\OCP\Server::get(Connection::class));
+                       foreach ($missingColumns as $missingColumn) {
+                               if ($schema->hasTable($missingColumn['tableName'])) {
+                                       $table = $schema->getTable($missingColumn['tableName']);
+                                       if (!$table->hasColumn($missingColumn['columnName'])) {
+                                               $columnInfo->addHintForMissingColumn($missingColumn['tableName'], $missingColumn['columnName']);
+                                       }
+                               }
+                       }
+               }
+
+               return $columnInfo->getListOfMissingColumns();
        }
 
        protected function isSqliteUsed() {
index 8e6f439e0c4c367d0c05542163b3badd645fdee6..93e346e498cbcd3093880666ec66489be860b525 100644 (file)
@@ -28,6 +28,9 @@ namespace OC\Core\Command\Db;
 
 use OC\DB\Connection;
 use OC\DB\SchemaWrapper;
+use OCP\DB\Events\AddMissingColumnsEvent;
+use OCP\DB\Types;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IDBConnection;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
@@ -47,7 +50,8 @@ use Symfony\Component\EventDispatcher\GenericEvent;
 class AddMissingColumns extends Command {
        public function __construct(
                private Connection $connection,
-               private EventDispatcherInterface $dispatcher,
+               private EventDispatcherInterface $legacyDispatcher,
+               private IEventDispatcher $dispatcher,
        ) {
                parent::__construct();
        }
@@ -60,22 +64,54 @@ class AddMissingColumns extends Command {
        }
 
        protected function execute(InputInterface $input, OutputInterface $output): int {
-               $this->addCoreColumns($output, $input->getOption('dry-run'));
+               $dryRun = $input->getOption('dry-run');
+
+               $updated = $this->addCoreColumns($output, $dryRun);
 
                // Dispatch event so apps can also update columns if needed
                $event = new GenericEvent($output);
-               $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_COLUMNS_EVENT, $event);
+               $this->legacyDispatcher->dispatch(IDBConnection::ADD_MISSING_COLUMNS_EVENT, $event);
+
+               $event = new AddMissingColumnsEvent();
+               $this->dispatcher->dispatchTyped($event);
+               $missingColumns = $event->getMissingColumns();
+
+               if (!empty($missingColumns)) {
+                       $schema = new SchemaWrapper($this->connection);
+
+                       foreach ($missingColumns as $missingColumn) {
+                               if ($schema->hasTable($missingColumn['tableName'])) {
+                                       $table = $schema->getTable($missingColumn['tableName']);
+                                       if (!$table->hasColumn($missingColumn['columnName'])) {
+                                               $output->writeln('<info>Adding additional ' . $missingColumn['columnName'] . ' column to the ' . $missingColumn['tableName'] . ' table, this can take some time...</info>');
+                                               $table->addColumn($missingColumn['columnName'], $missingColumn['typeName'], $missingColumn['options']);
+                                               $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
+                                               if ($dryRun && $sqlQueries !== null) {
+                                                       $output->writeln($sqlQueries);
+                                               }
+                                               $updated = true;
+                                               $output->writeln('<info>' . $missingColumn['tableName'] . ' table updated successfully.</info>');
+                                       }
+                               }
+                       }
+               }
+
+               if (!$updated) {
+                       $output->writeln('<info>Done.</info>');
+               }
+
                return 0;
        }
 
        /**
-        * add missing indices to the share table
+        * Add missing column for core tables
         *
         * @param OutputInterface $output
         * @param bool $dryRun If true, will return the sql queries instead of running them.
+        * @return bool True when the schema changed
         * @throws \Doctrine\DBAL\Schema\SchemaException
         */
-       private function addCoreColumns(OutputInterface $output, bool $dryRun): void {
+       private function addCoreColumns(OutputInterface $output, bool $dryRun): bool {
                $output->writeln('<info>Check columns of the comments table.</info>');
 
                $schema = new SchemaWrapper($this->connection);
@@ -85,7 +121,7 @@ class AddMissingColumns extends Command {
                        $table = $schema->getTable('comments');
                        if (!$table->hasColumn('reference_id')) {
                                $output->writeln('<info>Adding additional reference_id column to the comments table, this can take some time...</info>');
-                               $table->addColumn('reference_id', 'string', [
+                               $table->addColumn('reference_id', Types::STRING, [
                                        'notnull' => false,
                                        'length' => 64,
                                ]);
@@ -98,8 +134,6 @@ class AddMissingColumns extends Command {
                        }
                }
 
-               if (!$updated) {
-                       $output->writeln('<info>Done.</info>');
-               }
+               return $updated;
        }
 }
index fcd1020be10aac80721caf3a189257410cb3a1ac..5b89d45cdfb480cbf51435383e27407235039d4a 100644 (file)
@@ -210,6 +210,7 @@ return array(
     'OCP\\Contacts\\ContactsMenu\\IProvider' => $baseDir . '/lib/public/Contacts/ContactsMenu/IProvider.php',
     'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => $baseDir . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
     'OCP\\Contacts\\IManager' => $baseDir . '/lib/public/Contacts/IManager.php',
+    'OCP\\DB\\Events\\AddMissingColumnsEvent' => $baseDir . '/lib/public/DB/Events/AddMissingColumnsEvent.php',
     'OCP\\DB\\Events\\AddMissingIndicesEvent' => $baseDir . '/lib/public/DB/Events/AddMissingIndicesEvent.php',
     'OCP\\DB\\Exception' => $baseDir . '/lib/public/DB/Exception.php',
     'OCP\\DB\\IPreparedStatement' => $baseDir . '/lib/public/DB/IPreparedStatement.php',
index 783e63550c072272d4d6a2bbcb5450e3a3d2c45c..2b71939fa6f4b4f91e0b84bcb0629fac11898ff5 100644 (file)
@@ -243,6 +243,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
         'OCP\\Contacts\\ContactsMenu\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IProvider.php',
         'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => __DIR__ . '/../../..' . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
         'OCP\\Contacts\\IManager' => __DIR__ . '/../../..' . '/lib/public/Contacts/IManager.php',
+        'OCP\\DB\\Events\\AddMissingColumnsEvent' => __DIR__ . '/../../..' . '/lib/public/DB/Events/AddMissingColumnsEvent.php',
         'OCP\\DB\\Events\\AddMissingIndicesEvent' => __DIR__ . '/../../..' . '/lib/public/DB/Events/AddMissingIndicesEvent.php',
         'OCP\\DB\\Exception' => __DIR__ . '/../../..' . '/lib/public/DB/Exception.php',
         'OCP\\DB\\IPreparedStatement' => __DIR__ . '/../../..' . '/lib/public/DB/IPreparedStatement.php',
diff --git a/lib/public/DB/Events/AddMissingColumnsEvent.php b/lib/public/DB/Events/AddMissingColumnsEvent.php
new file mode 100644 (file)
index 0000000..1fb44e8
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\DB\Events;
+
+/**
+ * Event to allow apps to register information about missing database columns
+ *
+ * This event will be dispatched for checking on the admin settings and when running
+ * occ db:add-missing-columns which will then create those columns
+ *
+ * @since 28.0.0
+ */
+class AddMissingColumnsEvent extends \OCP\EventDispatcher\Event {
+       /** @var array<array-key, array{tableName: string, columnName: string, typeName: string, options: array{}}> */
+       private array $missingColumns = [];
+
+       /**
+        * @param mixed[] $options
+        * @since 28.0.0
+        */
+       public function addMissingColumn(string $tableName, string $columnName, string $typeName, array $options): void {
+               $this->missingColumns[] = [
+                       'tableName' => $tableName,
+                       'columnName' => $columnName,
+                       'typeName' => $typeName,
+                       'options' => $options,
+               ];
+       }
+
+       /**
+        * @since 28.0.0
+        * @return array<array-key, array{tableName: string, columnName: string, typeName: string, options: array{}}>
+        */
+       public function getMissingColumns(): array {
+               return $this->missingColumns;
+       }
+}
index 139b776b1360ef0d683b7989b83122a505b4e0aa..58ba6b34a59eec51c16a6b5ea73af678b05675bb 100644 (file)
@@ -2,9 +2,9 @@
 
 declare(strict_types=1);
 /**
- * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net
+ * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
  *
- * @author Julius Härtl <jus@bitgrid.net
+ * @author Julius Härtl <jus@bitgrid.net>
  *
  * @license GNU AGPL version 3 or any later version
  *
index bfc63b2aab09452a4c09905988c201f5f1dbd58d..bee2edad130ec15531abc5d706f44f904d977aba 100644 (file)
@@ -34,6 +34,7 @@
 namespace OCP;
 
 use Doctrine\DBAL\Schema\Schema;
+use OCP\DB\Events\AddMissingColumnsEvent;
 use OCP\DB\Events\AddMissingIndicesEvent;
 use OCP\DB\Exception;
 use OCP\DB\IPreparedStatement;
@@ -67,12 +68,12 @@ interface IDBConnection {
        public const CHECK_MISSING_PRIMARY_KEYS_EVENT = self::class . '::CHECK_MISSING_PRIMARY_KEYS';
 
        /**
-        * @deprecated 22.0.0 this is an internal event
+        * @deprecated 22.0.0 this is an internal event, use {@see AddMissingColumnsEvent} instead
         */
        public const ADD_MISSING_COLUMNS_EVENT = self::class . '::ADD_MISSING_COLUMNS';
 
        /**
-        * @deprecated 22.0.0 this is an internal event
+        * @deprecated 22.0.0 this is an internal event, use {@see AddMissingColumnsEvent} instead
         */
        public const CHECK_MISSING_COLUMNS_EVENT = self::class . '::CHECK_MISSING_COLUMNS';