DatabaseHasMissingIndices.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OC\DB\Connection;
  9. use OC\DB\MissingIndexInformation;
  10. use OC\DB\SchemaWrapper;
  11. use OCP\DB\Events\AddMissingIndicesEvent;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\IL10N;
  14. use OCP\IURLGenerator;
  15. use OCP\SetupCheck\ISetupCheck;
  16. use OCP\SetupCheck\SetupResult;
  17. class DatabaseHasMissingIndices implements ISetupCheck {
  18. public function __construct(
  19. private IL10N $l10n,
  20. private Connection $connection,
  21. private IEventDispatcher $dispatcher,
  22. private IURLGenerator $urlGenerator,
  23. ) {
  24. }
  25. public function getCategory(): string {
  26. return 'database';
  27. }
  28. public function getName(): string {
  29. return $this->l10n->t('Database missing indices');
  30. }
  31. private function getMissingIndices(): array {
  32. $indexInfo = new MissingIndexInformation();
  33. // Dispatch event so apps can also hint for pending index updates if needed
  34. $event = new AddMissingIndicesEvent();
  35. $this->dispatcher->dispatchTyped($event);
  36. $missingIndices = $event->getMissingIndices();
  37. $indicesToReplace = $event->getIndicesToReplace();
  38. if (!empty($missingIndices)) {
  39. $schema = new SchemaWrapper($this->connection);
  40. foreach ($missingIndices as $missingIndex) {
  41. if ($schema->hasTable($missingIndex['tableName'])) {
  42. $table = $schema->getTable($missingIndex['tableName']);
  43. if (!$table->hasIndex($missingIndex['indexName'])) {
  44. $indexInfo->addHintForMissingIndex($missingIndex['tableName'], $missingIndex['indexName']);
  45. }
  46. }
  47. }
  48. }
  49. if (!empty($indicesToReplace)) {
  50. $schema = new SchemaWrapper($this->connection);
  51. foreach ($indicesToReplace as $indexToReplace) {
  52. if ($schema->hasTable($indexToReplace['tableName'])) {
  53. $table = $schema->getTable($indexToReplace['tableName']);
  54. if (!$table->hasIndex($indexToReplace['newIndexName'])) {
  55. $indexInfo->addHintForMissingIndex($indexToReplace['tableName'], $indexToReplace['newIndexName']);
  56. }
  57. }
  58. }
  59. }
  60. return $indexInfo->getListOfMissingIndices();
  61. }
  62. public function run(): SetupResult {
  63. $missingIndices = $this->getMissingIndices();
  64. if (empty($missingIndices)) {
  65. return SetupResult::success('None');
  66. } else {
  67. $processed = 0;
  68. $list = $this->l10n->t('Missing indices:');
  69. foreach ($missingIndices as $missingIndex) {
  70. $processed++;
  71. $list .= "\n " . $this->l10n->t('"%s" in table "%s"', [$missingIndex['indexName'], $missingIndex['tableName']]);
  72. if (count($missingIndices) > $processed) {
  73. $list .= ", ";
  74. }
  75. }
  76. return SetupResult::warning(
  77. $this->l10n->t('Detected some missing optional indices. Occasionally new indices are added (by Nextcloud or installed applications) to improve database performance. Adding indices can sometimes take awhile and temporarily hurt performance so this is not done automatically during upgrades. Once the indices are added, queries to those tables should be faster. Use the command `occ db:add-missing-indices` to add them. ') . $list . '.',
  78. $this->urlGenerator->linkToDocs('admin-long-running-migration-steps')
  79. );
  80. }
  81. }
  82. }