AddMissingColumns.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\DB\Events\AddMissingColumnsEvent;
  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 AddMissingColumns
  36. *
  37. * if you added a new lazy column 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 AddMissingColumns 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-columns')
  52. ->setDescription('Add missing optional columns 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 columns if needed
  58. $event = new AddMissingColumnsEvent();
  59. $this->dispatcher->dispatchTyped($event);
  60. $missingColumns = $event->getMissingColumns();
  61. $updated = false;
  62. if (!empty($missingColumns)) {
  63. $schema = new SchemaWrapper($this->connection);
  64. foreach ($missingColumns as $missingColumn) {
  65. if ($schema->hasTable($missingColumn['tableName'])) {
  66. $table = $schema->getTable($missingColumn['tableName']);
  67. if (!$table->hasColumn($missingColumn['columnName'])) {
  68. $output->writeln('<info>Adding additional ' . $missingColumn['columnName'] . ' column to the ' . $missingColumn['tableName'] . ' table, this can take some time...</info>');
  69. $table->addColumn($missingColumn['columnName'], $missingColumn['typeName'], $missingColumn['options']);
  70. $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
  71. if ($dryRun && $sqlQueries !== null) {
  72. $output->writeln($sqlQueries);
  73. }
  74. $updated = true;
  75. $output->writeln('<info>' . $missingColumn['tableName'] . ' table updated successfully.</info>');
  76. }
  77. }
  78. }
  79. }
  80. if (!$updated) {
  81. $output->writeln('<info>Done.</info>');
  82. }
  83. return 0;
  84. }
  85. }