AddMissingIndices.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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->addCoreIndexes($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 addCoreIndexes(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. $output->writeln('<info>Check indices of the filecache table.</info>');
  101. if ($schema->hasTable('filecache')) {
  102. $table = $schema->getTable('filecache');
  103. if (!$table->hasIndex('fs_mtime')) {
  104. $output->writeln('<info>Adding additional mtime index to the filecache table, this can take some time...</info>');
  105. $table->addIndex(['mtime'], 'fs_mtime');
  106. $this->connection->migrateToSchema($schema->getWrappedSchema());
  107. $updated = true;
  108. $output->writeln('<info>Filecache table updated successfully.</info>');
  109. }
  110. }
  111. $output->writeln('<info>Check indices of the twofactor_providers table.</info>');
  112. if ($schema->hasTable('twofactor_providers')) {
  113. $table = $schema->getTable('twofactor_providers');
  114. if (!$table->hasIndex('twofactor_providers_uid')) {
  115. $output->writeln('<info>Adding additional twofactor_providers_uid index to the twofactor_providers table, this can take some time...</info>');
  116. $table->addIndex(['uid'], 'twofactor_providers_uid');
  117. $this->connection->migrateToSchema($schema->getWrappedSchema());
  118. $updated = true;
  119. $output->writeln('<info>Twofactor_providers table updated successfully.</info>');
  120. }
  121. }
  122. $output->writeln('<info>Check indices of the login_flow_v2 table.</info>');
  123. if ($schema->hasTable('login_flow_v2')) {
  124. $table = $schema->getTable('login_flow_v2');
  125. if (!$table->hasIndex('poll_token')) {
  126. $output->writeln('<info>Adding additional indeces to the login_flow_v2 table, this can take some time...</info>');
  127. foreach ($table->getIndexes() as $index) {
  128. $columns = $index->getColumns();
  129. if ($columns === ['poll_token'] ||
  130. $columns === ['login_token'] ||
  131. $columns === ['timestamp']) {
  132. $table->dropIndex($index->getName());
  133. }
  134. }
  135. $table->addUniqueIndex(['poll_token'], 'poll_token');
  136. $table->addUniqueIndex(['login_token'], 'login_token');
  137. $table->addIndex(['timestamp'], 'timestamp');
  138. $this->connection->migrateToSchema($schema->getWrappedSchema());
  139. $updated = true;
  140. $output->writeln('<info>login_flow_v2 table updated successfully.</info>');
  141. }
  142. }
  143. $output->writeln('<info>Check indices of the whats_new table.</info>');
  144. if ($schema->hasTable('whats_new')) {
  145. $table = $schema->getTable('whats_new');
  146. if (!$table->hasIndex('version')) {
  147. $output->writeln('<info>Adding version index to the whats_new table, this can take some time...</info>');
  148. foreach ($table->getIndexes() as $index) {
  149. if ($index->getColumns() === ['version']) {
  150. $table->dropIndex($index->getName());
  151. }
  152. }
  153. $table->addUniqueIndex(['version'], 'version');
  154. $this->connection->migrateToSchema($schema->getWrappedSchema());
  155. $updated = true;
  156. $output->writeln('<info>whats_new table updated successfully.</info>');
  157. }
  158. }
  159. $output->writeln('<info>Check indices of the cards table.</info>');
  160. if ($schema->hasTable('cards')) {
  161. $table = $schema->getTable('cards');
  162. if (!$table->hasIndex('cards_abid')) {
  163. $output->writeln('<info>Adding cards_abid index to the cards table, this can take some time...</info>');
  164. foreach ($table->getIndexes() as $index) {
  165. if ($index->getColumns() === ['addressbookid']) {
  166. $table->dropIndex($index->getName());
  167. }
  168. }
  169. $table->addIndex(['addressbookid'], 'cards_abid');
  170. $this->connection->migrateToSchema($schema->getWrappedSchema());
  171. $updated = true;
  172. $output->writeln('<info>cards table updated successfully.</info>');
  173. }
  174. }
  175. $output->writeln('<info>Check indices of the cards_properties table.</info>');
  176. if ($schema->hasTable('cards_properties')) {
  177. $table = $schema->getTable('cards_properties');
  178. if (!$table->hasIndex('cards_prop_abid')) {
  179. $output->writeln('<info>Adding cards_prop_abid index to the cards_properties table, this can take some time...</info>');
  180. foreach ($table->getIndexes() as $index) {
  181. if ($index->getColumns() === ['addressbookid']) {
  182. $table->dropIndex($index->getName());
  183. }
  184. }
  185. $table->addIndex(['addressbookid'], 'cards_prop_abid');
  186. $this->connection->migrateToSchema($schema->getWrappedSchema());
  187. $updated = true;
  188. $output->writeln('<info>cards_properties table updated successfully.</info>');
  189. }
  190. }
  191. if (!$updated) {
  192. $output->writeln('<info>Done.</info>');
  193. }
  194. }
  195. }