Completed
Pull Request — master (#7)
by Joao
06:50
created

ConsoleCommand::initialize()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
eloc 17
nc 4
nop 2
1
<?php
2
3
namespace ByJG\DbMigration\Console;
4
5
use ByJG\DbMigration\Database\DblibDatabase;
6
use ByJG\DbMigration\Database\MySqlDatabase;
7
use ByJG\DbMigration\Database\PgsqlDatabase;
8
use ByJG\DbMigration\Database\SqliteDatabase;
9
use ByJG\DbMigration\Migration;
10
use ByJG\Util\Uri;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Exception\InvalidArgumentException;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
abstract class ConsoleCommand extends Command
19
{
20
    protected function configure()
21
    {
22
        $this
23
            ->addArgument(
24
                'connection',
25
                InputArgument::OPTIONAL,
26
                'The connection string. Ex. mysql://root:password@server/database',
27
                getenv('MIGRATE_CONNECTION')
28
            )
29
            ->addOption(
30
                'path',
31
                'p',
32
                InputOption::VALUE_OPTIONAL,
33
                'Define the path where the base.sql resides. If not set assumes the current folder'
34
            )
35
            ->addOption(
36
                'up-to',
37
                'u',
38
                InputOption::VALUE_OPTIONAL,
39
                'Run up to the specified version'
40
            )
41
            ->addUsage('')
42
            ->addUsage('Example: ')
43
            ->addUsage('   migrate reset mysql://root:password@server/database')
44
            ->addUsage('   migrate up mysql://root:password@server/database')
45
            ->addUsage('   migrate down mysql://root:password@server/database')
46
            ->addUsage('   migrate up --up-to=10 --path=/somepath mysql://root:password@server/database')
47
            ->addUsage('   migrate down --up-to=3 --path=/somepath mysql://root:password@server/database')
48
        ;
49
    }
50
51
    /**
52
     * @var Migration
53
     */
54
    protected $migration;
55
56
    protected $upTo;
57
58
    protected $connection;
59
60
    protected $path;
61
62
    /**
63
     * @param \Symfony\Component\Console\Input\InputInterface $input
64
     * @param \Symfony\Component\Console\Output\OutputInterface $output
65
     * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile
66
     */
67
    protected function initialize(InputInterface $input, OutputInterface $output)
68
    {
69
        $this->connection = $input->getArgument('connection');
70
        if (!$this->connection) {
71
            throw new InvalidArgumentException(
72
                'You need to setup the connection in the argument or setting the environment MIGRATE_CONNECTION'
73
            );
74
        }
75
76
        $this->path = $input->getOption('path');
77
        if (!$this->path) {
78
            $this->path = (!empty(getenv('MIGRATE_PATH')) ? getenv('MIGRATE_PATH') : ".");
79
        }
80
        $this->path = realpath($this->path);
81
82
        $this->upTo = $input->getOption('up-to');
83
84
        $uri = new Uri($this->connection);
85
        $this->migration = new Migration($uri, $this->path);
86
        $this->migration
87
            ->registerDatabase('sqlite', SqliteDatabase::class)
88
            ->registerDatabase('mysql', MySqlDatabase::class)
89
            ->registerDatabase('pgsql', PgsqlDatabase::class)
90
            ->registerDatabase('dblib', DblibDatabase::class);
91
    }
92
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
96
            $output->writeln('Connection String: ' . $this->connection);
97
            $output->writeln('Path: ' . $this->path);
98
        }
99
100
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
101
            $this->migration->addCallbackProgress(function ($command, $version) use ($output) {
102
                $output->writeln('Doing: ' . $command . " to " . $version);
103
            });
104
        }
105
    }
106
107
    /**
108
     * @param \Exception|\Error $exception
109
     * @param \Symfony\Component\Console\Output\OutputInterface $output
110
     */
111
    protected function handleError($exception, OutputInterface $output)
112
    {
113
        $output->writeln('-- Error migrating tables --');
114
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
115
            $output->writeln(get_class($exception));
116
            $output->writeln($exception->getMessage());
117
        }
118
    }
119
}
120