AddMissingIndices.php 4.6 KB

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