ExecuteCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. public function __construct(
  37. private Connection $connection,
  38. private IConfig $config,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure() {
  43. $this
  44. ->setName('migrations:execute')
  45. ->setDescription('Execute a single migration version manually.')
  46. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  47. ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null);
  48. parent::configure();
  49. }
  50. /**
  51. * @param InputInterface $input
  52. * @param OutputInterface $output
  53. * @return int
  54. */
  55. public function execute(InputInterface $input, OutputInterface $output): int {
  56. $appName = $input->getArgument('app');
  57. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  58. $version = $input->getArgument('version');
  59. if ($this->config->getSystemValue('debug', false) === false) {
  60. $olderVersions = $ms->getMigratedVersions();
  61. $olderVersions[] = '0';
  62. $olderVersions[] = 'prev';
  63. if (in_array($version, $olderVersions, true)) {
  64. $output->writeln('<error>Can not go back to previous migration without debug enabled</error>');
  65. return 1;
  66. }
  67. }
  68. $ms->executeStep($version);
  69. return 0;
  70. }
  71. /**
  72. * @param string $optionName
  73. * @param CompletionContext $context
  74. * @return string[]
  75. */
  76. public function completeOptionValues($optionName, CompletionContext $context) {
  77. return [];
  78. }
  79. /**
  80. * @param string $argumentName
  81. * @param CompletionContext $context
  82. * @return string[]
  83. */
  84. public function completeArgumentValues($argumentName, CompletionContext $context) {
  85. if ($argumentName === 'app') {
  86. $allApps = \OC_App::getAllApps();
  87. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  88. }
  89. if ($argumentName === 'version') {
  90. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  91. $ms = new MigrationService($appName, $this->connection);
  92. $migrations = $ms->getAvailableVersions();
  93. array_unshift($migrations, 'next', 'latest');
  94. return $migrations;
  95. }
  96. return [];
  97. }
  98. }