-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathMigrator.php
More file actions
327 lines (274 loc) · 7.25 KB
/
Migrator.php
File metadata and controls
327 lines (274 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace Nwidart\Modules\Migrations;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Nwidart\Modules\Module;
use Nwidart\Modules\Support\Config\GenerateConfigReader;
class Migrator
{
/**
* Module instance.
*
* @var Module
*/
protected $module;
/**
* Laravel Application instance.
*
* @var Application.
*/
protected $laravel;
/**
* Optional subpath for specific migration file.
*
* @var string|null
*
* @example subpath 2000_01_01_000000_create_example_table.php
*/
protected $subpath = '';
/**
* The database connection to be used
*
* @var string
*/
protected $database = '';
/**
* Create new instance.
*
* @param string|null $subpath
*/
public function __construct(Module $module, Application $application, $subpath = null)
{
$this->module = $module;
$this->laravel = $application;
$this->subpath = $subpath;
}
/**
* Set the database connection to be used
*
*
* @return $this
*/
public function setDatabase($database)
{
if (is_string($database) && $database) {
$this->database = $database;
}
return $this;
}
/**
* @return Module
*/
public function getModule()
{
return $this->module;
}
/**
* Get migration path.
*
* @return string
*/
public function getPath()
{
$config = $this->module->get('migration');
$migrationPath = GenerateConfigReader::read('migration');
$path = (is_array($config) && array_key_exists('path', $config)) ? $config['path'] : $migrationPath->getPath();
return $this->module->getExtraPath($path);
}
/**
* Get migration files.
*
* @param bool $reverse
* @return array
*/
public function getMigrations($reverse = false)
{
if (! empty($this->subpath)) {
$files = $this->laravel['files']->glob($this->getPath().'/'.$this->subpath);
} else {
$files = $this->laravel['files']->glob($this->getPath().'/*_*.php');
}
// Once we have the array of files in the directory we will just remove the
// extension and take the basename of the file which is all we need when
// finding the migrations that haven't been run against the databases.
if ($files === false) {
return [];
}
$files = array_map(function ($file) {
return str_replace('.php', '', basename($file));
}, $files);
// Once we have all of the formatted file names we will sort them and since
// they all start with a timestamp this should give us the migrations in
// the order they were actually created by the application developers.
sort($files);
if ($reverse) {
return array_reverse($files);
}
return $files;
}
/**
* Rollback migration.
*
* @return array
*/
public function rollback()
{
$migrations = $this->getLast($this->getMigrations(true));
$this->requireFiles($migrations->toArray());
$migrated = [];
foreach ($migrations as $migration) {
$data = $this->find($migration);
if ($data->count()) {
$migrated[] = $migration;
$this->down($migration);
$data->delete();
}
}
return $migrated;
}
/**
* Reset migration.
*
* @return array
*/
public function reset()
{
$migrations = $this->getMigrations(true);
$this->requireFiles($migrations);
$migrated = [];
foreach ($migrations as $migration) {
$data = $this->find($migration);
if ($data->count()) {
$migrated[] = $migration;
$this->down($migration);
$data->delete();
}
}
return $migrated;
}
/**
* Run down schema from the given migration name.
*
* @param string $migration
*/
public function down($migration)
{
$this->resolve($migration)->down();
}
/**
* Run up schema from the given migration name.
*
* @param string $migration
*/
public function up($migration)
{
$this->resolve($migration)->up();
}
/**
* Resolve a migration instance from a file.
*
* @param string $file
* @return object
*/
public function resolve($file)
{
$name = implode('_', array_slice(explode('_', $file), 4));
$class = Str::studly($name);
if (! class_exists($class) && file_exists($this->getPath().'/'.$file.'.php')) {
return include $this->getPath().'/'.$file.'.php';
}
return new $class;
}
/**
* Require in all the migration files in a given path.
*/
public function requireFiles(array $files)
{
$path = $this->getPath();
foreach ($files as $file) {
$this->laravel['files']->requireOnce($path.'/'.$file.'.php');
}
}
/**
* Get table instance.
*
* @return Builder
*/
public function table()
{
return $this->laravel['db']->connection($this->database ?: null)->table(config('database.migrations.table', 'migrations'));
}
/**
* Find migration data from database by given migration name.
*
* @param string $migration
* @return object
*/
public function find($migration)
{
return $this->table()->whereMigration($migration);
}
/**
* Save new migration to database.
*
* @param string $migration
* @return mixed
*/
public function log($migration)
{
return $this->table()->insert([
'migration' => $migration,
'batch' => $this->getNextBatchNumber(),
]);
}
/**
* Get the next migration batch number.
*
* @return int
*/
public function getNextBatchNumber()
{
return $this->getLastBatchNumber() + 1;
}
/**
* Get the last migration batch number.
*
* @param array|null $migrations
* @return int
*/
public function getLastBatchNumber($migrations = null)
{
$table = $this->table();
if (is_array($migrations)) {
$table = $table->whereIn('migration', $migrations);
}
return $table->max('batch');
}
/**
* Get the last migration batch.
*
* @param array $migrations
* @return Collection
*/
public function getLast($migrations)
{
$query = $this->table()
->where('batch', $this->getLastBatchNumber($migrations))
->whereIn('migration', $migrations);
$result = $query->orderBy('migration', 'desc')->get();
return collect($result)->map(function ($item) {
return (array) $item;
})->pluck('migration');
}
/**
* Get the ran migrations.
*
* @return Collection
*/
public function getRan()
{
return $this->table()->pluck('migration');
}
}