AddMissingIndices.php 10 KB

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