diff options
author | Julius Härtl <jus@bitgrid.net> | 2023-07-14 10:34:09 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2023-07-14 13:11:22 +0200 |
commit | 925bcebcb6230ffdf74c39302c09da9493db7411 (patch) | |
tree | 90be852b8c6f061887ec49989550e133f2178aa8 /core/Command | |
parent | 99aefbd75a0fea6fb722290966452f3647aa5a63 (diff) | |
download | nextcloud-server-925bcebcb6230ffdf74c39302c09da9493db7411.tar.gz nextcloud-server-925bcebcb6230ffdf74c39302c09da9493db7411.zip |
feat: Add public event for missing indices
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'core/Command')
-rw-r--r-- | core/Command/Db/AddMissingIndices.php | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/core/Command/Db/AddMissingIndices.php b/core/Command/Db/AddMissingIndices.php index 62109867913..15cec415b62 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; @@ -55,6 +57,7 @@ use Symfony\Component\EventDispatcher\GenericEvent; class AddMissingIndices extends Command { public function __construct( private Connection $connection, + private IEventDispatcher $eventDispatcher, private EventDispatcherInterface $dispatcher, ) { parent::__construct(); @@ -68,11 +71,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('<info>Adding additional ' . $missingIndex['indexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); + $table->addIndex($missingIndex['columns'], $missingIndex['indexName']); + $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun); + if ($dryRun && $sqlQueries !== null) { + $output->writeln($sqlQueries); + } + $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); + } + } + } + } + return 0; } |