aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Db/AddMissingIndices.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Db/AddMissingIndices.php')
-rw-r--r--core/Command/Db/AddMissingIndices.php82
1 files changed, 49 insertions, 33 deletions
diff --git a/core/Command/Db/AddMissingIndices.php b/core/Command/Db/AddMissingIndices.php
index bd47fea20fe..eec0aedce11 100644
--- a/core/Command/Db/AddMissingIndices.php
+++ b/core/Command/Db/AddMissingIndices.php
@@ -3,33 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Mario Danic <mario@lovelyhq.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Citharel <nextcloud@tcit.fr>
- *
- * @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/>.
- *
+ * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Db;
@@ -62,7 +37,7 @@ class AddMissingIndices extends Command {
$this
->setName('db:add-missing-indices')
->setDescription('Add missing indices to the database tables')
- ->addOption('dry-run', null, InputOption::VALUE_NONE, "Output the SQL queries instead of running them.");
+ ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Output the SQL queries instead of running them.');
}
protected function execute(InputInterface $input, OutputInterface $output): int {
@@ -73,7 +48,9 @@ class AddMissingIndices extends Command {
$this->dispatcher->dispatchTyped($event);
$missingIndices = $event->getMissingIndices();
- if ($missingIndices !== []) {
+ $toReplaceIndices = $event->getIndicesToReplace();
+
+ if ($missingIndices !== [] || $toReplaceIndices !== []) {
$schema = new SchemaWrapper($this->connection);
foreach ($missingIndices as $missingIndex) {
@@ -97,15 +74,54 @@ class AddMissingIndices extends Command {
$table->addIndex($missingIndex['columns'], $missingIndex['indexName'], [], $missingIndex['options']);
}
-
- $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
- if ($dryRun && $sqlQueries !== null) {
- $output->writeln($sqlQueries);
+ if (!$dryRun) {
+ $this->connection->migrateToSchema($schema->getWrappedSchema());
}
$output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>');
}
}
}
+
+ foreach ($toReplaceIndices as $toReplaceIndex) {
+ if ($schema->hasTable($toReplaceIndex['tableName'])) {
+ $table = $schema->getTable($toReplaceIndex['tableName']);
+
+ if ($table->hasIndex($toReplaceIndex['newIndexName'])) {
+ continue;
+ }
+
+ $output->writeln('<info>Adding additional ' . $toReplaceIndex['newIndexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>');
+
+ if ($toReplaceIndex['uniqueIndex']) {
+ $table->addUniqueIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], $toReplaceIndex['options']);
+ } else {
+ $table->addIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], [], $toReplaceIndex['options']);
+ }
+
+ if (!$dryRun) {
+ $this->connection->migrateToSchema($schema->getWrappedSchema());
+ }
+
+ foreach ($toReplaceIndex['oldIndexNames'] as $oldIndexName) {
+ if ($table->hasIndex($oldIndexName)) {
+ $output->writeln('<info>Removing ' . $oldIndexName . ' index from the ' . $table->getName() . ' table</info>');
+ $table->dropIndex($oldIndexName);
+ }
+ }
+
+ if (!$dryRun) {
+ $this->connection->migrateToSchema($schema->getWrappedSchema());
+ }
+ $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>');
+ }
+ }
+
+ if ($dryRun) {
+ $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
+ if ($sqlQueries !== null) {
+ $output->writeln($sqlQueries);
+ }
+ }
}
return 0;