AddMissingPrimaryKeys.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Db;
  8. use OC\DB\Connection;
  9. use OC\DB\SchemaWrapper;
  10. use OCP\DB\Events\AddMissingPrimaryKeyEvent;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * Class AddMissingPrimaryKeys
  18. *
  19. * if you added primary keys to the database, this is the right place to add
  20. * your update routine for existing instances
  21. *
  22. * @package OC\Core\Command\Db
  23. */
  24. class AddMissingPrimaryKeys extends Command {
  25. public function __construct(
  26. private Connection $connection,
  27. private IEventDispatcher $dispatcher,
  28. ) {
  29. parent::__construct();
  30. }
  31. protected function configure() {
  32. $this
  33. ->setName('db:add-missing-primary-keys')
  34. ->setDescription('Add missing primary keys to the database tables')
  35. ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Output the SQL queries instead of running them.');
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output): int {
  38. $dryRun = $input->getOption('dry-run');
  39. // Dispatch event so apps can also update indexes if needed
  40. $event = new AddMissingPrimaryKeyEvent();
  41. $this->dispatcher->dispatchTyped($event);
  42. $missingKeys = $event->getMissingPrimaryKeys();
  43. $updated = false;
  44. if (!empty($missingKeys)) {
  45. $schema = new SchemaWrapper($this->connection);
  46. foreach ($missingKeys as $missingKey) {
  47. if ($schema->hasTable($missingKey['tableName'])) {
  48. $table = $schema->getTable($missingKey['tableName']);
  49. if (!$table->hasPrimaryKey()) {
  50. $output->writeln('<info>Adding primary key to the ' . $missingKey['tableName'] . ' table, this can take some time...</info>');
  51. $table->setPrimaryKey($missingKey['columns'], $missingKey['primaryKeyName']);
  52. if ($missingKey['formerIndex'] && $table->hasIndex($missingKey['formerIndex'])) {
  53. $table->dropIndex($missingKey['formerIndex']);
  54. }
  55. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  56. if ($dryRun && $sqlQueries !== null) {
  57. $output->writeln($sqlQueries);
  58. }
  59. $updated = true;
  60. $output->writeln('<info>' . $missingKey['tableName'] . ' table updated successfully.</info>');
  61. }
  62. }
  63. }
  64. }
  65. if (!$updated) {
  66. $output->writeln('<info>Done.</info>');
  67. }
  68. return 0;
  69. }
  70. }