DatabaseHasMissingPrimaryKeys.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\MissingPrimaryKeyInformation;
  27. use OC\DB\SchemaWrapper;
  28. use OCP\DB\Events\AddMissingPrimaryKeyEvent;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IL10N;
  31. use OCP\SetupCheck\ISetupCheck;
  32. use OCP\SetupCheck\SetupResult;
  33. class DatabaseHasMissingPrimaryKeys 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 primary keys');
  45. }
  46. private function getMissingPrimaryKeys(): array {
  47. $primaryKeyInfo = new MissingPrimaryKeyInformation();
  48. // Dispatch event so apps can also hint for pending primary key updates if needed
  49. $event = new AddMissingPrimaryKeyEvent();
  50. $this->dispatcher->dispatchTyped($event);
  51. $missingPrimaryKeys = $event->getMissingPrimaryKeys();
  52. if (!empty($missingPrimaryKeys)) {
  53. $schema = new SchemaWrapper($this->connection);
  54. foreach ($missingPrimaryKeys as $missingPrimaryKey) {
  55. if ($schema->hasTable($missingPrimaryKey['tableName'])) {
  56. $table = $schema->getTable($missingPrimaryKey['tableName']);
  57. if ($table->getPrimaryKey() === null) {
  58. $primaryKeyInfo->addHintForMissingPrimaryKey($missingPrimaryKey['tableName']);
  59. }
  60. }
  61. }
  62. }
  63. return $primaryKeyInfo->getListOfMissingPrimaryKeys();
  64. }
  65. public function run(): SetupResult {
  66. $missingPrimaryKeys = $this->getMissingPrimaryKeys();
  67. if (empty($missingPrimaryKeys)) {
  68. return SetupResult::success('None');
  69. } else {
  70. $list = '';
  71. foreach ($missingPrimaryKeys as $missingPrimaryKey) {
  72. $list .= "\n".$this->l10n->t('Missing primary key on table "%s".', [$missingPrimaryKey['tableName']]);
  73. }
  74. return SetupResult::warning(
  75. $this->l10n->t('The database is missing some primary keys. Due to the fact that adding primary keys on big tables could take some time they were not added automatically. By running "occ db:add-missing-primary-keys" those missing primary keys could be added manually while the instance keeps running.').$list
  76. );
  77. }
  78. }
  79. }