You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddMissingColumns.php 3.1KB

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