MigrateCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 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 Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  12. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class MigrateCommand extends Command implements CompletionAwareInterface {
  18. public function __construct(
  19. private Connection $connection,
  20. ) {
  21. parent::__construct();
  22. }
  23. protected function configure() {
  24. $this
  25. ->setName('migrations:migrate')
  26. ->setDescription('Execute a migration to a specified version or the latest available version.')
  27. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  28. ->addArgument('version', InputArgument::OPTIONAL, 'The version number (YYYYMMDDHHMMSS) or alias (first, prev, next, latest) to migrate to.', 'latest');
  29. parent::configure();
  30. }
  31. public function execute(InputInterface $input, OutputInterface $output): int {
  32. $appName = $input->getArgument('app');
  33. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  34. $version = $input->getArgument('version');
  35. $ms->migrate($version);
  36. return 0;
  37. }
  38. /**
  39. * @param string $optionName
  40. * @param CompletionContext $context
  41. * @return string[]
  42. */
  43. public function completeOptionValues($optionName, CompletionContext $context) {
  44. return [];
  45. }
  46. /**
  47. * @param string $argumentName
  48. * @param CompletionContext $context
  49. * @return string[]
  50. */
  51. public function completeArgumentValues($argumentName, CompletionContext $context) {
  52. if ($argumentName === 'app') {
  53. $allApps = \OC_App::getAllApps();
  54. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  55. }
  56. if ($argumentName === 'version') {
  57. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  58. $ms = new MigrationService($appName, $this->connection);
  59. $migrations = $ms->getAvailableVersions();
  60. array_unshift($migrations, 'next', 'latest');
  61. return $migrations;
  62. }
  63. return [];
  64. }
  65. }