|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yarak\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Yarak\Config\Config; |
|
6
|
|
|
use Yarak\Migrations\Migrator; |
|
7
|
|
|
use Yarak\DB\ConnectionResolver; |
|
8
|
|
|
use Yarak\DB\Seeders\SeedRunner; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class Migrate extends YarakCommand |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Configure the command. |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->setName('migrate') |
|
21
|
|
|
->setDescription('Run the database migrations.') |
|
22
|
|
|
->setHelp('This command allows you to run migrations.') |
|
23
|
|
|
->addRollback() |
|
24
|
|
|
->addSteps() |
|
25
|
|
|
->addReset() |
|
26
|
|
|
->addRefresh() |
|
27
|
|
|
->addSeed() |
|
28
|
|
|
->addSeedClass(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Add rollback option. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function addRollback() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->addOption( |
|
37
|
|
|
'rollback', |
|
38
|
|
|
null, |
|
39
|
|
|
InputOption::VALUE_NONE, |
|
40
|
|
|
'Rollback migrations by given number of steps.' |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add steps option. |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function addSteps() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->addOption( |
|
50
|
|
|
'steps', |
|
51
|
|
|
null, |
|
52
|
|
|
InputOption::VALUE_OPTIONAL, |
|
53
|
|
|
'Number of steps to rollback.', |
|
54
|
|
|
1 |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Add reset option. |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function addReset() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->addOption( |
|
64
|
|
|
'reset', |
|
65
|
|
|
null, |
|
66
|
|
|
InputOption::VALUE_NONE, |
|
67
|
|
|
'Rollback all migrations.' |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add refresh option. |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function addRefresh() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->addOption( |
|
77
|
|
|
'refresh', |
|
78
|
|
|
null, |
|
79
|
|
|
InputOption::VALUE_NONE, |
|
80
|
|
|
'Rollback and re-run all migrations.' |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Add seed option. |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function addSeed() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->addOption( |
|
90
|
|
|
'seed', |
|
91
|
|
|
null, |
|
92
|
|
|
InputOption::VALUE_NONE, |
|
93
|
|
|
'Seed the database after refreshing.' |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function addSeedClass() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->addOption( |
|
100
|
|
|
'class', |
|
101
|
|
|
null, |
|
102
|
|
|
InputOption::VALUE_OPTIONAL, |
|
103
|
|
|
'The name of the seeder class to run.', |
|
104
|
|
|
'DatabaseSeeder' |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Execute the command. |
|
110
|
|
|
* |
|
111
|
|
|
* @param InputInterface $input |
|
112
|
|
|
* @param OutputInterface $output |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
115
|
|
|
{ |
|
116
|
|
|
$migrator = $this->getMigrator(); |
|
117
|
|
|
|
|
118
|
|
|
if ($input->getOption('rollback')) { |
|
119
|
|
|
$migrator->rollback($input->getOption('steps')); |
|
120
|
|
|
} elseif ($input->getOption('reset')) { |
|
121
|
|
|
$migrator->reset(); |
|
122
|
|
|
} elseif ($input->getOption('refresh')) { |
|
123
|
|
|
$this->preformRefresh($migrator, $input, $output); |
|
124
|
|
|
} else { |
|
125
|
|
|
$migrator->run(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
foreach ($migrator->getLog() as $message) { |
|
129
|
|
|
$output->writeln($message); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get an instance of the migrator. |
|
135
|
|
|
* |
|
136
|
|
|
* @return Migrator |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function getMigrator() |
|
139
|
|
|
{ |
|
140
|
|
|
$config = Config::getInstance($this->configArray); |
|
141
|
|
|
|
|
142
|
|
|
$migratorClassName = $this->getMigratorClassName($config); |
|
143
|
|
|
|
|
144
|
|
|
return new $migratorClassName( |
|
145
|
|
|
$config, |
|
146
|
|
|
new ConnectionResolver(), |
|
147
|
|
|
$this->getRepository($config) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the name of the migrator class. |
|
153
|
|
|
* |
|
154
|
|
|
* @param Config $config |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getMigratorClassName(Config $config) |
|
159
|
|
|
{ |
|
160
|
|
|
$migratorType = ucfirst($config->get('migratorType')); |
|
161
|
|
|
|
|
162
|
|
|
return "Yarak\\Migrations\\$migratorType\\". |
|
163
|
|
|
$migratorType.'Migrator'; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get an instance of MigrationRepository. |
|
168
|
|
|
* |
|
169
|
|
|
* @param Config $config |
|
170
|
|
|
* |
|
171
|
|
|
* @return Yarak\Migrations\MigrationRepository |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getRepository(Config $config) |
|
174
|
|
|
{ |
|
175
|
|
|
$repositoryType = ucfirst($config->get('migrationRepository')); |
|
176
|
|
|
|
|
177
|
|
|
$repositoryClass = 'Yarak\\Migrations\\Repositories\\'. |
|
178
|
|
|
$repositoryType.'MigrationRepository'; |
|
179
|
|
|
|
|
180
|
|
|
return new $repositoryClass(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Perform the database refresh. |
|
185
|
|
|
* |
|
186
|
|
|
* @param Migrator $migrator |
|
187
|
|
|
* @param InputInterface $input |
|
188
|
|
|
* @param OutputInterface $output |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function preformRefresh( |
|
191
|
|
|
Migrator $migrator, |
|
192
|
|
|
InputInterface $input, |
|
193
|
|
|
OutputInterface $output |
|
194
|
|
|
) { |
|
195
|
|
|
$migrator->refresh(); |
|
196
|
|
|
|
|
197
|
|
|
if ($input->getOption('seed')) { |
|
198
|
|
|
$seedRunner = new SeedRunner(); |
|
199
|
|
|
|
|
200
|
|
|
$seedRunner->run($input->getOption('class')); |
|
201
|
|
|
|
|
202
|
|
|
foreach ($seedRunner->getLog() as $message) { |
|
203
|
|
|
$output->writeln($message); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|