ExecuteCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. * @copyright Copyright (c) 2017, ownCloud GmbH
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command\Db\Migrations;
  24. use OC\DB\MigrationService;
  25. use OC\Migration\ConsoleOutput;
  26. use OCP\App\IAppManager;
  27. use OCP\IConfig;
  28. use OCP\IDBConnection;
  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 IDBConnection */
  37. private $connection;
  38. /** @var IConfig */
  39. private $config;
  40. /** @var IAppManager */
  41. protected $appManager;
  42. /**
  43. * ExecuteCommand constructor.
  44. *
  45. * @param IDBConnection $connection
  46. * @param IConfig $config
  47. * @param IAppManager $appManager
  48. */
  49. public function __construct(IDBConnection $connection, IAppManager $appManager, IConfig $config) {
  50. $this->connection = $connection;
  51. $this->config = $config;
  52. parent::__construct();
  53. }
  54. protected function configure() {
  55. $this
  56. ->setName('migrations:execute')
  57. ->setDescription('Execute a single migration version manually.')
  58. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  59. ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null);
  60. parent::configure();
  61. }
  62. /**
  63. * @param InputInterface $input
  64. * @param OutputInterface $output
  65. * @return int
  66. */
  67. public function execute(InputInterface $input, OutputInterface $output) {
  68. $appName = $input->getArgument('app');
  69. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  70. $version = $input->getArgument('version');
  71. if ($this->config->getSystemValue('debug', false) === false) {
  72. $olderVersions = $ms->getMigratedVersions();
  73. $olderVersions[] = '0';
  74. $olderVersions[] = 'prev';
  75. if (in_array($version, $olderVersions, true)) {
  76. $output->writeln('<error>Can not go back to previous migration without debug enabled</error>');
  77. return 1;
  78. }
  79. }
  80. $ms->executeStep($version);
  81. return 0;
  82. }
  83. /**
  84. * @param string $optionName
  85. * @param CompletionContext $context
  86. * @return string[]
  87. */
  88. public function completeOptionValues($optionName, CompletionContext $context) {
  89. return [];
  90. }
  91. /**
  92. * @param string $argumentName
  93. * @param CompletionContext $context
  94. * @return string[]
  95. */
  96. public function completeArgumentValues($argumentName, CompletionContext $context) {
  97. if ($argumentName === 'app') {
  98. $allApps = \OC_App::getAllApps();
  99. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  100. }
  101. if ($argumentName === 'version') {
  102. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  103. $ms = new MigrationService($appName, $this->connection);
  104. $migrations = $ms->getAvailableVersions();
  105. array_unshift($migrations, 'next', 'latest');
  106. return $migrations;
  107. }
  108. return [];
  109. }
  110. }