Completed
Push — master ( ca5656...60398b )
by Morris
32:43 queued 16:28
created

StatusCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\Db\Migrations;
23
24
use OC\DB\MigrationService;
25
use OC\Migration\ConsoleOutput;
26
use OCP\IDBConnection;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Input\InputArgument;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
32
class StatusCommand extends Command {
33
34
	/** @var IDBConnection */
35
	private $connection;
36
37
	/**
38
	 * @param IDBConnection $connection
39
	 */
40
	public function __construct(IDBConnection $connection) {
41
		$this->connection = $connection;
42
		parent::__construct();
43
	}
44
45
	protected function configure() {
46
		$this
47
			->setName('migrations:status')
48
			->setDescription('View the status of a set of migrations.')
49
			->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on');
50
	}
51
52
	public function execute(InputInterface $input, OutputInterface $output) {
53
		$appName = $input->getArgument('app');
54
		$ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
55
56
		$infos = $this->getMigrationsInfos($ms);
57
		foreach ($infos as $key => $value) {
58
			$output->writeln("    <comment>>></comment> $key: " . str_repeat(' ', 50 - strlen($key)) . $value);
59
		}
60
	}
61
62
	/**
63
	 * @param MigrationService $ms
64
	 * @return array associative array of human readable info name as key and the actual information as value
65
	 */
66
	public function getMigrationsInfos(MigrationService $ms) {
67
68
		$executedMigrations = $ms->getMigratedVersions();
69
		$availableMigrations = $ms->getAvailableVersions();
70
		$executedUnavailableMigrations = array_diff($executedMigrations, array_keys($availableMigrations));
71
72
		$numExecutedUnavailableMigrations = count($executedUnavailableMigrations);
73
		$numNewMigrations = count(array_diff(array_keys($availableMigrations), $executedMigrations));
74
75
		$infos = [
76
			'App'								=> $ms->getApp(),
77
			'Version Table Name'				=> $ms->getMigrationsTableName(),
78
			'Migrations Namespace'				=> $ms->getMigrationsNamespace(),
79
			'Migrations Directory'				=> $ms->getMigrationsDirectory(),
80
			'Previous Version'					=> $this->getFormattedVersionAlias($ms, 'prev'),
81
			'Current Version'					=> $this->getFormattedVersionAlias($ms, 'current'),
82
			'Next Version'						=> $this->getFormattedVersionAlias($ms, 'next'),
83
			'Latest Version'					=> $this->getFormattedVersionAlias($ms, 'latest'),
84
			'Executed Migrations'				=> count($executedMigrations),
85
			'Executed Unavailable Migrations'	=> $numExecutedUnavailableMigrations,
86
			'Available Migrations'				=> count($availableMigrations),
87
			'New Migrations'					=> $numNewMigrations,
88
		];
89
90
		return $infos;
91
	}
92
93
	/**
94
	 * @param MigrationService $migrationService
95
	 * @param string $alias
96
	 * @return mixed|null|string
97
	 */
98
	private function getFormattedVersionAlias(MigrationService $migrationService, $alias) {
99
		$migration = $migrationService->getMigration($alias);
100
		//No version found
101
		if ($migration === null) {
102
			if ($alias === 'next') {
103
				return 'Already at latest migration step';
104
			}
105
106
			if ($alias === 'prev') {
107
				return 'Already at first migration step';
108
			}
109
		}
110
111
		return $migration;
112
	}
113
114
115
}
116