Completed
Pull Request — master (#2)
by Joao
03:57
created

InstallCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jg
5
 * Date: 17/06/16
6
 * Time: 21:52
7
 */
8
9
namespace ByJG\DbMigration\Console;
10
11
use ByJG\DbMigration\Exception\DatabaseNotVersionedException;
12
use ByJG\DbMigration\Exception\OldVersionSchemaException;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class InstallCommand extends ConsoleCommand
17
{
18
    protected function configure()
19
    {
20
        parent::configure(); 
21
        $this
22
            ->setName('install')
23
            ->setDescription('Install or upgrade the migrate version in a existing database');
24
25
    }
26
27
    protected function execute(InputInterface $input, OutputInterface $output)
28
    {
29
        try {
30
            parent::execute($input, $output);
31
32
            $action = 'Database is already versioned. ';
33
            try {
34
                $this->migration->getCurrentVersion();
35
            } catch (DatabaseNotVersionedException $ex) {
36
                $action = 'Created the version table';
37
                $this->migration->createVersion();
38
            } catch (OldVersionSchemaException $ex) {
39
                $action = 'Updated the version table';
40
                $this->migration->updateTableVersion();
41
            }
42
43
            $version = $this->migration->getCurrentVersion();
44
            $output->writeln($action);
45
            $output->writeln('current version: ' . $version['version']);
46
            $output->writeln('current status.: ' . $version['status']);
47
        } catch (\Exception $ex) {
48
            $this->handleError($ex, $output);
49
        }
50
    }
51
}
52