AddMissingColumns.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
  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\IDBConnection;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. use Symfony\Component\EventDispatcher\GenericEvent;
  35. /**
  36. * Class AddMissingColumns
  37. *
  38. * if you added a new lazy column to the database, this is the right place to add
  39. * your update routine for existing instances
  40. *
  41. * @package OC\Core\Command\Db
  42. */
  43. class AddMissingColumns extends Command {
  44. private Connection $connection;
  45. private EventDispatcherInterface $dispatcher;
  46. public function __construct(Connection $connection, EventDispatcherInterface $dispatcher) {
  47. parent::__construct();
  48. $this->connection = $connection;
  49. $this->dispatcher = $dispatcher;
  50. }
  51. protected function configure() {
  52. $this
  53. ->setName('db:add-missing-columns')
  54. ->setDescription('Add missing optional columns to the database tables')
  55. ->addOption('dry-run', null, InputOption::VALUE_NONE, "Output the SQL queries instead of running them.");
  56. }
  57. protected function execute(InputInterface $input, OutputInterface $output): int {
  58. $this->addCoreColumns($output, $input->getOption('dry-run'));
  59. // Dispatch event so apps can also update columns if needed
  60. $event = new GenericEvent($output);
  61. $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_COLUMNS_EVENT, $event);
  62. return 0;
  63. }
  64. /**
  65. * add missing indices to the share table
  66. *
  67. * @param OutputInterface $output
  68. * @param bool $dryRun If true, will return the sql queries instead of running them.
  69. * @throws \Doctrine\DBAL\Schema\SchemaException
  70. */
  71. private function addCoreColumns(OutputInterface $output, bool $dryRun): void {
  72. $output->writeln('<info>Check columns of the comments table.</info>');
  73. $schema = new SchemaWrapper($this->connection);
  74. $updated = false;
  75. if ($schema->hasTable('comments')) {
  76. $table = $schema->getTable('comments');
  77. if (!$table->hasColumn('reference_id')) {
  78. $output->writeln('<info>Adding additional reference_id column to the comments table, this can take some time...</info>');
  79. $table->addColumn('reference_id', 'string', [
  80. 'notnull' => false,
  81. 'length' => 64,
  82. ]);
  83. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  84. if ($dryRun && $sqlQueries !== null) {
  85. $output->writeln($sqlQueries);
  86. }
  87. $updated = true;
  88. $output->writeln('<info>Comments table updated successfully.</info>');
  89. }
  90. }
  91. if (!$updated) {
  92. $output->writeln('<info>Done.</info>');
  93. }
  94. }
  95. }