Completed
Push — master ( 39c620...12e227 )
by Mike
03:57
created

StatusCommand::showVersions()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 9.2
cc 4
eloc 13
nc 5
nop 3
crap 20
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
21
22
use Doctrine\DBAL\Migrations\Configuration\AbstractFileConfiguration;
23
use Doctrine\DBAL\Migrations\Configuration\Configuration;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
28
/**
29
 * Command to view the status of a set of migrations.
30
 *
31
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
32
 * @link    www.doctrine-project.org
33
 * @since   2.0
34
 * @author  Jonathan Wage <[email protected]>
35
 */
36
class StatusCommand extends AbstractCommand
37
{
38 9
    protected function configure()
39
    {
40 9
        $this
41 9
            ->setName('migrations:status')
42 9
            ->setDescription('View the status of a set of migrations.')
43 9
            ->addOption('show-versions', null, InputOption::VALUE_NONE, 'This will display a list of all available migrations and their status')
44 9
            ->setHelp(<<<EOT
45
The <info>%command.name%</info> command outputs the status of a set of migrations:
46
47
    <info>%command.full_name%</info>
48
49
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
50
51
    <info>%command.full_name% --show-versions</info>
52
EOT
53 9
        );
54
55 9
        parent::configure();
56 9
    }
57
58 7
    public function execute(InputInterface $input, OutputInterface $output)
59
    {
60 7
        $configuration = $this->getMigrationConfiguration($input, $output);
61
62 7
        $formattedVersions = [];
63 7
        foreach (['prev', 'current', 'next', 'latest'] as $alias) {
64 7
            $version = $configuration->resolveVersionAlias($alias);
65 7
            if ($version === null) {
66 7
                if ($alias == 'next') {
67 3
                    $formattedVersions[$alias] = 'Already at latest version';
68 7
                } elseif ($alias == 'prev') {
69 7
                    $formattedVersions[$alias] = 'Already at first version';
70 7
                }
71 7
            } elseif ($version === '0') {
72 7
                $formattedVersions[$alias] = '<comment>0</comment>';
73 7
            } else {
74 7
                $formattedVersions[$alias] = $configuration->getDateTime($version) . ' (<comment>' . $version . '</comment>)';
75
            }
76 7
        }
77
78 7
        $executedMigrations = $configuration->getMigratedVersions();
79 7
        $availableMigrations = $configuration->getAvailableVersions();
80 7
        $executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations);
81 7
        $numExecutedUnavailableMigrations = count($executedUnavailableMigrations);
82 7
        $newMigrations = count(array_diff($availableMigrations, $executedMigrations));
83
84 7
        $output->writeln("\n <info>==</info> Configuration\n");
85
86
        $info = [
87 7
            'Name'                              => $configuration->getName() ? $configuration->getName() : 'Doctrine Database Migrations',
88 7
            'Database Driver'                   => $configuration->getConnection()->getDriver()->getName(),
89 7
            'Database Name'                     => $configuration->getConnection()->getDatabase(),
90 7
            'Configuration Source'              => $configuration instanceof AbstractFileConfiguration ? $configuration->getFile() : 'manually configured',
91 7
            'Version Table Name'                => $configuration->getMigrationsTableName(),
92 7
            'Version Column Name'                => $configuration->getMigrationsColumnName(),
93 7
            'Migrations Namespace'              => $configuration->getMigrationsNamespace(),
94 7
            'Migrations Directory'              => $configuration->getMigrationsDirectory(),
95 7
            'Previous Version'                  => $formattedVersions['prev'],
96 7
            'Current Version'                   => $formattedVersions['current'],
97 7
            'Next Version'                      => $formattedVersions['next'],
98 7
            'Latest Version'                    => $formattedVersions['latest'],
99 7
            'Executed Migrations'               => count($executedMigrations),
100 7
            'Executed Unavailable Migrations'   => $numExecutedUnavailableMigrations > 0 ? '<error>'.$numExecutedUnavailableMigrations.'</error>' : 0,
101 7
            'Available Migrations'              => count($availableMigrations),
102 7
            'New Migrations'                    => $newMigrations > 0 ? '<question>' . $newMigrations . '</question>' : 0
103 7
        ];
104 7
        foreach ($info as $name => $value) {
105 7
            $output->writeln('    <comment>>></comment> ' . $name . ': ' . str_repeat(' ', 50 - strlen($name)) . $value);
106 7
        }
107
108 7
        if ($input->getOption('show-versions')) {
109
            if ($migrations = $configuration->getMigrations()) {
110
                $output->writeln("\n <info>==</info> Available Migration Versions\n");
111
112
                $this->showVersions($migrations, $configuration, $output);
113
            }
114
115
            if ($numExecutedUnavailableMigrations) {
116
                $output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
117
                foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
118
                    $output->writeln('    <comment>>></comment> ' . $configuration->formatVersion($executedUnavailableMigration) .
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Migrations...ration::formatVersion() has been deprecated.

This method has been deprecated.

Loading history...
119
                        ' (<comment>' . $executedUnavailableMigration . '</comment>)');
120
                }
121
            }
122
        }
123 7
    }
124
125
    private function showVersions($migrations, Configuration $configuration, OutputInterface $output)
126
    {
127
        $migratedVersions = $configuration->getMigratedVersions();
128
129
        foreach($migrations as $version) {
130
            $isMigrated = in_array($version->getVersion(), $migratedVersions);
131
            $status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
132
            $migrationDescription = '';
133
            if ($version->getMigration()->getDescription()) {
134
                $migrationDescription = str_repeat(' ', 5) . $version->getMigration()->getDescription();
135
            }
136
            $formattedVersion = $configuration->getDateTime($version->getVersion());
137
138
            $output->writeln('    <comment>>></comment> ' . $formattedVersion .
139
                ' (<comment>' . $version->getVersion() . '</comment>)' .
140
                str_repeat(' ', 49 - strlen($formattedVersion) - strlen($version->getVersion()))  .
141
                $status  . $migrationDescription);
142
        }
143
    }
144
}
145