MigrateCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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\Connection;
  25. use OC\DB\MigrationService;
  26. use OC\Migration\ConsoleOutput;
  27. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  28. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  29. use Symfony\Component\Console\Command\Command;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class MigrateCommand extends Command implements CompletionAwareInterface {
  34. public function __construct(
  35. private Connection $connection,
  36. ) {
  37. parent::__construct();
  38. }
  39. protected function configure() {
  40. $this
  41. ->setName('migrations:migrate')
  42. ->setDescription('Execute a migration to a specified version or the latest available version.')
  43. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  44. ->addArgument('version', InputArgument::OPTIONAL, 'The version number (YYYYMMDDHHMMSS) or alias (first, prev, next, latest) to migrate to.', 'latest');
  45. parent::configure();
  46. }
  47. public function execute(InputInterface $input, OutputInterface $output): int {
  48. $appName = $input->getArgument('app');
  49. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  50. $version = $input->getArgument('version');
  51. $ms->migrate($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. }