AddMissingPrimaryKeys.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\Db;
  26. use OC\DB\Connection;
  27. use OC\DB\SchemaWrapper;
  28. use OCP\DB\Events\AddMissingPrimaryKeyEvent;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. /**
  35. * Class AddMissingPrimaryKeys
  36. *
  37. * if you added primary keys to the database, this is the right place to add
  38. * your update routine for existing instances
  39. *
  40. * @package OC\Core\Command\Db
  41. */
  42. class AddMissingPrimaryKeys extends Command {
  43. public function __construct(
  44. private Connection $connection,
  45. private IEventDispatcher $dispatcher,
  46. ) {
  47. parent::__construct();
  48. }
  49. protected function configure() {
  50. $this
  51. ->setName('db:add-missing-primary-keys')
  52. ->setDescription('Add missing primary keys to the database tables')
  53. ->addOption('dry-run', null, InputOption::VALUE_NONE, "Output the SQL queries instead of running them.");
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output): int {
  56. $dryRun = $input->getOption('dry-run');
  57. // Dispatch event so apps can also update indexes if needed
  58. $event = new AddMissingPrimaryKeyEvent();
  59. $this->dispatcher->dispatchTyped($event);
  60. $missingKeys = $event->getMissingPrimaryKeys();
  61. $updated = false;
  62. if (!empty($missingKeys)) {
  63. $schema = new SchemaWrapper($this->connection);
  64. foreach ($missingKeys as $missingKey) {
  65. if ($schema->hasTable($missingKey['tableName'])) {
  66. $table = $schema->getTable($missingKey['tableName']);
  67. if (!$table->hasPrimaryKey()) {
  68. $output->writeln('<info>Adding primary key to the ' . $missingKey['tableName'] . ' table, this can take some time...</info>');
  69. $table->setPrimaryKey($missingKey['columns'], $missingKey['primaryKeyName']);
  70. if ($missingKey['formerIndex'] && $table->hasIndex($missingKey['formerIndex'])) {
  71. $table->dropIndex($missingKey['formerIndex']);
  72. }
  73. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  74. if ($dryRun && $sqlQueries !== null) {
  75. $output->writeln($sqlQueries);
  76. }
  77. $updated = true;
  78. $output->writeln('<info>' . $missingKey['tableName'] . ' table updated successfully.</info>');
  79. }
  80. }
  81. }
  82. }
  83. if (!$updated) {
  84. $output->writeln('<info>Done.</info>');
  85. }
  86. return 0;
  87. }
  88. }