1
0

DatabaseHasMissingColumns.php 3.1 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\MissingColumnInformation;
  27. use OC\DB\SchemaWrapper;
  28. use OCP\DB\Events\AddMissingColumnsEvent;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IL10N;
  31. use OCP\SetupCheck\ISetupCheck;
  32. use OCP\SetupCheck\SetupResult;
  33. class DatabaseHasMissingColumns 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 columns');
  45. }
  46. private function getMissingColumns(): array {
  47. $columnInfo = new MissingColumnInformation();
  48. // Dispatch event so apps can also hint for pending column updates if needed
  49. $event = new AddMissingColumnsEvent();
  50. $this->dispatcher->dispatchTyped($event);
  51. $missingColumns = $event->getMissingColumns();
  52. if (!empty($missingColumns)) {
  53. $schema = new SchemaWrapper($this->connection);
  54. foreach ($missingColumns as $missingColumn) {
  55. if ($schema->hasTable($missingColumn['tableName'])) {
  56. $table = $schema->getTable($missingColumn['tableName']);
  57. if (!$table->hasColumn($missingColumn['columnName'])) {
  58. $columnInfo->addHintForMissingColumn($missingColumn['tableName'], $missingColumn['columnName']);
  59. }
  60. }
  61. }
  62. }
  63. return $columnInfo->getListOfMissingColumns();
  64. }
  65. public function run(): SetupResult {
  66. $missingColumns = $this->getMissingColumns();
  67. if (empty($missingColumns)) {
  68. return SetupResult::success('None');
  69. } else {
  70. $list = '';
  71. foreach ($missingColumns as $missingColumn) {
  72. $list .= "\n".$this->l10n->t('Missing optional column "%s" in table "%s".', [$missingColumn['columnName'], $missingColumn['tableName']]);
  73. }
  74. return SetupResult::warning(
  75. $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
  76. );
  77. }
  78. }
  79. }