aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Migrate/MigrateStatus.php
blob: e07bbe18bf21d26413efa91a2b313d217d18a690 (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
<?php

namespace Studip\Cli\Commands\Migrate;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateStatus extends Command
{
    protected static $defaultName = 'migrate:status';

    protected function configure(): void
    {
        $this->setDescription('Shows the state of all migrations.');
        $this->setHelp('This command shows the state of all migrations.');

        $this->addOption('domain', 'd', InputOption::VALUE_OPTIONAL, 'the domain of the migrations', 'studip');

        $defaultPath = $GLOBALS['STUDIP_BASE_PATH'] . '/db/migrations';
        $this->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'the id of the migration to list to', $defaultPath);
    }

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

        $version = new \DBSchemaVersion($domain);
        $migrator = new \Migrator($path, $version, $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;
    }
}