@@ -26,104 +26,104 @@ |
||
| 26 | 26 | * @package OC\Core\Command\Db |
| 27 | 27 | */ |
| 28 | 28 | class AddMissingIndices extends Command { |
| 29 | - public function __construct( |
|
| 30 | - private Connection $connection, |
|
| 31 | - private IEventDispatcher $dispatcher, |
|
| 32 | - ) { |
|
| 33 | - parent::__construct(); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - protected function configure() { |
|
| 37 | - $this |
|
| 38 | - ->setName('db:add-missing-indices') |
|
| 39 | - ->setDescription('Add missing indices to the database tables') |
|
| 40 | - ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Output the SQL queries instead of running them.'); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 44 | - $dryRun = $input->getOption('dry-run'); |
|
| 45 | - |
|
| 46 | - // Dispatch event so apps can also update indexes if needed |
|
| 47 | - $event = new AddMissingIndicesEvent(); |
|
| 48 | - $this->dispatcher->dispatchTyped($event); |
|
| 49 | - |
|
| 50 | - $missingIndices = $event->getMissingIndices(); |
|
| 51 | - $toReplaceIndices = $event->getIndicesToReplace(); |
|
| 52 | - |
|
| 53 | - if ($missingIndices !== [] || $toReplaceIndices !== []) { |
|
| 54 | - $schema = new SchemaWrapper($this->connection); |
|
| 55 | - |
|
| 56 | - foreach ($missingIndices as $missingIndex) { |
|
| 57 | - if ($schema->hasTable($missingIndex['tableName'])) { |
|
| 58 | - $table = $schema->getTable($missingIndex['tableName']); |
|
| 59 | - if (!$table->hasIndex($missingIndex['indexName'])) { |
|
| 60 | - $output->writeln('<info>Adding additional ' . $missingIndex['indexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); |
|
| 61 | - |
|
| 62 | - if ($missingIndex['dropUnnamedIndex']) { |
|
| 63 | - foreach ($table->getIndexes() as $index) { |
|
| 64 | - $columns = $index->getColumns(); |
|
| 65 | - if ($columns === $missingIndex['columns']) { |
|
| 66 | - $table->dropIndex($index->getName()); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - if ($missingIndex['uniqueIndex']) { |
|
| 72 | - $table->addUniqueIndex($missingIndex['columns'], $missingIndex['indexName'], $missingIndex['options']); |
|
| 73 | - } else { |
|
| 74 | - $table->addIndex($missingIndex['columns'], $missingIndex['indexName'], [], $missingIndex['options']); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - if (!$dryRun) { |
|
| 78 | - $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
| 79 | - } |
|
| 80 | - $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - foreach ($toReplaceIndices as $toReplaceIndex) { |
|
| 86 | - if ($schema->hasTable($toReplaceIndex['tableName'])) { |
|
| 87 | - $table = $schema->getTable($toReplaceIndex['tableName']); |
|
| 88 | - |
|
| 89 | - if ($table->hasIndex($toReplaceIndex['newIndexName'])) { |
|
| 90 | - continue; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $output->writeln('<info>Adding additional ' . $toReplaceIndex['newIndexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); |
|
| 94 | - |
|
| 95 | - if ($toReplaceIndex['uniqueIndex']) { |
|
| 96 | - $table->addUniqueIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], $toReplaceIndex['options']); |
|
| 97 | - } else { |
|
| 98 | - $table->addIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], [], $toReplaceIndex['options']); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if (!$dryRun) { |
|
| 102 | - $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - foreach ($toReplaceIndex['oldIndexNames'] as $oldIndexName) { |
|
| 106 | - if ($table->hasIndex($oldIndexName)) { |
|
| 107 | - $output->writeln('<info>Removing ' . $oldIndexName . ' index from the ' . $table->getName() . ' table</info>'); |
|
| 108 | - $table->dropIndex($oldIndexName); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if (!$dryRun) { |
|
| 113 | - $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
| 114 | - } |
|
| 115 | - $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - if ($dryRun) { |
|
| 120 | - $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun); |
|
| 121 | - if ($sqlQueries !== null) { |
|
| 122 | - $output->writeln($sqlQueries); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return 0; |
|
| 128 | - } |
|
| 29 | + public function __construct( |
|
| 30 | + private Connection $connection, |
|
| 31 | + private IEventDispatcher $dispatcher, |
|
| 32 | + ) { |
|
| 33 | + parent::__construct(); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + protected function configure() { |
|
| 37 | + $this |
|
| 38 | + ->setName('db:add-missing-indices') |
|
| 39 | + ->setDescription('Add missing indices to the database tables') |
|
| 40 | + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Output the SQL queries instead of running them.'); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 44 | + $dryRun = $input->getOption('dry-run'); |
|
| 45 | + |
|
| 46 | + // Dispatch event so apps can also update indexes if needed |
|
| 47 | + $event = new AddMissingIndicesEvent(); |
|
| 48 | + $this->dispatcher->dispatchTyped($event); |
|
| 49 | + |
|
| 50 | + $missingIndices = $event->getMissingIndices(); |
|
| 51 | + $toReplaceIndices = $event->getIndicesToReplace(); |
|
| 52 | + |
|
| 53 | + if ($missingIndices !== [] || $toReplaceIndices !== []) { |
|
| 54 | + $schema = new SchemaWrapper($this->connection); |
|
| 55 | + |
|
| 56 | + foreach ($missingIndices as $missingIndex) { |
|
| 57 | + if ($schema->hasTable($missingIndex['tableName'])) { |
|
| 58 | + $table = $schema->getTable($missingIndex['tableName']); |
|
| 59 | + if (!$table->hasIndex($missingIndex['indexName'])) { |
|
| 60 | + $output->writeln('<info>Adding additional ' . $missingIndex['indexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); |
|
| 61 | + |
|
| 62 | + if ($missingIndex['dropUnnamedIndex']) { |
|
| 63 | + foreach ($table->getIndexes() as $index) { |
|
| 64 | + $columns = $index->getColumns(); |
|
| 65 | + if ($columns === $missingIndex['columns']) { |
|
| 66 | + $table->dropIndex($index->getName()); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + if ($missingIndex['uniqueIndex']) { |
|
| 72 | + $table->addUniqueIndex($missingIndex['columns'], $missingIndex['indexName'], $missingIndex['options']); |
|
| 73 | + } else { |
|
| 74 | + $table->addIndex($missingIndex['columns'], $missingIndex['indexName'], [], $missingIndex['options']); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + if (!$dryRun) { |
|
| 78 | + $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
| 79 | + } |
|
| 80 | + $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + foreach ($toReplaceIndices as $toReplaceIndex) { |
|
| 86 | + if ($schema->hasTable($toReplaceIndex['tableName'])) { |
|
| 87 | + $table = $schema->getTable($toReplaceIndex['tableName']); |
|
| 88 | + |
|
| 89 | + if ($table->hasIndex($toReplaceIndex['newIndexName'])) { |
|
| 90 | + continue; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $output->writeln('<info>Adding additional ' . $toReplaceIndex['newIndexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); |
|
| 94 | + |
|
| 95 | + if ($toReplaceIndex['uniqueIndex']) { |
|
| 96 | + $table->addUniqueIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], $toReplaceIndex['options']); |
|
| 97 | + } else { |
|
| 98 | + $table->addIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], [], $toReplaceIndex['options']); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if (!$dryRun) { |
|
| 102 | + $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + foreach ($toReplaceIndex['oldIndexNames'] as $oldIndexName) { |
|
| 106 | + if ($table->hasIndex($oldIndexName)) { |
|
| 107 | + $output->writeln('<info>Removing ' . $oldIndexName . ' index from the ' . $table->getName() . ' table</info>'); |
|
| 108 | + $table->dropIndex($oldIndexName); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if (!$dryRun) { |
|
| 113 | + $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
| 114 | + } |
|
| 115 | + $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + if ($dryRun) { |
|
| 120 | + $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun); |
|
| 121 | + if ($sqlQueries !== null) { |
|
| 122 | + $output->writeln($sqlQueries); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return 0; |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | if ($schema->hasTable($missingIndex['tableName'])) { |
| 58 | 58 | $table = $schema->getTable($missingIndex['tableName']); |
| 59 | 59 | if (!$table->hasIndex($missingIndex['indexName'])) { |
| 60 | - $output->writeln('<info>Adding additional ' . $missingIndex['indexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); |
|
| 60 | + $output->writeln('<info>Adding additional '.$missingIndex['indexName'].' index to the '.$table->getName().' table, this can take some time...</info>'); |
|
| 61 | 61 | |
| 62 | 62 | if ($missingIndex['dropUnnamedIndex']) { |
| 63 | 63 | foreach ($table->getIndexes() as $index) { |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | if (!$dryRun) { |
| 78 | 78 | $this->connection->migrateToSchema($schema->getWrappedSchema()); |
| 79 | 79 | } |
| 80 | - $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); |
|
| 80 | + $output->writeln('<info>'.$table->getName().' table updated successfully.</info>'); |
|
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | continue; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - $output->writeln('<info>Adding additional ' . $toReplaceIndex['newIndexName'] . ' index to the ' . $table->getName() . ' table, this can take some time...</info>'); |
|
| 93 | + $output->writeln('<info>Adding additional '.$toReplaceIndex['newIndexName'].' index to the '.$table->getName().' table, this can take some time...</info>'); |
|
| 94 | 94 | |
| 95 | 95 | if ($toReplaceIndex['uniqueIndex']) { |
| 96 | 96 | $table->addUniqueIndex($toReplaceIndex['columns'], $toReplaceIndex['newIndexName'], $toReplaceIndex['options']); |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | foreach ($toReplaceIndex['oldIndexNames'] as $oldIndexName) { |
| 106 | 106 | if ($table->hasIndex($oldIndexName)) { |
| 107 | - $output->writeln('<info>Removing ' . $oldIndexName . ' index from the ' . $table->getName() . ' table</info>'); |
|
| 107 | + $output->writeln('<info>Removing '.$oldIndexName.' index from the '.$table->getName().' table</info>'); |
|
| 108 | 108 | $table->dropIndex($oldIndexName); |
| 109 | 109 | } |
| 110 | 110 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | if (!$dryRun) { |
| 113 | 113 | $this->connection->migrateToSchema($schema->getWrappedSchema()); |
| 114 | 114 | } |
| 115 | - $output->writeln('<info>' . $table->getName() . ' table updated successfully.</info>'); |
|
| 115 | + $output->writeln('<info>'.$table->getName().' table updated successfully.</info>'); |
|
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |