AddMissingIndices.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Core\Command\Db;
  22. use OC\DB\SchemaWrapper;
  23. use OCP\IDBConnection;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Output\OutputInterface;
  27. /**
  28. * Class AddMissingIndices
  29. *
  30. * if you added any new indices to the database, this is the right place to add
  31. * it your update routine for existing instances
  32. *
  33. * @package OC\Core\Command\Db
  34. */
  35. class AddMissingIndices extends Command {
  36. /** @var IDBConnection */
  37. private $connection;
  38. /**
  39. * @param IDBConnection $connection
  40. */
  41. public function __construct(IDBConnection $connection) {
  42. $this->connection = $connection;
  43. parent::__construct();
  44. }
  45. protected function configure() {
  46. $this
  47. ->setName('db:add-missing-indices')
  48. ->setDescription('Add missing indices to the database tables');
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output) {
  51. $this->addShareTableIndicies($output);
  52. }
  53. /**
  54. * add missing indices to the share table
  55. *
  56. * @param OutputInterface $output
  57. * @throws \Doctrine\DBAL\Schema\SchemaException
  58. */
  59. private function addShareTableIndicies(OutputInterface $output) {
  60. $output->writeln('<info>Check indices of the share table.</info>');
  61. $schema = new SchemaWrapper($this->connection);
  62. $updated = false;
  63. if ($schema->hasTable("share")) {
  64. $table = $schema->getTable("share");
  65. if (!$table->hasIndex('share_with_index')) {
  66. $output->writeln('<info>Adding additional index to the share table, this can take some time...</info>');
  67. $table->addIndex(['share_with'], 'share_with_index');
  68. $this->connection->migrateToSchema($schema->getWrappedSchema());
  69. $updated = true;
  70. $output->writeln('<info>Share table updated successfully.</info>');
  71. }
  72. }
  73. if (!$updated) {
  74. $output->writeln('<info>Done.</info>');
  75. }
  76. }
  77. }