1
0

DatabaseHasMissingColumns.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\MissingColumnInformation;
  10. use OC\DB\SchemaWrapper;
  11. use OCP\DB\Events\AddMissingColumnsEvent;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\IL10N;
  14. use OCP\SetupCheck\ISetupCheck;
  15. use OCP\SetupCheck\SetupResult;
  16. class DatabaseHasMissingColumns implements ISetupCheck {
  17. public function __construct(
  18. private IL10N $l10n,
  19. private Connection $connection,
  20. private IEventDispatcher $dispatcher,
  21. ) {
  22. }
  23. public function getCategory(): string {
  24. return 'database';
  25. }
  26. public function getName(): string {
  27. return $this->l10n->t('Database missing columns');
  28. }
  29. private function getMissingColumns(): array {
  30. $columnInfo = new MissingColumnInformation();
  31. // Dispatch event so apps can also hint for pending column updates if needed
  32. $event = new AddMissingColumnsEvent();
  33. $this->dispatcher->dispatchTyped($event);
  34. $missingColumns = $event->getMissingColumns();
  35. if (!empty($missingColumns)) {
  36. $schema = new SchemaWrapper($this->connection);
  37. foreach ($missingColumns as $missingColumn) {
  38. if ($schema->hasTable($missingColumn['tableName'])) {
  39. $table = $schema->getTable($missingColumn['tableName']);
  40. if (!$table->hasColumn($missingColumn['columnName'])) {
  41. $columnInfo->addHintForMissingColumn($missingColumn['tableName'], $missingColumn['columnName']);
  42. }
  43. }
  44. }
  45. }
  46. return $columnInfo->getListOfMissingColumns();
  47. }
  48. public function run(): SetupResult {
  49. $missingColumns = $this->getMissingColumns();
  50. if (empty($missingColumns)) {
  51. return SetupResult::success('None');
  52. } else {
  53. $list = '';
  54. foreach ($missingColumns as $missingColumn) {
  55. $list .= "\n" . $this->l10n->t('Missing optional column "%s" in table "%s".', [$missingColumn['columnName'], $missingColumn['tableName']]);
  56. }
  57. return SetupResult::warning(
  58. $this->l10n->t('The database is missing some optional columns. Due to the fact that adding columns on big tables could take some time they were not added automatically when they can be optional. By running "occ db:add-missing-columns" those missing columns could be added manually while the instance keeps running. Once the columns are added some features might improve responsiveness or usability.') . $list
  59. );
  60. }
  61. }
  62. }