You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddMissingIndices.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Citharel <nextcloud@tcit.fr>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Core\Command\Db;
  30. use OC\DB\SchemaWrapper;
  31. use OCP\IDBConnection;
  32. use Symfony\Component\Console\Command\Command;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  36. use Symfony\Component\EventDispatcher\GenericEvent;
  37. /**
  38. * Class AddMissingIndices
  39. *
  40. * if you added any new indices to the database, this is the right place to add
  41. * your update routine for existing instances
  42. *
  43. * @package OC\Core\Command\Db
  44. */
  45. class AddMissingIndices extends Command {
  46. /** @var IDBConnection */
  47. private $connection;
  48. /** @var EventDispatcherInterface */
  49. private $dispatcher;
  50. public function __construct(IDBConnection $connection, EventDispatcherInterface $dispatcher) {
  51. parent::__construct();
  52. $this->connection = $connection;
  53. $this->dispatcher = $dispatcher;
  54. }
  55. protected function configure() {
  56. $this
  57. ->setName('db:add-missing-indices')
  58. ->setDescription('Add missing indices to the database tables');
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output) {
  61. $this->addCoreIndexes($output);
  62. // Dispatch event so apps can also update indexes if needed
  63. $event = new GenericEvent($output);
  64. $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_INDEXES_EVENT, $event);
  65. }
  66. /**
  67. * add missing indices to the share table
  68. *
  69. * @param OutputInterface $output
  70. * @throws \Doctrine\DBAL\Schema\SchemaException
  71. */
  72. private function addCoreIndexes(OutputInterface $output) {
  73. $output->writeln('<info>Check indices of the share table.</info>');
  74. $schema = new SchemaWrapper($this->connection);
  75. $updated = false;
  76. if ($schema->hasTable('share')) {
  77. $table = $schema->getTable('share');
  78. if (!$table->hasIndex('share_with_index')) {
  79. $output->writeln('<info>Adding additional share_with index to the share table, this can take some time...</info>');
  80. $table->addIndex(['share_with'], 'share_with_index');
  81. $this->connection->migrateToSchema($schema->getWrappedSchema());
  82. $updated = true;
  83. $output->writeln('<info>Share table updated successfully.</info>');
  84. }
  85. if (!$table->hasIndex('parent_index')) {
  86. $output->writeln('<info>Adding additional parent index to the share table, this can take some time...</info>');
  87. $table->addIndex(['parent'], 'parent_index');
  88. $this->connection->migrateToSchema($schema->getWrappedSchema());
  89. $updated = true;
  90. $output->writeln('<info>Share table updated successfully.</info>');
  91. }
  92. if (!$table->hasIndex('owner_index')) {
  93. $output->writeln('<info>Adding additional owner index to the share table, this can take some time...</info>');
  94. $table->addIndex(['uid_owner'], 'owner_index');
  95. $this->connection->migrateToSchema($schema->getWrappedSchema());
  96. $updated = true;
  97. $output->writeln('<info>Share table updated successfully.</info>');
  98. }
  99. if (!$table->hasIndex('initiator_index')) {
  100. $output->writeln('<info>Adding additional initiator index to the share table, this can take some time...</info>');
  101. $table->addIndex(['uid_initiator'], 'initiator_index');
  102. $this->connection->migrateToSchema($schema->getWrappedSchema());
  103. $updated = true;
  104. $output->writeln('<info>Share table updated successfully.</info>');
  105. }
  106. }
  107. $output->writeln('<info>Check indices of the filecache table.</info>');
  108. if ($schema->hasTable('filecache')) {
  109. $table = $schema->getTable('filecache');
  110. if (!$table->hasIndex('fs_mtime')) {
  111. $output->writeln('<info>Adding additional mtime index to the filecache table, this can take some time...</info>');
  112. $table->addIndex(['mtime'], 'fs_mtime');
  113. $this->connection->migrateToSchema($schema->getWrappedSchema());
  114. $updated = true;
  115. $output->writeln('<info>Filecache table updated successfully.</info>');
  116. }
  117. }
  118. $output->writeln('<info>Check indices of the twofactor_providers table.</info>');
  119. if ($schema->hasTable('twofactor_providers')) {
  120. $table = $schema->getTable('twofactor_providers');
  121. if (!$table->hasIndex('twofactor_providers_uid')) {
  122. $output->writeln('<info>Adding additional twofactor_providers_uid index to the twofactor_providers table, this can take some time...</info>');
  123. $table->addIndex(['uid'], 'twofactor_providers_uid');
  124. $this->connection->migrateToSchema($schema->getWrappedSchema());
  125. $updated = true;
  126. $output->writeln('<info>Twofactor_providers table updated successfully.</info>');
  127. }
  128. }
  129. $output->writeln('<info>Check indices of the login_flow_v2 table.</info>');
  130. if ($schema->hasTable('login_flow_v2')) {
  131. $table = $schema->getTable('login_flow_v2');
  132. if (!$table->hasIndex('poll_token')) {
  133. $output->writeln('<info>Adding additional indeces to the login_flow_v2 table, this can take some time...</info>');
  134. foreach ($table->getIndexes() as $index) {
  135. $columns = $index->getColumns();
  136. if ($columns === ['poll_token'] ||
  137. $columns === ['login_token'] ||
  138. $columns === ['timestamp']) {
  139. $table->dropIndex($index->getName());
  140. }
  141. }
  142. $table->addUniqueIndex(['poll_token'], 'poll_token');
  143. $table->addUniqueIndex(['login_token'], 'login_token');
  144. $table->addIndex(['timestamp'], 'timestamp');
  145. $this->connection->migrateToSchema($schema->getWrappedSchema());
  146. $updated = true;
  147. $output->writeln('<info>login_flow_v2 table updated successfully.</info>');
  148. }
  149. }
  150. $output->writeln('<info>Check indices of the whats_new table.</info>');
  151. if ($schema->hasTable('whats_new')) {
  152. $table = $schema->getTable('whats_new');
  153. if (!$table->hasIndex('version')) {
  154. $output->writeln('<info>Adding version index to the whats_new table, this can take some time...</info>');
  155. foreach ($table->getIndexes() as $index) {
  156. if ($index->getColumns() === ['version']) {
  157. $table->dropIndex($index->getName());
  158. }
  159. }
  160. $table->addUniqueIndex(['version'], 'version');
  161. $this->connection->migrateToSchema($schema->getWrappedSchema());
  162. $updated = true;
  163. $output->writeln('<info>whats_new table updated successfully.</info>');
  164. }
  165. }
  166. $output->writeln('<info>Check indices of the cards table.</info>');
  167. if ($schema->hasTable('cards')) {
  168. $table = $schema->getTable('cards');
  169. if (!$table->hasIndex('cards_abid')) {
  170. $output->writeln('<info>Adding cards_abid index to the cards table, this can take some time...</info>');
  171. foreach ($table->getIndexes() as $index) {
  172. if ($index->getColumns() === ['addressbookid']) {
  173. $table->dropIndex($index->getName());
  174. }
  175. }
  176. $table->addIndex(['addressbookid'], 'cards_abid');
  177. $this->connection->migrateToSchema($schema->getWrappedSchema());
  178. $updated = true;
  179. $output->writeln('<info>cards table updated successfully.</info>');
  180. }
  181. }
  182. $output->writeln('<info>Check indices of the cards_properties table.</info>');
  183. if ($schema->hasTable('cards_properties')) {
  184. $table = $schema->getTable('cards_properties');
  185. if (!$table->hasIndex('cards_prop_abid')) {
  186. $output->writeln('<info>Adding cards_prop_abid index to the cards_properties table, this can take some time...</info>');
  187. foreach ($table->getIndexes() as $index) {
  188. if ($index->getColumns() === ['addressbookid']) {
  189. $table->dropIndex($index->getName());
  190. }
  191. }
  192. $table->addIndex(['addressbookid'], 'cards_prop_abid');
  193. $this->connection->migrateToSchema($schema->getWrappedSchema());
  194. $updated = true;
  195. $output->writeln('<info>cards_properties table updated successfully.</info>');
  196. }
  197. }
  198. $output->writeln('<info>Check indices of the calendarobjects_props table.</info>');
  199. if ($schema->hasTable('calendarobjects_props')) {
  200. $table = $schema->getTable('calendarobjects_props');
  201. if (!$table->hasIndex('calendarobject_calid_index')) {
  202. $output->writeln('<info>Adding calendarobject_calid_index index to the calendarobjects_props table, this can take some time...</info>');
  203. $table->addIndex(['calendarid', 'calendartype'], 'calendarobject_calid_index');
  204. $this->connection->migrateToSchema($schema->getWrappedSchema());
  205. $updated = true;
  206. $output->writeln('<info>calendarobjects_props table updated successfully.</info>');
  207. }
  208. }
  209. $output->writeln('<info>Check indices of the schedulingobjects table.</info>');
  210. if ($schema->hasTable('schedulingobjects')) {
  211. $table = $schema->getTable('schedulingobjects');
  212. if (!$table->hasIndex('schedulobj_principuri_index')) {
  213. $output->writeln('<info>Adding schedulobj_principuri_index index to the schedulingobjects table, this can take some time...</info>');
  214. $table->addIndex(['principaluri'], 'schedulobj_principuri_index');
  215. $this->connection->migrateToSchema($schema->getWrappedSchema());
  216. $updated = true;
  217. $output->writeln('<info>schedulingobjects table updated successfully.</info>');
  218. }
  219. }
  220. if (!$updated) {
  221. $output->writeln('<info>Done.</info>');
  222. }
  223. }
  224. }