diff --git a/apps/settings/lib/Controller/CheckSetupController.php b/apps/settings/lib/Controller/CheckSetupController.php index 07fb627dbd8..170c6a3870a 100644 --- a/apps/settings/lib/Controller/CheckSetupController.php +++ b/apps/settings/lib/Controller/CheckSetupController.php @@ -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() { diff --git a/core/Command/Db/AddMissingColumns.php b/core/Command/Db/AddMissingColumns.php index 8e6f439e0c4..93e346e498c 100644 --- a/core/Command/Db/AddMissingColumns.php +++ b/core/Command/Db/AddMissingColumns.php @@ -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('Adding additional ' . $missingColumn['columnName'] . ' column to the ' . $missingColumn['tableName'] . ' table, this can take some time...'); + $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('' . $missingColumn['tableName'] . ' table updated successfully.'); + } + } + } + } + + if (!$updated) { + $output->writeln('Done.'); + } + 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('Check columns of the comments table.'); $schema = new SchemaWrapper($this->connection); @@ -85,7 +121,7 @@ class AddMissingColumns extends Command { $table = $schema->getTable('comments'); if (!$table->hasColumn('reference_id')) { $output->writeln('Adding additional reference_id column to the comments table, this can take some time...'); - $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('Done.'); - } + return $updated; } } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index fcd1020be10..5b89d45cdfb 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 783e63550c0..2b71939fa6f 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -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 index 00000000000..1fb44e86842 --- /dev/null +++ b/lib/public/DB/Events/AddMissingColumnsEvent.php @@ -0,0 +1,60 @@ + + * + * @author Joas Schilling + * + * @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 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 */ + 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 + */ + public function getMissingColumns(): array { + return $this->missingColumns; + } +} diff --git a/lib/public/DB/Events/AddMissingIndicesEvent.php b/lib/public/DB/Events/AddMissingIndicesEvent.php index 139b776b136..58ba6b34a59 100644 --- a/lib/public/DB/Events/AddMissingIndicesEvent.php +++ b/lib/public/DB/Events/AddMissingIndicesEvent.php @@ -2,9 +2,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2023 Julius Härtl * - * @author Julius Härtl * * @license GNU AGPL version 3 or any later version * diff --git a/lib/public/IDBConnection.php b/lib/public/IDBConnection.php index bfc63b2aab0..bee2edad130 100644 --- a/lib/public/IDBConnection.php +++ b/lib/public/IDBConnection.php @@ -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';