MigrateCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Db\Migrations;
  23. use OC\DB\MigrationService;
  24. use OC\Migration\ConsoleOutput;
  25. use OCP\IDBConnection;
  26. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  27. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class MigrateCommand extends Command implements CompletionAwareInterface {
  33. /** @var IDBConnection */
  34. private $connection;
  35. /**
  36. * @param IDBConnection $connection
  37. */
  38. public function __construct(IDBConnection $connection) {
  39. $this->connection = $connection;
  40. parent::__construct();
  41. }
  42. protected function configure() {
  43. $this
  44. ->setName('migrations:migrate')
  45. ->setDescription('Execute a migration to a specified version or the latest available version.')
  46. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
  47. ->addArgument('version', InputArgument::OPTIONAL, 'The version number (YYYYMMDDHHMMSS) or alias (first, prev, next, latest) to migrate to.', 'latest');
  48. parent::configure();
  49. }
  50. public function execute(InputInterface $input, OutputInterface $output) {
  51. $appName = $input->getArgument('app');
  52. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  53. $version = $input->getArgument('version');
  54. $ms->migrate($version);
  55. }
  56. /**
  57. * @param string $optionName
  58. * @param CompletionContext $context
  59. * @return string[]
  60. */
  61. public function completeOptionValues($optionName, CompletionContext $context) {
  62. return [];
  63. }
  64. /**
  65. * @param string $argumentName
  66. * @param CompletionContext $context
  67. * @return string[]
  68. */
  69. public function completeArgumentValues($argumentName, CompletionContext $context) {
  70. if ($argumentName === 'app') {
  71. $allApps = \OC_App::getAllApps();
  72. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  73. }
  74. if ($argumentName === 'version') {
  75. $appName = $context->getWordAtIndex($context->getWordIndex() - 1);
  76. $ms = new MigrationService($appName, $this->connection);
  77. $migrations = $ms->getAvailableVersions();
  78. array_unshift($migrations, 'next', 'latest');
  79. return $migrations;
  80. }
  81. return [];
  82. }
  83. }