Completed
Push — master ( b34843...6180fe )
by Morris
41:44 queued 25:33
created

MigrateCommand::completeArgumentValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, ownCloud GmbH
4
 *
5
 * @author Joas Schilling <[email protected]>
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
23
namespace OC\Core\Command\Db\Migrations;
24
25
26
use OC\DB\MigrationService;
27
use OC\Migration\ConsoleOutput;
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
36
class MigrateCommand extends Command implements CompletionAwareInterface {
37
38
	/** @var IDBConnection */
39
	private $connection;
40
41
	/**
42
	 * @param IDBConnection $connection
43
	 */
44
	public function __construct(IDBConnection $connection) {
45
		$this->connection = $connection;
46
		parent::__construct();
47
	}
48
49
	protected function configure() {
50
		$this
51
			->setName('migrations:migrate')
52
			->setDescription('Execute a migration to a specified version or the latest available version.')
53
			->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
54
			->addArgument('version', InputArgument::OPTIONAL, 'The version number (YYYYMMDDHHMMSS) or alias (first, prev, next, latest) to migrate to.', 'latest');
55
56
		parent::configure();
57
	}
58
59
	public function execute(InputInterface $input, OutputInterface $output) {
60
		$appName = $input->getArgument('app');
61
		$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
62
		$version = $input->getArgument('version');
63
64
		$ms->migrate($version);
65
	}
66
67
	/**
68
	 * @param string $optionName
69
	 * @param CompletionContext $context
70
	 * @return string[]
71
	 */
72
	public function completeOptionValues($optionName, CompletionContext $context) {
73
		return [];
74
	}
75
76
	/**
77
	 * @param string $argumentName
78
	 * @param CompletionContext $context
79
	 * @return string[]
80
	 */
81 View Code Duplication
	public function completeArgumentValues($argumentName, CompletionContext $context) {
82
		if ($argumentName === 'app') {
83
			$allApps = \OC_App::getAllApps();
84
			return array_diff($allApps, \OC_App::getEnabledApps(true, true));
85
		}
86
87
		if ($argumentName === 'version') {
88
			$appName = $context->getWordAtIndex($context->getWordIndex() - 1);
89
90
			$ms = new MigrationService($appName, $this->connection);
91
			$migrations = $ms->getAvailableVersions();
92
93
			array_unshift($migrations, 'next', 'latest');
94
			return $migrations;
95
		}
96
97
		return [];
98
	}
99
100
}
101