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
|
|
|
->addOption( |
42
|
|
|
'no-base', |
43
|
|
|
null, |
44
|
|
|
InputOption::VALUE_NONE, |
45
|
|
|
'Remove the check for base.sql file' |
46
|
|
|
) |
47
|
|
|
->addUsage('') |
48
|
|
|
->addUsage('Example: ') |
49
|
|
|
->addUsage(' migrate reset mysql://root:password@server/database') |
50
|
|
|
->addUsage(' migrate up mysql://root:password@server/database') |
51
|
|
|
->addUsage(' migrate down mysql://root:password@server/database') |
52
|
|
|
->addUsage(' migrate up --up-to=10 --path=/somepath mysql://root:password@server/database') |
53
|
|
|
->addUsage(' migrate down --up-to=3 --path=/somepath mysql://root:password@server/database') |
54
|
|
|
; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Migration |
59
|
|
|
*/ |
60
|
|
|
protected $migration; |
61
|
|
|
|
62
|
|
|
protected $upTo; |
63
|
|
|
|
64
|
|
|
protected $connection; |
65
|
|
|
|
66
|
|
|
protected $path; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
70
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
71
|
|
|
* @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
72
|
|
|
*/ |
73
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$this->connection = $input->getArgument('connection'); |
76
|
|
|
if (!$this->connection) { |
77
|
|
|
throw new InvalidArgumentException( |
78
|
|
|
'You need to setup the connection in the argument or setting the environment MIGRATE_CONNECTION' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->path = $input->getOption('path'); |
83
|
|
|
if (!$this->path) { |
84
|
|
|
$this->path = (!empty(getenv('MIGRATE_PATH')) ? getenv('MIGRATE_PATH') : "."); |
85
|
|
|
} |
86
|
|
|
$this->path = realpath($this->path); |
87
|
|
|
|
88
|
|
|
$this->upTo = $input->getOption('up-to'); |
89
|
|
|
|
90
|
|
|
$requiredBase = !$input->getOption('no-base'); |
91
|
|
|
|
92
|
|
|
$uri = new Uri($this->connection); |
93
|
|
|
$this->migration = new Migration($uri, $this->path, $requiredBase); |
94
|
|
|
$this->migration |
95
|
|
|
->registerDatabase('sqlite', SqliteDatabase::class) |
96
|
|
|
->registerDatabase('mysql', MySqlDatabase::class) |
97
|
|
|
->registerDatabase('pgsql', PgsqlDatabase::class) |
98
|
|
|
->registerDatabase('dblib', DblibDatabase::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
102
|
|
|
{ |
103
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
104
|
|
|
$output->writeln('Connection String: ' . $this->connection); |
105
|
|
|
$output->writeln('Path: ' . $this->path); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { |
109
|
|
|
$this->migration->addCallbackProgress(function ($command, $version) use ($output) { |
110
|
|
|
$output->writeln('Doing: ' . $command . " to " . $version); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param \Exception|\Error $exception |
117
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
118
|
|
|
*/ |
119
|
|
|
protected function handleError($exception, OutputInterface $output) |
120
|
|
|
{ |
121
|
|
|
$output->writeln('-- Error migrating tables --'); |
122
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
123
|
|
|
$output->writeln(get_class($exception)); |
124
|
|
|
$output->writeln($exception->getMessage()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|