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.

ExecuteCommand.php 3.7KB

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