aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Plugins/PluginStatusMigrations.php
blob: 8bc087522dfd0fd5f845e66781ef9c21705c3824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace Studip\Cli\Commands\Plugins;

use Studip\Cli\Commands\AbstractPluginCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PluginStatusMigrations extends AbstractPluginCommand
{
    protected static $defaultName = 'plugin:status-migrations';

    protected function configure(): void
    {
        $this->setDescription('Shows the state of all migrations of a plugin.');
        $this->setHelp('This command shows the state of all migrations of a plugin.');
        $this->addArgument('pluginname', InputArgument::REQUIRED, 'name of the plugin');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $pluginname = $input->getArgument('pluginname');
        $verbose = $input->getOption('verbose');

        $pluginManager = \PluginManager::getInstance();
        $plugin = $this->findPluginByName($pluginManager, $pluginname);
        if (null === $plugin) {
            $output->writeln('<error>Could not find plugin of that name.</error>');

            return Command::FAILURE;
        }

        $pluginpath = \Config::get()->PLUGINS_PATH . '/' . $plugin['path'];

        if (!is_dir($pluginpath . '/migrations')) {
            $output->writeln('<comment>Could not find any migrations of that plugin.</comment>');

            return Command::SUCCESS;
        }

        // if there are migrations, migrate
        $schemaVersion = new \DBSchemaVersion($plugin['name']);
        $migrator = new \Migrator($pluginpath . '/migrations', $schemaVersion, $verbose);


        $migrations = $migrator->migrationClasses();
        uksort($migrations, 'version_compare');
        $migrations = array_reverse($migrations, true);

        $pending = $migrator->relevantMigrations(null);

        $rows = [];
        foreach ($migrations as $number => $migration) {
            $rows[] = [isset($pending[$number]) ? 'No' : 'Yes', $number, basename($migration[0], '.php')];
        }

        $table = new Table($output);
        $table->setHeaders(['Ran?', 'ID', 'Migration'])->setRows($rows);
        $table->setStyle('box');
        $table->render();

        return Command::SUCCESS;
    }
}