ExecuteCommand.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2017 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command\Db\Migrations;
  8. use OC\DB\Connection;
  9. use OC\DB\MigrationService;
  10. use OC\Migration\ConsoleOutput;
  11. use OCP\IConfig;
  12. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  13. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. class ExecuteCommand extends Command implements CompletionAwareInterface {
  19. public function __construct(
  20. private Connection $connection,
  21. private IConfig $config,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure() {
  26. $this
  27. ->setName('migrations:execute')
  28. ->setDescription('Execute a single migration version manually.')
  29. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  30. ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null);
  31. parent::configure();
  32. }
  33. /**
  34. * @param InputInterface $input
  35. * @param OutputInterface $output
  36. * @return int
  37. */
  38. public function execute(InputInterface $input, OutputInterface $output): int {
  39. $appName = $input->getArgument('app');
  40. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  41. $version = $input->getArgument('version');
  42. if ($this->config->getSystemValue('debug', false) === false) {
  43. $olderVersions = $ms->getMigratedVersions();
  44. $olderVersions[] = '0';
  45. $olderVersions[] = 'prev';
  46. if (in_array($version, $olderVersions, true)) {
  47. $output->writeln('<error>Can not go back to previous migration without debug enabled</error>');
  48. return 1;
  49. }
  50. }
  51. $ms->executeStep($version);
  52. return 0;
  53. }
  54. /**
  55. * @param string $optionName
  56. * @param CompletionContext $context
  57. * @return string[]
  58. */
  59. public function completeOptionValues($optionName, CompletionContext $context) {
  60. return [];
  61. }
  62. /**
  63. * @param string $argumentName
  64. * @param CompletionContext $context
  65. * @return string[]
  66. */
  67. public function completeArgumentValues($argumentName, CompletionContext $context) {
  68. if ($argumentName === 'app') {
  69. $allApps = \OC_App::getAllApps();
  70. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  71. }
  72. if ($argumentName === 'version') {
  73. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  74. $ms = new MigrationService($appName, $this->connection);
  75. $migrations = $ms->getAvailableVersions();
  76. array_unshift($migrations, 'next', 'latest');
  77. return $migrations;
  78. }
  79. return [];
  80. }
  81. }