StatusCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 StatusCommand 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:status')
  45. ->setDescription('View the status of a set of migrations.')
  46. ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on');
  47. }
  48. public function execute(InputInterface $input, OutputInterface $output) {
  49. $appName = $input->getArgument('app');
  50. $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output));
  51. $infos = $this->getMigrationsInfos($ms);
  52. foreach ($infos as $key => $value) {
  53. if (is_array($value)) {
  54. $output->writeln(" <comment>>></comment> $key:");
  55. foreach ($value as $subKey => $subValue) {
  56. $output->writeln(" <comment>>></comment> $subKey: " . str_repeat(' ', 46 - strlen($subKey)) . $subValue);
  57. }
  58. } else {
  59. $output->writeln(" <comment>>></comment> $key: " . str_repeat(' ', 50 - strlen($key)) . $value);
  60. }
  61. }
  62. }
  63. /**
  64. * @param string $optionName
  65. * @param CompletionContext $context
  66. * @return string[]
  67. */
  68. public function completeOptionValues($optionName, CompletionContext $context) {
  69. return [];
  70. }
  71. /**
  72. * @param string $argumentName
  73. * @param CompletionContext $context
  74. * @return string[]
  75. */
  76. public function completeArgumentValues($argumentName, CompletionContext $context) {
  77. if ($argumentName === 'app') {
  78. $allApps = \OC_App::getAllApps();
  79. return array_diff($allApps, \OC_App::getEnabledApps(true, true));
  80. }
  81. return [];
  82. }
  83. /**
  84. * @param MigrationService $ms
  85. * @return array associative array of human readable info name as key and the actual information as value
  86. */
  87. public function getMigrationsInfos(MigrationService $ms) {
  88. $executedMigrations = $ms->getMigratedVersions();
  89. $availableMigrations = $ms->getAvailableVersions();
  90. $executedUnavailableMigrations = array_diff($executedMigrations, array_keys($availableMigrations));
  91. $numExecutedUnavailableMigrations = count($executedUnavailableMigrations);
  92. $numNewMigrations = count(array_diff(array_keys($availableMigrations), $executedMigrations));
  93. $pending = $ms->describeMigrationStep('lastest');
  94. $infos = [
  95. 'App' => $ms->getApp(),
  96. 'Version Table Name' => $ms->getMigrationsTableName(),
  97. 'Migrations Namespace' => $ms->getMigrationsNamespace(),
  98. 'Migrations Directory' => $ms->getMigrationsDirectory(),
  99. 'Previous Version' => $this->getFormattedVersionAlias($ms, 'prev'),
  100. 'Current Version' => $this->getFormattedVersionAlias($ms, 'current'),
  101. 'Next Version' => $this->getFormattedVersionAlias($ms, 'next'),
  102. 'Latest Version' => $this->getFormattedVersionAlias($ms, 'latest'),
  103. 'Executed Migrations' => count($executedMigrations),
  104. 'Executed Unavailable Migrations' => $numExecutedUnavailableMigrations,
  105. 'Available Migrations' => count($availableMigrations),
  106. 'New Migrations' => $numNewMigrations,
  107. 'Pending Migrations' => count($pending) ? $pending : 'None'
  108. ];
  109. return $infos;
  110. }
  111. /**
  112. * @param MigrationService $migrationService
  113. * @param string $alias
  114. * @return mixed|null|string
  115. */
  116. private function getFormattedVersionAlias(MigrationService $migrationService, $alias) {
  117. $migration = $migrationService->getMigration($alias);
  118. //No version found
  119. if ($migration === null) {
  120. if ($alias === 'next') {
  121. return 'Already at latest migration step';
  122. }
  123. if ($alias === 'prev') {
  124. return 'Already at first migration step';
  125. }
  126. }
  127. return $migration;
  128. }
  129. }