From 18db96c304c2b2d93c3effc2c1d5b9320fe78ce1 Mon Sep 17 00:00:00 2001 From: Julius Härtl Date: Fri, 14 Jul 2023 10:34:09 +0200 Subject: feat: Add public event for missing indices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- core/Command/Db/AddMissingIndices.php | 34 ++++++++++++++++++++++++++++++++-- core/register_command.php | 2 +- 2 files changed, 33 insertions(+), 3 deletions(-) (limited to 'core') diff --git a/core/Command/Db/AddMissingIndices.php b/core/Command/Db/AddMissingIndices.php index 452bcbad5dc..3a680921777 100644 --- a/core/Command/Db/AddMissingIndices.php +++ b/core/Command/Db/AddMissingIndices.php @@ -36,6 +36,8 @@ namespace OC\Core\Command\Db; use Doctrine\DBAL\Platforms\PostgreSQL94Platform; use OC\DB\Connection; use OC\DB\SchemaWrapper; +use OCP\DB\Events\AddMissingIndicesEvent; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IDBConnection; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputOption; @@ -54,12 +56,14 @@ use Symfony\Component\EventDispatcher\GenericEvent; */ class AddMissingIndices extends Command { private Connection $connection; + private IEventDispatcher $eventDispatcher; private EventDispatcherInterface $dispatcher; - public function __construct(Connection $connection, EventDispatcherInterface $dispatcher) { + public function __construct(Connection $connection, IEventDispatcher $eventDispatcher, EventDispatcherInterface $dispatcher) { parent::__construct(); $this->connection = $connection; + $this->eventDispatcher = $eventDispatcher; $this->dispatcher = $dispatcher; } @@ -71,11 +75,37 @@ class AddMissingIndices extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { - $this->addCoreIndexes($output, $input->getOption('dry-run')); + $dryRun = $input->getOption('dry-run'); + + $this->addCoreIndexes($output, $dryRun); // Dispatch event so apps can also update indexes if needed $event = new GenericEvent($output); $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_INDEXES_EVENT, $event); + + $event = new AddMissingIndicesEvent(); + $this->eventDispatcher->dispatchTyped($event); + + $missingIndices = $event->getMissingIndices(); + if ($missingIndices !== []) { + $schema = new SchemaWrapper($this->connection); + + foreach ($missingIndices as $missingIndex) { + if ($schema->hasTable($missingIndex['tableName'])) { + $table = $schema->getTable($missingIndex['tableName']); + if (!$table->hasIndex($missingIndex['indexName'])) { + $output->writeln('Adding additional ' . $missingIndex['indexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...'); + $table->addIndex($missingIndex['columns'], $missingIndex['indexName']); + $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun); + if ($dryRun && $sqlQueries !== null) { + $output->writeln($sqlQueries); + } + $output->writeln('' . $table->getName() . ' table updated successfully.'); + } + } + } + } + return 0; } diff --git a/core/register_command.php b/core/register_command.php index 8f600d7b894..32cd4099618 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -109,7 +109,7 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->get(LoggerInterface::class))); $application->add(new OC\Core\Command\Db\ConvertFilecacheBigInt(\OC::$server->get(\OC\DB\Connection::class))); - $application->add(new OC\Core\Command\Db\AddMissingIndices(\OC::$server->get(\OC\DB\Connection::class), \OC::$server->getEventDispatcher())); + $application->add(\OCP\Server::get(\OC\Core\Command\Db\AddMissingIndices::class)); $application->add(new OC\Core\Command\Db\AddMissingColumns(\OC::$server->get(\OC\DB\Connection::class), \OC::$server->getEventDispatcher())); $application->add(new OC\Core\Command\Db\AddMissingPrimaryKeys(\OC::$server->get(\OC\DB\Connection::class), \OC::$server->getEventDispatcher())); -- cgit v1.2.3