feat(dispatcher): Add typed event for "db:add-missing-columns"

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/39487/head
Joas Schilling 3 years ago
parent 7548e62181
commit 2eded24eff
No known key found for this signature in database
GPG Key ID: C400AAF20C1BB6FC
  1. 23
      apps/settings/lib/Controller/CheckSetupController.php
  2. 52
      core/Command/Db/AddMissingColumns.php
  3. 1
      lib/composer/composer/autoload_classmap.php
  4. 1
      lib/composer/composer/autoload_static.php
  5. 60
      lib/public/DB/Events/AddMissingColumnsEvent.php
  6. 4
      lib/public/DB/Events/AddMissingIndicesEvent.php
  7. 5
      lib/public/IDBConnection.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() {

@ -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;
}
}

@ -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',

@ -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',

@ -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;
}
}

@ -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
*

@ -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';

Loading…
Cancel
Save