Completed
Push — master ( 47b2ba...97e8f3 )
by Thomas
27s
created

StatusCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getMigrationsInfos() 0 26 1
A __construct() 0 4 1
A configure() 0 6 1
A execute() 0 9 2
A getFormattedVersionAlias() 0 15 4
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, 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 OCP\IDBConnection;
26
use Symfony\Component\Console\Command\Command;
27
use Symfony\Component\Console\Input\InputArgument;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
class StatusCommand extends Command {
32
33
	/** @var IDBConnection */
34
	private $connection;
35
36
	/**
37
	 * @param IDBConnection $connection
38
	 */
39
	public function __construct(IDBConnection $connection) {
40
		$this->connection = $connection;
41
		parent::__construct();
42
	}
43
44
	protected function configure() {
45
		$this
46
			->setName('migrations:status')
47
			->setDescription('View the status of a set of migrations.')
48
			->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on');
49
	}
50
51
	public function execute(InputInterface $input, OutputInterface $output) {
52
		$appName = $input->getArgument('app');
53
		$ms = new MigrationService($appName, $this->connection);
54
55
		$infos = $this->getMigrationsInfos($ms);
56
		foreach ($infos as $key => $value) {
57
			$output->writeln("    <comment>>></comment> $key: " . str_repeat(' ', 50 - strlen($key)) . $value);
58
		}
59
	}
60
61
	/**
62
	 * @param MigrationService $ms
63
	 * @return array associative array of human readable info name as key and the actual information as value
64
	 */
65
	public function getMigrationsInfos(MigrationService $ms) {
66
67
		$executedMigrations = $ms->getMigratedVersions();
68
		$availableMigrations = $ms->getAvailableVersions();
69
		$executedUnavailableMigrations = array_diff($executedMigrations, array_keys($availableMigrations));
70
71
		$numExecutedUnavailableMigrations = count($executedUnavailableMigrations);
72
		$numNewMigrations = count(array_diff(array_keys($availableMigrations), $executedMigrations));
73
74
		$infos = [
75
			'App'								=> $ms->getApp(),
76
			'Version Table Name'				=> $ms->getMigrationsTableName(),
77
			'Migrations Namespace'				=> $ms->getMigrationsNamespace(),
78
			'Migrations Directory'				=> $ms->getMigrationsDirectory(),
79
			'Previous Version'					=> $this->getFormattedVersionAlias($ms, 'prev'),
80
			'Current Version'					=> $this->getFormattedVersionAlias($ms, 'current'),
81
			'Next Version'						=> $this->getFormattedVersionAlias($ms, 'next'),
82
			'Latest Version'					=> $this->getFormattedVersionAlias($ms, 'latest'),
83
			'Executed Migrations'				=> count($executedMigrations),
84
			'Executed Unavailable Migrations'	=> $numExecutedUnavailableMigrations,
85
			'Available Migrations'				=> count($availableMigrations),
86
			'New Migrations'					=> $numNewMigrations,
87
		];
88
89
		return $infos;
90
	}
91
92
	/**
93
	 * @param MigrationService $migrationService
94
	 * @param string $alias
95
	 * @return mixed|null|string
96
	 */
97
	private function getFormattedVersionAlias(MigrationService $migrationService, $alias) {
98
		$migration = $migrationService->getMigration($alias);
99
		//No version found
100
		if ($migration === null) {
101
			if ($alias === 'next') {
102
				return 'Already at latest migration step';
103
			}
104
105
			if ($alias === 'prev') {
106
				return 'Already at first migration step';
107
			}
108
		}
109
110
		return $migration;
111
	}
112
113
114
}
115