AddMissingIndices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Mario Danic <mario@lovelyhq.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Citharel <nextcloud@tcit.fr>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. namespace OC\Core\Command\Db;
  33. use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
  34. use OC\DB\Connection;
  35. use OC\DB\SchemaWrapper;
  36. use OCP\IDBConnection;
  37. use Symfony\Component\Console\Command\Command;
  38. use Symfony\Component\Console\Input\InputOption;
  39. use Symfony\Component\Console\Input\InputInterface;
  40. use Symfony\Component\Console\Output\OutputInterface;
  41. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  42. use Symfony\Component\EventDispatcher\GenericEvent;
  43. /**
  44. * Class AddMissingIndices
  45. *
  46. * if you added any new indices to the database, this is the right place to add
  47. * your update routine for existing instances
  48. *
  49. * @package OC\Core\Command\Db
  50. */
  51. class AddMissingIndices extends Command {
  52. private Connection $connection;
  53. private EventDispatcherInterface $dispatcher;
  54. public function __construct(Connection $connection, EventDispatcherInterface $dispatcher) {
  55. parent::__construct();
  56. $this->connection = $connection;
  57. $this->dispatcher = $dispatcher;
  58. }
  59. protected function configure() {
  60. $this
  61. ->setName('db:add-missing-indices')
  62. ->setDescription('Add missing indices to the database tables')
  63. ->addOption('dry-run', null, InputOption::VALUE_NONE, "Output the SQL queries instead of running them.");
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. $this->addCoreIndexes($output, $input->getOption('dry-run'));
  67. // Dispatch event so apps can also update indexes if needed
  68. $event = new GenericEvent($output);
  69. $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_INDEXES_EVENT, $event);
  70. return 0;
  71. }
  72. /**
  73. * add missing indices to the share table
  74. *
  75. * @param OutputInterface $output
  76. * @param bool $dryRun If true, will return the sql queries instead of running them.
  77. * @throws \Doctrine\DBAL\Schema\SchemaException
  78. */
  79. private function addCoreIndexes(OutputInterface $output, bool $dryRun): void {
  80. $output->writeln('<info>Check indices of the share table.</info>');
  81. $schema = new SchemaWrapper($this->connection);
  82. $updated = false;
  83. if ($schema->hasTable('share')) {
  84. $table = $schema->getTable('share');
  85. if (!$table->hasIndex('share_with_index')) {
  86. $output->writeln('<info>Adding additional share_with index to the share table, this can take some time...</info>');
  87. $table->addIndex(['share_with'], 'share_with_index');
  88. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  89. if ($dryRun && $sqlQueries !== null) {
  90. $output->writeln($sqlQueries);
  91. }
  92. $updated = true;
  93. $output->writeln('<info>Share table updated successfully.</info>');
  94. }
  95. if (!$table->hasIndex('parent_index')) {
  96. $output->writeln('<info>Adding additional parent index to the share table, this can take some time...</info>');
  97. $table->addIndex(['parent'], 'parent_index');
  98. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  99. if ($dryRun && $sqlQueries !== null) {
  100. $output->writeln($sqlQueries);
  101. }
  102. $updated = true;
  103. $output->writeln('<info>Share table updated successfully.</info>');
  104. }
  105. if (!$table->hasIndex('owner_index')) {
  106. $output->writeln('<info>Adding additional owner index to the share table, this can take some time...</info>');
  107. $table->addIndex(['uid_owner'], 'owner_index');
  108. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  109. if ($dryRun && $sqlQueries !== null) {
  110. $output->writeln($sqlQueries);
  111. }
  112. $updated = true;
  113. $output->writeln('<info>Share table updated successfully.</info>');
  114. }
  115. if (!$table->hasIndex('initiator_index')) {
  116. $output->writeln('<info>Adding additional initiator index to the share table, this can take some time...</info>');
  117. $table->addIndex(['uid_initiator'], 'initiator_index');
  118. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  119. if ($dryRun && $sqlQueries !== null) {
  120. $output->writeln($sqlQueries);
  121. }
  122. $updated = true;
  123. $output->writeln('<info>Share table updated successfully.</info>');
  124. }
  125. }
  126. $output->writeln('<info>Check indices of the filecache table.</info>');
  127. if ($schema->hasTable('filecache')) {
  128. $table = $schema->getTable('filecache');
  129. if (!$table->hasIndex('fs_mtime')) {
  130. $output->writeln('<info>Adding additional mtime index to the filecache table, this can take some time...</info>');
  131. $table->addIndex(['mtime'], 'fs_mtime');
  132. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  133. if ($dryRun && $sqlQueries !== null) {
  134. $output->writeln($sqlQueries);
  135. }
  136. $updated = true;
  137. $output->writeln('<info>Filecache table updated successfully.</info>');
  138. }
  139. if (!$table->hasIndex('fs_size')) {
  140. $output->writeln('<info>Adding additional size index to the filecache table, this can take some time...</info>');
  141. $table->addIndex(['size'], 'fs_size');
  142. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  143. if ($dryRun && $sqlQueries !== null) {
  144. $output->writeln($sqlQueries);
  145. }
  146. $updated = true;
  147. $output->writeln('<info>Filecache table updated successfully.</info>');
  148. }
  149. if (!$table->hasIndex('fs_id_storage_size')) {
  150. $output->writeln('<info>Adding additional size index to the filecache table, this can take some time...</info>');
  151. $table->addIndex(['fileid', 'storage', 'size'], 'fs_id_storage_size');
  152. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  153. if ($dryRun && $sqlQueries !== null) {
  154. $output->writeln($sqlQueries);
  155. }
  156. $updated = true;
  157. $output->writeln('<info>Filecache table updated successfully.</info>');
  158. }
  159. if (!$table->hasIndex('fs_storage_path_prefix') && !$schema->getDatabasePlatform() instanceof PostgreSQL94Platform) {
  160. $output->writeln('<info>Adding additional path index to the filecache table, this can take some time...</info>');
  161. $table->addIndex(['storage', 'path'], 'fs_storage_path_prefix', [], ['lengths' => [null, 64]]);
  162. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  163. if ($dryRun && $sqlQueries !== null) {
  164. $output->writeln($sqlQueries);
  165. }
  166. $updated = true;
  167. $output->writeln('<info>Filecache table updated successfully.</info>');
  168. }
  169. }
  170. $output->writeln('<info>Check indices of the twofactor_providers table.</info>');
  171. if ($schema->hasTable('twofactor_providers')) {
  172. $table = $schema->getTable('twofactor_providers');
  173. if (!$table->hasIndex('twofactor_providers_uid')) {
  174. $output->writeln('<info>Adding additional twofactor_providers_uid index to the twofactor_providers table, this can take some time...</info>');
  175. $table->addIndex(['uid'], 'twofactor_providers_uid');
  176. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  177. if ($dryRun && $sqlQueries !== null) {
  178. $output->writeln($sqlQueries);
  179. }
  180. $updated = true;
  181. $output->writeln('<info>Twofactor_providers table updated successfully.</info>');
  182. }
  183. }
  184. $output->writeln('<info>Check indices of the login_flow_v2 table.</info>');
  185. if ($schema->hasTable('login_flow_v2')) {
  186. $table = $schema->getTable('login_flow_v2');
  187. if (!$table->hasIndex('poll_token')) {
  188. $output->writeln('<info>Adding additional indeces to the login_flow_v2 table, this can take some time...</info>');
  189. foreach ($table->getIndexes() as $index) {
  190. $columns = $index->getColumns();
  191. if ($columns === ['poll_token'] ||
  192. $columns === ['login_token'] ||
  193. $columns === ['timestamp']) {
  194. $table->dropIndex($index->getName());
  195. }
  196. }
  197. $table->addUniqueIndex(['poll_token'], 'poll_token');
  198. $table->addUniqueIndex(['login_token'], 'login_token');
  199. $table->addIndex(['timestamp'], 'timestamp');
  200. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  201. if ($dryRun && $sqlQueries !== null) {
  202. $output->writeln($sqlQueries);
  203. }
  204. $updated = true;
  205. $output->writeln('<info>login_flow_v2 table updated successfully.</info>');
  206. }
  207. }
  208. $output->writeln('<info>Check indices of the whats_new table.</info>');
  209. if ($schema->hasTable('whats_new')) {
  210. $table = $schema->getTable('whats_new');
  211. if (!$table->hasIndex('version')) {
  212. $output->writeln('<info>Adding version index to the whats_new table, this can take some time...</info>');
  213. foreach ($table->getIndexes() as $index) {
  214. if ($index->getColumns() === ['version']) {
  215. $table->dropIndex($index->getName());
  216. }
  217. }
  218. $table->addUniqueIndex(['version'], 'version');
  219. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  220. if ($dryRun && $sqlQueries !== null) {
  221. $output->writeln($sqlQueries);
  222. }
  223. $updated = true;
  224. $output->writeln('<info>whats_new table updated successfully.</info>');
  225. }
  226. }
  227. $output->writeln('<info>Check indices of the cards table.</info>');
  228. $cardsUpdated = false;
  229. if ($schema->hasTable('cards')) {
  230. $table = $schema->getTable('cards');
  231. if ($table->hasIndex('addressbookid_uri_index')) {
  232. if ($table->hasIndex('cards_abiduri')) {
  233. $table->dropIndex('addressbookid_uri_index');
  234. } else {
  235. $output->writeln('<info>Renaming addressbookid_uri_index index to cards_abiduri in the cards table, this can take some time...</info>');
  236. foreach ($table->getIndexes() as $index) {
  237. if ($index->getColumns() === ['addressbookid', 'uri']) {
  238. $table->renameIndex('addressbookid_uri_index', 'cards_abiduri');
  239. }
  240. }
  241. }
  242. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  243. if ($dryRun && $sqlQueries !== null) {
  244. $output->writeln($sqlQueries);
  245. }
  246. $cardsUpdated = true;
  247. }
  248. if (!$table->hasIndex('cards_abid')) {
  249. $output->writeln('<info>Adding cards_abid index to the cards table, this can take some time...</info>');
  250. foreach ($table->getIndexes() as $index) {
  251. if ($index->getColumns() === ['addressbookid']) {
  252. $table->dropIndex($index->getName());
  253. }
  254. }
  255. $table->addIndex(['addressbookid'], 'cards_abid');
  256. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  257. if ($dryRun && $sqlQueries !== null) {
  258. $output->writeln($sqlQueries);
  259. }
  260. $cardsUpdated = true;
  261. }
  262. if (!$table->hasIndex('cards_abiduri')) {
  263. $output->writeln('<info>Adding cards_abiduri index to the cards table, this can take some time...</info>');
  264. foreach ($table->getIndexes() as $index) {
  265. if ($index->getColumns() === ['addressbookid', 'uri']) {
  266. $table->dropIndex($index->getName());
  267. }
  268. }
  269. $table->addIndex(['addressbookid', 'uri'], 'cards_abiduri');
  270. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  271. if ($dryRun && $sqlQueries !== null) {
  272. $output->writeln($sqlQueries);
  273. }
  274. $cardsUpdated = true;
  275. }
  276. if ($cardsUpdated) {
  277. $updated = true;
  278. $output->writeln('<info>cards table updated successfully.</info>');
  279. }
  280. }
  281. $output->writeln('<info>Check indices of the cards_properties table.</info>');
  282. if ($schema->hasTable('cards_properties')) {
  283. $table = $schema->getTable('cards_properties');
  284. if (!$table->hasIndex('cards_prop_abid')) {
  285. $output->writeln('<info>Adding cards_prop_abid index to the cards_properties table, this can take some time...</info>');
  286. foreach ($table->getIndexes() as $index) {
  287. if ($index->getColumns() === ['addressbookid']) {
  288. $table->dropIndex($index->getName());
  289. }
  290. }
  291. $table->addIndex(['addressbookid'], 'cards_prop_abid');
  292. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  293. if ($dryRun && $sqlQueries !== null) {
  294. $output->writeln($sqlQueries);
  295. }
  296. $updated = true;
  297. $output->writeln('<info>cards_properties table updated successfully.</info>');
  298. }
  299. }
  300. $output->writeln('<info>Check indices of the calendarobjects_props table.</info>');
  301. if ($schema->hasTable('calendarobjects_props')) {
  302. $table = $schema->getTable('calendarobjects_props');
  303. if (!$table->hasIndex('calendarobject_calid_index')) {
  304. $output->writeln('<info>Adding calendarobject_calid_index index to the calendarobjects_props table, this can take some time...</info>');
  305. $table->addIndex(['calendarid', 'calendartype'], 'calendarobject_calid_index');
  306. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  307. if ($dryRun && $sqlQueries !== null) {
  308. $output->writeln($sqlQueries);
  309. }
  310. $updated = true;
  311. $output->writeln('<info>calendarobjects_props table updated successfully.</info>');
  312. }
  313. }
  314. $output->writeln('<info>Check indices of the schedulingobjects table.</info>');
  315. if ($schema->hasTable('schedulingobjects')) {
  316. $table = $schema->getTable('schedulingobjects');
  317. if (!$table->hasIndex('schedulobj_principuri_index')) {
  318. $output->writeln('<info>Adding schedulobj_principuri_index index to the schedulingobjects table, this can take some time...</info>');
  319. $table->addIndex(['principaluri'], 'schedulobj_principuri_index');
  320. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  321. if ($dryRun && $sqlQueries !== null) {
  322. $output->writeln($sqlQueries);
  323. }
  324. $updated = true;
  325. $output->writeln('<info>schedulingobjects table updated successfully.</info>');
  326. }
  327. }
  328. $output->writeln('<info>Check indices of the oc_properties table.</info>');
  329. if ($schema->hasTable('properties')) {
  330. $table = $schema->getTable('properties');
  331. $propertiesUpdated = false;
  332. if (!$table->hasIndex('properties_path_index')) {
  333. $output->writeln('<info>Adding properties_path_index index to the oc_properties table, this can take some time...</info>');
  334. $table->addIndex(['userid', 'propertypath'], 'properties_path_index');
  335. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  336. if ($dryRun && $sqlQueries !== null) {
  337. $output->writeln($sqlQueries);
  338. }
  339. $propertiesUpdated = true;
  340. }
  341. if (!$table->hasIndex('properties_pathonly_index')) {
  342. $output->writeln('<info>Adding properties_pathonly_index index to the oc_properties table, this can take some time...</info>');
  343. $table->addIndex(['propertypath'], 'properties_pathonly_index');
  344. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  345. if ($dryRun && $sqlQueries !== null) {
  346. $output->writeln($sqlQueries);
  347. }
  348. $propertiesUpdated = true;
  349. }
  350. if ($propertiesUpdated) {
  351. $updated = true;
  352. $output->writeln('<info>oc_properties table updated successfully.</info>');
  353. }
  354. }
  355. $output->writeln('<info>Check indices of the oc_jobs table.</info>');
  356. if ($schema->hasTable('jobs')) {
  357. $table = $schema->getTable('jobs');
  358. if (!$table->hasIndex('job_lastcheck_reserved')) {
  359. $output->writeln('<info>Adding job_lastcheck_reserved index to the oc_jobs table, this can take some time...</info>');
  360. $table->addIndex(['last_checked', 'reserved_at'], 'job_lastcheck_reserved');
  361. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  362. if ($dryRun && $sqlQueries !== null) {
  363. $output->writeln($sqlQueries);
  364. }
  365. $updated = true;
  366. $output->writeln('<info>oc_properties table updated successfully.</info>');
  367. }
  368. }
  369. $output->writeln('<info>Check indices of the oc_direct_edit table.</info>');
  370. if ($schema->hasTable('direct_edit')) {
  371. $table = $schema->getTable('direct_edit');
  372. if (!$table->hasIndex('direct_edit_timestamp')) {
  373. $output->writeln('<info>Adding direct_edit_timestamp index to the oc_direct_edit table, this can take some time...</info>');
  374. $table->addIndex(['timestamp'], 'direct_edit_timestamp');
  375. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  376. if ($dryRun && $sqlQueries !== null) {
  377. $output->writeln($sqlQueries);
  378. }
  379. $updated = true;
  380. $output->writeln('<info>oc_direct_edit table updated successfully.</info>');
  381. }
  382. }
  383. $output->writeln('<info>Check indices of the oc_preferences table.</info>');
  384. if ($schema->hasTable('preferences')) {
  385. $table = $schema->getTable('preferences');
  386. if (!$table->hasIndex('preferences_app_key')) {
  387. $output->writeln('<info>Adding preferences_app_key index to the oc_preferences table, this can take some time...</info>');
  388. $table->addIndex(['appid', 'configkey'], 'preferences_app_key');
  389. $this->connection->migrateToSchema($schema->getWrappedSchema());
  390. $updated = true;
  391. $output->writeln('<info>oc_properties table updated successfully.</info>');
  392. }
  393. }
  394. $output->writeln('<info>Check indices of the oc_mounts table.</info>');
  395. if ($schema->hasTable('mounts')) {
  396. $table = $schema->getTable('mounts');
  397. if (!$table->hasIndex('mounts_class_index')) {
  398. $output->writeln('<info>Adding mounts_class_index index to the oc_mounts table, this can take some time...</info>');
  399. $table->addIndex(['mount_provider_class'], 'mounts_class_index');
  400. $this->connection->migrateToSchema($schema->getWrappedSchema());
  401. $updated = true;
  402. $output->writeln('<info>oc_mounts table updated successfully.</info>');
  403. }
  404. }
  405. if (!$updated) {
  406. $output->writeln('<info>Done.</info>');
  407. }
  408. }
  409. }