ExecuteCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. * @copyright Copyright (c) 2017, ownCloud GmbH
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Core\Command\Db\Migrations;
  25. use OC\DB\Connection;
  26. use OC\DB\MigrationService;
  27. use OC\Migration\ConsoleOutput;
  28. use OCP\IConfig;
  29. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  30. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  31. use Symfony\Component\Console\Command\Command;
  32. use Symfony\Component\Console\Input\InputArgument;
  33. use Symfony\Component\Console\Input\InputInterface;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class ExecuteCommand extends Command implements CompletionAwareInterface {
  36. private Connection $connection;
  37. private IConfig $config;
  38. public function __construct(Connection $connection, IConfig $config) {
  39. $this->connection = $connection;
  40. $this->config = $config;
  41. parent::__construct();
  42. }
  43. protected function configure() {
  44. $this
  45. ->setName('migrations:execute')
  46. ->setDescription('Execute a single migration version manually.')
  47. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  48. ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null);
  49. parent::configure();
  50. }
  51. /**
  52. * @param InputInterface $input
  53. * @param OutputInterface $output
  54. * @return int
  55. */
  56. public function execute(InputInterface $input, OutputInterface $output): int {
  57. $appName = $input->getArgument('app');
  58. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  59. $version = $input->getArgument('version');
  60. if ($this->config->getSystemValue('debug', false) === false) {
  61. $olderVersions = $ms->getMigratedVersions();
  62. $olderVersions[] = '0';
  63. $olderVersions[] = 'prev';
  64. if (in_array($version, $olderVersions, true)) {
  65. $output->writeln('<error>Can not go back to previous migration without debug enabled</error>');
  66. return 1;
  67. }
  68. }
  69. $ms->executeStep($version);
  70. return 0;
  71. }
  72. /**
  73. * @param string $optionName
  74. * @param CompletionContext $context
  75. * @return string[]
  76. */
  77. public function completeOptionValues($optionName, CompletionContext $context) {
  78. return [];
  79. }
  80. /**
  81. * @param string $argumentName
  82. * @param CompletionContext $context
  83. * @return string[]
  84. */
  85. public function completeArgumentValues($argumentName, CompletionContext $context) {
  86. if ($argumentName === 'app') {
  87. $allApps = \OC_App::getAllApps();
  88. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  89. }
  90. if ($argumentName === 'version') {
  91. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  92. $ms = new MigrationService($appName, $this->connection);
  93. $migrations = $ms->getAvailableVersions();
  94. array_unshift($migrations, 'next', 'latest');
  95. return $migrations;
  96. }
  97. return [];
  98. }
  99. }