AddMissingIndices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /** @var Connection */
  53. private $connection;
  54. /** @var EventDispatcherInterface */
  55. private $dispatcher;
  56. public function __construct(Connection $connection, EventDispatcherInterface $dispatcher) {
  57. parent::__construct();
  58. $this->connection = $connection;
  59. $this->dispatcher = $dispatcher;
  60. }
  61. protected function configure() {
  62. $this
  63. ->setName('db:add-missing-indices')
  64. ->setDescription('Add missing indices to the database tables')
  65. ->addOption('dry-run', null, InputOption::VALUE_NONE, "Output the SQL queries instead of running them.");
  66. }
  67. protected function execute(InputInterface $input, OutputInterface $output): int {
  68. $this->addCoreIndexes($output, $input->getOption('dry-run'));
  69. // Dispatch event so apps can also update indexes if needed
  70. $event = new GenericEvent($output);
  71. $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_INDEXES_EVENT, $event);
  72. return 0;
  73. }
  74. /**
  75. * add missing indices to the share table
  76. *
  77. * @param OutputInterface $output
  78. * @param bool $dryRun If true, will return the sql queries instead of running them.
  79. * @throws \Doctrine\DBAL\Schema\SchemaException
  80. */
  81. private function addCoreIndexes(OutputInterface $output, bool $dryRun): void {
  82. $output->writeln('<info>Check indices of the share table.</info>');
  83. $schema = new SchemaWrapper($this->connection);
  84. $updated = false;
  85. if ($schema->hasTable('share')) {
  86. $table = $schema->getTable('share');
  87. if (!$table->hasIndex('share_with_index')) {
  88. $output->writeln('<info>Adding additional share_with index to the share table, this can take some time...</info>');
  89. $table->addIndex(['share_with'], 'share_with_index');
  90. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  91. if ($dryRun && $sqlQueries !== null) {
  92. $output->writeln($sqlQueries);
  93. }
  94. $updated = true;
  95. $output->writeln('<info>Share table updated successfully.</info>');
  96. }
  97. if (!$table->hasIndex('parent_index')) {
  98. $output->writeln('<info>Adding additional parent index to the share table, this can take some time...</info>');
  99. $table->addIndex(['parent'], 'parent_index');
  100. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  101. if ($dryRun && $sqlQueries !== null) {
  102. $output->writeln($sqlQueries);
  103. }
  104. $updated = true;
  105. $output->writeln('<info>Share table updated successfully.</info>');
  106. }
  107. if (!$table->hasIndex('owner_index')) {
  108. $output->writeln('<info>Adding additional owner index to the share table, this can take some time...</info>');
  109. $table->addIndex(['uid_owner'], 'owner_index');
  110. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  111. if ($dryRun && $sqlQueries !== null) {
  112. $output->writeln($sqlQueries);
  113. }
  114. $updated = true;
  115. $output->writeln('<info>Share table updated successfully.</info>');
  116. }
  117. if (!$table->hasIndex('initiator_index')) {
  118. $output->writeln('<info>Adding additional initiator index to the share table, this can take some time...</info>');
  119. $table->addIndex(['uid_initiator'], 'initiator_index');
  120. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  121. if ($dryRun && $sqlQueries !== null) {
  122. $output->writeln($sqlQueries);
  123. }
  124. $updated = true;
  125. $output->writeln('<info>Share table updated successfully.</info>');
  126. }
  127. }
  128. $output->writeln('<info>Check indices of the filecache table.</info>');
  129. if ($schema->hasTable('filecache')) {
  130. $table = $schema->getTable('filecache');
  131. if (!$table->hasIndex('fs_mtime')) {
  132. $output->writeln('<info>Adding additional mtime index to the filecache table, this can take some time...</info>');
  133. $table->addIndex(['mtime'], 'fs_mtime');
  134. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  135. if ($dryRun && $sqlQueries !== null) {
  136. $output->writeln($sqlQueries);
  137. }
  138. $updated = true;
  139. $output->writeln('<info>Filecache table updated successfully.</info>');
  140. }
  141. if (!$table->hasIndex('fs_size')) {
  142. $output->writeln('<info>Adding additional size index to the filecache table, this can take some time...</info>');
  143. $table->addIndex(['size'], 'fs_size');
  144. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  145. if ($dryRun && $sqlQueries !== null) {
  146. $output->writeln($sqlQueries);
  147. }
  148. $updated = true;
  149. $output->writeln('<info>Filecache table updated successfully.</info>');
  150. }
  151. if (!$table->hasIndex('fs_id_storage_size')) {
  152. $output->writeln('<info>Adding additional size index to the filecache table, this can take some time...</info>');
  153. $table->addIndex(['fileid', 'storage', 'size'], 'fs_id_storage_size');
  154. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  155. if ($dryRun && $sqlQueries !== null) {
  156. $output->writeln($sqlQueries);
  157. }
  158. $updated = true;
  159. $output->writeln('<info>Filecache table updated successfully.</info>');
  160. }
  161. if (!$table->hasIndex('fs_storage_path_prefix') && !$schema->getDatabasePlatform() instanceof PostgreSQL94Platform) {
  162. $output->writeln('<info>Adding additional path index to the filecache table, this can take some time...</info>');
  163. $table->addIndex(['storage', 'path'], 'fs_storage_path_prefix', [], ['lengths' => [null, 64]]);
  164. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  165. if ($dryRun && $sqlQueries !== null) {
  166. $output->writeln($sqlQueries);
  167. }
  168. $updated = true;
  169. $output->writeln('<info>Filecache table updated successfully.</info>');
  170. }
  171. }
  172. $output->writeln('<info>Check indices of the twofactor_providers table.</info>');
  173. if ($schema->hasTable('twofactor_providers')) {
  174. $table = $schema->getTable('twofactor_providers');
  175. if (!$table->hasIndex('twofactor_providers_uid')) {
  176. $output->writeln('<info>Adding additional twofactor_providers_uid index to the twofactor_providers table, this can take some time...</info>');
  177. $table->addIndex(['uid'], 'twofactor_providers_uid');
  178. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  179. if ($dryRun && $sqlQueries !== null) {
  180. $output->writeln($sqlQueries);
  181. }
  182. $updated = true;
  183. $output->writeln('<info>Twofactor_providers table updated successfully.</info>');
  184. }
  185. }
  186. $output->writeln('<info>Check indices of the login_flow_v2 table.</info>');
  187. if ($schema->hasTable('login_flow_v2')) {
  188. $table = $schema->getTable('login_flow_v2');
  189. if (!$table->hasIndex('poll_token')) {
  190. $output->writeln('<info>Adding additional indeces to the login_flow_v2 table, this can take some time...</info>');
  191. foreach ($table->getIndexes() as $index) {
  192. $columns = $index->getColumns();
  193. if ($columns === ['poll_token'] ||
  194. $columns === ['login_token'] ||
  195. $columns === ['timestamp']) {
  196. $table->dropIndex($index->getName());
  197. }
  198. }
  199. $table->addUniqueIndex(['poll_token'], 'poll_token');
  200. $table->addUniqueIndex(['login_token'], 'login_token');
  201. $table->addIndex(['timestamp'], 'timestamp');
  202. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  203. if ($dryRun && $sqlQueries !== null) {
  204. $output->writeln($sqlQueries);
  205. }
  206. $updated = true;
  207. $output->writeln('<info>login_flow_v2 table updated successfully.</info>');
  208. }
  209. }
  210. $output->writeln('<info>Check indices of the whats_new table.</info>');
  211. if ($schema->hasTable('whats_new')) {
  212. $table = $schema->getTable('whats_new');
  213. if (!$table->hasIndex('version')) {
  214. $output->writeln('<info>Adding version index to the whats_new table, this can take some time...</info>');
  215. foreach ($table->getIndexes() as $index) {
  216. if ($index->getColumns() === ['version']) {
  217. $table->dropIndex($index->getName());
  218. }
  219. }
  220. $table->addUniqueIndex(['version'], 'version');
  221. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  222. if ($dryRun && $sqlQueries !== null) {
  223. $output->writeln($sqlQueries);
  224. }
  225. $updated = true;
  226. $output->writeln('<info>whats_new table updated successfully.</info>');
  227. }
  228. }
  229. $output->writeln('<info>Check indices of the cards table.</info>');
  230. $cardsUpdated = false;
  231. if ($schema->hasTable('cards')) {
  232. $table = $schema->getTable('cards');
  233. if ($table->hasIndex('addressbookid_uri_index')) {
  234. if ($table->hasIndex('cards_abiduri')) {
  235. $table->dropIndex('addressbookid_uri_index');
  236. } else {
  237. $output->writeln('<info>Renaming addressbookid_uri_index index to cards_abiduri in the cards table, this can take some time...</info>');
  238. foreach ($table->getIndexes() as $index) {
  239. if ($index->getColumns() === ['addressbookid', 'uri']) {
  240. $table->renameIndex('addressbookid_uri_index', 'cards_abiduri');
  241. }
  242. }
  243. }
  244. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  245. if ($dryRun && $sqlQueries !== null) {
  246. $output->writeln($sqlQueries);
  247. }
  248. $cardsUpdated = true;
  249. }
  250. if (!$table->hasIndex('cards_abid')) {
  251. $output->writeln('<info>Adding cards_abid index to the cards table, this can take some time...</info>');
  252. foreach ($table->getIndexes() as $index) {
  253. if ($index->getColumns() === ['addressbookid']) {
  254. $table->dropIndex($index->getName());
  255. }
  256. }
  257. $table->addIndex(['addressbookid'], 'cards_abid');
  258. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  259. if ($dryRun && $sqlQueries !== null) {
  260. $output->writeln($sqlQueries);
  261. }
  262. $cardsUpdated = true;
  263. }
  264. if (!$table->hasIndex('cards_abiduri')) {
  265. $output->writeln('<info>Adding cards_abiduri index to the cards table, this can take some time...</info>');
  266. foreach ($table->getIndexes() as $index) {
  267. if ($index->getColumns() === ['addressbookid', 'uri']) {
  268. $table->dropIndex($index->getName());
  269. }
  270. }
  271. $table->addIndex(['addressbookid', 'uri'], 'cards_abiduri');
  272. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  273. if ($dryRun && $sqlQueries !== null) {
  274. $output->writeln($sqlQueries);
  275. }
  276. $cardsUpdated = true;
  277. }
  278. if ($cardsUpdated) {
  279. $updated = true;
  280. $output->writeln('<info>cards table updated successfully.</info>');
  281. }
  282. }
  283. $output->writeln('<info>Check indices of the cards_properties table.</info>');
  284. if ($schema->hasTable('cards_properties')) {
  285. $table = $schema->getTable('cards_properties');
  286. if (!$table->hasIndex('cards_prop_abid')) {
  287. $output->writeln('<info>Adding cards_prop_abid index to the cards_properties table, this can take some time...</info>');
  288. foreach ($table->getIndexes() as $index) {
  289. if ($index->getColumns() === ['addressbookid']) {
  290. $table->dropIndex($index->getName());
  291. }
  292. }
  293. $table->addIndex(['addressbookid'], 'cards_prop_abid');
  294. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  295. if ($dryRun && $sqlQueries !== null) {
  296. $output->writeln($sqlQueries);
  297. }
  298. $updated = true;
  299. $output->writeln('<info>cards_properties table updated successfully.</info>');
  300. }
  301. }
  302. $output->writeln('<info>Check indices of the calendarobjects_props table.</info>');
  303. if ($schema->hasTable('calendarobjects_props')) {
  304. $table = $schema->getTable('calendarobjects_props');
  305. if (!$table->hasIndex('calendarobject_calid_index')) {
  306. $output->writeln('<info>Adding calendarobject_calid_index index to the calendarobjects_props table, this can take some time...</info>');
  307. $table->addIndex(['calendarid', 'calendartype'], 'calendarobject_calid_index');
  308. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  309. if ($dryRun && $sqlQueries !== null) {
  310. $output->writeln($sqlQueries);
  311. }
  312. $updated = true;
  313. $output->writeln('<info>calendarobjects_props table updated successfully.</info>');
  314. }
  315. }
  316. $output->writeln('<info>Check indices of the schedulingobjects table.</info>');
  317. if ($schema->hasTable('schedulingobjects')) {
  318. $table = $schema->getTable('schedulingobjects');
  319. if (!$table->hasIndex('schedulobj_principuri_index')) {
  320. $output->writeln('<info>Adding schedulobj_principuri_index index to the schedulingobjects table, this can take some time...</info>');
  321. $table->addIndex(['principaluri'], 'schedulobj_principuri_index');
  322. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  323. if ($dryRun && $sqlQueries !== null) {
  324. $output->writeln($sqlQueries);
  325. }
  326. $updated = true;
  327. $output->writeln('<info>schedulingobjects table updated successfully.</info>');
  328. }
  329. }
  330. $output->writeln('<info>Check indices of the oc_properties table.</info>');
  331. if ($schema->hasTable('properties')) {
  332. $table = $schema->getTable('properties');
  333. $propertiesUpdated = false;
  334. if (!$table->hasIndex('properties_path_index')) {
  335. $output->writeln('<info>Adding properties_path_index index to the oc_properties table, this can take some time...</info>');
  336. $table->addIndex(['userid', 'propertypath'], 'properties_path_index');
  337. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  338. if ($dryRun && $sqlQueries !== null) {
  339. $output->writeln($sqlQueries);
  340. }
  341. $propertiesUpdated = true;
  342. }
  343. if (!$table->hasIndex('properties_pathonly_index')) {
  344. $output->writeln('<info>Adding properties_pathonly_index index to the oc_properties table, this can take some time...</info>');
  345. $table->addIndex(['propertypath'], 'properties_pathonly_index');
  346. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  347. if ($dryRun && $sqlQueries !== null) {
  348. $output->writeln($sqlQueries);
  349. }
  350. $propertiesUpdated = true;
  351. }
  352. if ($propertiesUpdated) {
  353. $updated = true;
  354. $output->writeln('<info>oc_properties table updated successfully.</info>');
  355. }
  356. }
  357. $output->writeln('<info>Check indices of the oc_jobs table.</info>');
  358. if ($schema->hasTable('jobs')) {
  359. $table = $schema->getTable('jobs');
  360. if (!$table->hasIndex('job_lastcheck_reserved')) {
  361. $output->writeln('<info>Adding job_lastcheck_reserved index to the oc_jobs table, this can take some time...</info>');
  362. $table->addIndex(['last_checked', 'reserved_at'], 'job_lastcheck_reserved');
  363. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  364. if ($dryRun && $sqlQueries !== null) {
  365. $output->writeln($sqlQueries);
  366. }
  367. $updated = true;
  368. $output->writeln('<info>oc_properties table updated successfully.</info>');
  369. }
  370. }
  371. $output->writeln('<info>Check indices of the oc_direct_edit table.</info>');
  372. if ($schema->hasTable('direct_edit')) {
  373. $table = $schema->getTable('direct_edit');
  374. if (!$table->hasIndex('direct_edit_timestamp')) {
  375. $output->writeln('<info>Adding direct_edit_timestamp index to the oc_direct_edit table, this can take some time...</info>');
  376. $table->addIndex(['timestamp'], 'direct_edit_timestamp');
  377. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  378. if ($dryRun && $sqlQueries !== null) {
  379. $output->writeln($sqlQueries);
  380. }
  381. $updated = true;
  382. $output->writeln('<info>oc_direct_edit table updated successfully.</info>');
  383. }
  384. }
  385. if (!$updated) {
  386. $output->writeln('<info>Done.</info>');
  387. }
  388. }
  389. }