1
|
|
|
<?php namespace Anomaly\Streams\Platform\Database\Migration; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Addon; |
4
|
|
|
use Anomaly\Streams\Platform\Database\Migration\Command\Migrate; |
5
|
|
|
use Anomaly\Streams\Platform\Database\Migration\Command\Reset; |
6
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Migrator |
10
|
|
|
* |
11
|
|
|
* @link http://pyrocms.com/ |
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
13
|
|
|
* @author Ryan Thompson <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Migrator extends \Illuminate\Database\Migrations\Migrator |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
use DispatchesJobs; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The addon instance. |
22
|
|
|
* |
23
|
|
|
* @var Addon |
24
|
|
|
*/ |
25
|
|
|
protected $addon = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The migration repository. |
29
|
|
|
* |
30
|
|
|
* @var MigrationRepository |
31
|
|
|
*/ |
32
|
|
|
protected $repository; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Run the migrations. |
36
|
|
|
* |
37
|
|
|
* @param array $paths |
38
|
|
|
* @param array $options |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function run($paths = [], array $options = []) |
42
|
|
|
{ |
43
|
|
|
$this->repository->setMigrator($this); |
44
|
|
|
|
45
|
|
|
return parent::run($paths, $options); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Rolls all of the currently applied migrations back. |
50
|
|
|
* |
51
|
|
|
* This is a carbon copy of the Laravel method |
52
|
|
|
* except in the "!isset($files[$migration])" part. |
53
|
|
|
* |
54
|
|
|
* @param array|string $paths |
55
|
|
|
* @param bool $pretend |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function reset($paths = [], $pretend = false) |
59
|
|
|
{ |
60
|
|
|
$this->repository->setMigrator($this); |
61
|
|
|
|
62
|
|
|
$this->notes = []; |
63
|
|
|
|
64
|
|
|
$rolledBack = []; |
65
|
|
|
|
66
|
|
|
$files = $this->getMigrationFiles($paths); |
67
|
|
|
|
68
|
|
|
// Next, we will reverse the migration list so we can run them back in the |
69
|
|
|
// correct order for resetting this database. This will allow us to get |
70
|
|
|
// the database back into its "empty" state ready for the migrations. |
71
|
|
|
$migrations = array_reverse($this->repository->getRan()); |
72
|
|
|
|
73
|
|
|
$count = count($migrations); |
74
|
|
|
|
75
|
|
|
if ($count === 0) { |
76
|
|
|
$this->note('<info>Nothing to rollback.</info>'); |
77
|
|
|
} else { |
78
|
|
|
$this->requireFiles($files); |
79
|
|
|
|
80
|
|
|
// Next we will run through all of the migrations and call the "down" method |
81
|
|
|
// which will reverse each migration in order. This will get the database |
82
|
|
|
// back to its original "empty" state and will be ready for migrations. |
83
|
|
|
foreach ($migrations as $migration) { |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* This is the only adjustment to |
87
|
|
|
* Laravel's method.. |
88
|
|
|
*/ |
89
|
|
|
if (!isset($files[$migration])) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$rolledBack[] = $files[$migration]; |
94
|
|
|
|
95
|
|
|
$this->runDown($files[$migration], (object)['migration' => $migration], $pretend); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $rolledBack; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Rollback the last migration operation. |
104
|
|
|
* |
105
|
|
|
* @param array|string $paths |
106
|
|
|
* @param array $options |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function rollback($paths = [], array $options = []) |
110
|
|
|
{ |
111
|
|
|
$this->repository->setMigrator($this); |
112
|
|
|
|
113
|
|
|
return parent::rollback($paths, $options); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Run "up" a migration instance. |
118
|
|
|
* |
119
|
|
|
* @param string $file |
120
|
|
|
* @param int $batch |
121
|
|
|
* @param bool $pretend |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
protected function runUp($file, $batch, $pretend) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
/** |
127
|
|
|
* Run our migrations first. |
128
|
|
|
* |
129
|
|
|
* @var Migration $migration |
130
|
|
|
*/ |
131
|
|
|
$migration = $this->resolve($file); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the addon if there is |
135
|
|
|
* one contextually available. |
136
|
|
|
* |
137
|
|
|
* @var Addon $addon |
138
|
|
|
*/ |
139
|
|
|
if ($addon = $this->getAddon()) { |
140
|
|
|
$migration->setAddon($addon); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($migration instanceof Migration) { |
144
|
|
|
$this->dispatch(new Migrate($migration)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
parent::runUp($file, $batch, $pretend); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Run "down" a migration instance. |
152
|
|
|
* |
153
|
|
|
* @param string $file |
154
|
|
|
* @param object $migration |
155
|
|
|
* @param bool $pretend |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
protected function runDown($file, $migration, $pretend) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
/** |
161
|
|
|
* Run our migrations first. |
162
|
|
|
* |
163
|
|
|
* @var Migration $migration |
164
|
|
|
*/ |
165
|
|
|
$migration = $this->resolve($file); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the addon if there is |
169
|
|
|
* one contextually available. |
170
|
|
|
* |
171
|
|
|
* @var Addon $addon |
172
|
|
|
*/ |
173
|
|
|
if ($addon = $this->getAddon()) { |
174
|
|
|
$migration->setAddon($addon); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($migration instanceof Migration) { |
178
|
|
|
$this->dispatch(new Reset($migration)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
parent::runDown($file, $migration, $pretend); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Resolve a migration instance from a file. |
186
|
|
|
* |
187
|
|
|
* @param string $file |
188
|
|
|
* @return object |
189
|
|
|
*/ |
190
|
|
|
public function resolve($file) |
191
|
|
|
{ |
192
|
|
|
$migration = app((new MigrationName($file))->className()); |
193
|
|
|
|
194
|
|
|
$migration->migration = (new MigrationName($file))->migration(); |
195
|
|
|
|
196
|
|
|
return $migration; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set the addon. |
201
|
|
|
* |
202
|
|
|
* @param Addon $addon |
203
|
|
|
*/ |
204
|
|
|
public function setAddon(Addon $addon) |
205
|
|
|
{ |
206
|
|
|
$this->addon = $addon; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Clear the addon. |
213
|
|
|
* |
214
|
|
|
* @param Addon $addon |
|
|
|
|
215
|
|
|
*/ |
216
|
|
|
public function clearAddon() |
217
|
|
|
{ |
218
|
|
|
$this->addon = null; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get the addon. |
225
|
|
|
* |
226
|
|
|
* @return Addon |
227
|
|
|
*/ |
228
|
|
|
public function getAddon() |
229
|
|
|
{ |
230
|
|
|
return $this->addon; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.