8
8
use Carbon \Carbon ;
9
9
use Illuminate \Support \Facades \App ;
10
10
use Illuminate \Support \Str ;
11
+ use Symfony \Component \Finder \SplFileInfo ;
11
12
12
13
class MigrationGenerator implements Generator
13
14
{
@@ -37,6 +38,8 @@ class MigrationGenerator implements Generator
37
38
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
38
39
private $ files ;
39
40
41
+ private $ output = [];
42
+
40
43
private $ hasForeignKeyConstraints = false ;
41
44
42
45
public function __construct ($ files )
@@ -46,8 +49,6 @@ public function __construct($files)
46
49
47
50
public function output (Tree $ tree , $ overwrite = false ): array
48
51
{
49
- $ output = [];
50
-
51
52
$ created_pivot_tables = [];
52
53
53
54
$ stub = $ this ->files ->stub ('migration.stub ' );
@@ -60,7 +61,7 @@ public function output(Tree $tree, $overwrite = false): array
60
61
$ action = $ this ->files ->exists ($ path ) ? 'updated ' : 'created ' ;
61
62
$ this ->files ->put ($ path , $ this ->populateStub ($ stub , $ model ));
62
63
63
- $ output [$ action ][] = $ path ;
64
+ $ this -> output [$ action ][] = $ path ;
64
65
65
66
if (! empty ($ model ->pivotTables ())) {
66
67
foreach ($ model ->pivotTables () as $ pivotSegments ) {
@@ -75,10 +76,10 @@ public function output(Tree $tree, $overwrite = false): array
75
76
$ action = $ this ->files ->exists ($ path ) ? 'updated ' : 'created ' ;
76
77
$ this ->files ->put ($ path , $ this ->populatePivotStub ($ stub , $ pivotSegments ));
77
78
$ created_pivot_tables [] = $ pivotTable ;
78
- $ output [$ action ][] = $ path ;
79
+ $ this -> output [$ action ][] = $ path ;
79
80
}
80
81
81
- return $ output ;
82
+ return $ this -> output ;
82
83
}
83
84
84
85
public function types (): array
@@ -324,11 +325,30 @@ protected function getTablePath($tableName, Carbon $timestamp, $overwrite = fals
324
325
$ dir = 'database/migrations/ ' ;
325
326
$ name = '_create_ ' .$ tableName .'_table.php ' ;
326
327
327
- $ file = $ overwrite ? collect ($ this ->files ->files ($ dir ))->first (function ($ file ) use ($ tableName ) {
328
- return str_contains ($ file , $ tableName );
329
- }) : false ;
328
+ if ($ overwrite ) {
329
+ $ migrations = collect ($ this ->files ->files ($ dir ))
330
+ ->filter (function (SplFileInfo $ file ) use ($ name ) {
331
+ return str_contains ($ file ->getFilename (), $ name );
332
+ })
333
+ ->sort ();
334
+
335
+ if ($ migrations ->isNotEmpty ()) {
336
+ $ migration = $ migrations ->first ()->getPathname ();
337
+
338
+ $ migrations ->diff ($ migration )
339
+ ->each (function (SplFileInfo $ file ) {
340
+ $ path = $ file ->getPathname ();
341
+
342
+ $ this ->files ->delete ($ path );
343
+
344
+ $ this ->output ['deleted ' ][] = $ path ;
345
+ });
346
+
347
+ return $ migration ;
348
+ }
349
+ }
330
350
331
- return $ file ? ( string ) $ file : $ dir .$ timestamp ->format ('Y_m_d_His ' ).$ name ;
351
+ return $ dir .$ timestamp ->format ('Y_m_d_His ' ).$ name ;
332
352
}
333
353
334
354
protected function isLaravel7orNewer ()
0 commit comments