1
0

DatabaseHasMissingIndices.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\SetupChecks;
  25. use OC\DB\Connection;
  26. use OC\DB\MissingIndexInformation;
  27. use OC\DB\SchemaWrapper;
  28. use OCP\DB\Events\AddMissingIndicesEvent;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IL10N;
  31. use OCP\SetupCheck\ISetupCheck;
  32. use OCP\SetupCheck\SetupResult;
  33. class DatabaseHasMissingIndices implements ISetupCheck {
  34. public function __construct(
  35. private IL10N $l10n,
  36. private Connection $connection,
  37. private IEventDispatcher $dispatcher,
  38. ) {
  39. }
  40. public function getCategory(): string {
  41. return 'database';
  42. }
  43. public function getName(): string {
  44. return $this->l10n->t('Database missing indices');
  45. }
  46. private function getMissingIndices(): array {
  47. $indexInfo = new MissingIndexInformation();
  48. // Dispatch event so apps can also hint for pending index updates if needed
  49. $event = new AddMissingIndicesEvent();
  50. $this->dispatcher->dispatchTyped($event);
  51. $missingIndices = $event->getMissingIndices();
  52. if (!empty($missingIndices)) {
  53. $schema = new SchemaWrapper($this->connection);
  54. foreach ($missingIndices as $missingIndex) {
  55. if ($schema->hasTable($missingIndex['tableName'])) {
  56. $table = $schema->getTable($missingIndex['tableName']);
  57. if (!$table->hasIndex($missingIndex['indexName'])) {
  58. $indexInfo->addHintForMissingIndex($missingIndex['tableName'], $missingIndex['indexName']);
  59. }
  60. }
  61. }
  62. }
  63. return $indexInfo->getListOfMissingIndices();
  64. }
  65. public function run(): SetupResult {
  66. $missingIndices = $this->getMissingIndices();
  67. if (empty($missingIndices)) {
  68. return SetupResult::success('None');
  69. } else {
  70. $list = '';
  71. foreach ($missingIndices as $missingIndex) {
  72. $list .= "\n".$this->l10n->t('Missing optional index "%s" in table "%s".', [$missingIndex['indexName'], $missingIndex['tableName']]);
  73. }
  74. return SetupResult::warning(
  75. $this->l10n->t('The database is missing some indexes. Due to the fact that adding indexes on big tables could take some time they were not added automatically. By running "occ db:add-missing-indices" those missing indexes could be added manually while the instance keeps running. Once the indexes are added queries to those tables are usually much faster.').$list
  76. );
  77. }
  78. }
  79. }