|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\DbMigration\Console; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\ConnectionManagement; |
|
6
|
|
|
use ByJG\DbMigration\Migration; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
abstract class ConsoleCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
protected function configure() |
|
17
|
|
|
{ |
|
18
|
|
|
$this |
|
19
|
|
|
->addArgument( |
|
20
|
|
|
'connection', |
|
21
|
|
|
InputArgument::OPTIONAL, |
|
22
|
|
|
'The connection string. Ex. mysql://root:password@server/database', |
|
23
|
|
|
getenv('MIGRATE_CONNECTION') |
|
24
|
|
|
) |
|
25
|
|
|
->addOption( |
|
26
|
|
|
'path', |
|
27
|
|
|
'p', |
|
28
|
|
|
InputOption::VALUE_OPTIONAL, |
|
29
|
|
|
'Define the path where the base.sql resides. If not set assumes the current folder' |
|
30
|
|
|
) |
|
31
|
|
|
->addOption( |
|
32
|
|
|
'up-to', |
|
33
|
|
|
'u', |
|
34
|
|
|
InputOption::VALUE_OPTIONAL, |
|
35
|
|
|
'Run up to the specified version' |
|
36
|
|
|
) |
|
37
|
|
|
->addUsage('') |
|
38
|
|
|
->addUsage('Example: ') |
|
39
|
|
|
->addUsage(' migrate reset mysql://root:password@server/database') |
|
40
|
|
|
->addUsage(' migrate up mysql://root:password@server/database') |
|
41
|
|
|
->addUsage(' migrate down mysql://root:password@server/database') |
|
42
|
|
|
->addUsage(' migrate up --up-to=10 --path=/somepath mysql://root:password@server/database') |
|
43
|
|
|
->addUsage(' migrate down --up-to=3 --path=/somepath mysql://root:password@server/database') |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var Migration |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $migration; |
|
51
|
|
|
|
|
52
|
|
|
protected $upTo; |
|
53
|
|
|
|
|
54
|
|
|
protected $connection; |
|
55
|
|
|
|
|
56
|
|
|
protected $path; |
|
57
|
|
|
|
|
58
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->connection = $input->getArgument('connection'); |
|
61
|
|
|
if (!$this->connection) { |
|
62
|
|
|
throw new InvalidArgumentException('You need to setup the connection in the argument or setting the environment MIGRATE_CONNECTION'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->path = $input->getOption('path'); |
|
66
|
|
|
if (!$this->path) { |
|
67
|
|
|
$this->path = "."; |
|
68
|
|
|
} |
|
69
|
|
|
$this->path = realpath($this->path); |
|
70
|
|
|
|
|
71
|
|
|
$this->upTo = $input->getOption('up-to'); |
|
72
|
|
|
|
|
73
|
|
|
$connectionObject = new ConnectionManagement($this->connection); |
|
74
|
|
|
$this->migration = new Migration($connectionObject, $this->path); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
|
80
|
|
|
$output->writeln('Connection String: ' . $this->connection); |
|
81
|
|
|
$output->writeln('Path: ' . $this->path); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
|
85
|
|
|
$this->migration->addCallbackProgress(function($command, $version) use ($output) { |
|
86
|
|
|
$output->writeln('Doing: ' . $command . " to " . $version); |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|