Skip to content

Commit 71a4f57

Browse files
author
Nathan Esayeas
authored
Remove all duplicates when overwriting migrations (#375)
1 parent de322c2 commit 71a4f57

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Carbon\Carbon;
99
use Illuminate\Support\Facades\App;
1010
use Illuminate\Support\Str;
11+
use Symfony\Component\Finder\SplFileInfo;
1112

1213
class MigrationGenerator implements Generator
1314
{
@@ -37,6 +38,8 @@ class MigrationGenerator implements Generator
3738
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
3839
private $files;
3940

41+
private $output = [];
42+
4043
private $hasForeignKeyConstraints = false;
4144

4245
public function __construct($files)
@@ -46,8 +49,6 @@ public function __construct($files)
4649

4750
public function output(Tree $tree, $overwrite = false): array
4851
{
49-
$output = [];
50-
5152
$created_pivot_tables = [];
5253

5354
$stub = $this->files->stub('migration.stub');
@@ -60,7 +61,7 @@ public function output(Tree $tree, $overwrite = false): array
6061
$action = $this->files->exists($path) ? 'updated' : 'created';
6162
$this->files->put($path, $this->populateStub($stub, $model));
6263

63-
$output[$action][] = $path;
64+
$this->output[$action][] = $path;
6465

6566
if (! empty($model->pivotTables())) {
6667
foreach ($model->pivotTables() as $pivotSegments) {
@@ -75,10 +76,10 @@ public function output(Tree $tree, $overwrite = false): array
7576
$action = $this->files->exists($path) ? 'updated' : 'created';
7677
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
7778
$created_pivot_tables[] = $pivotTable;
78-
$output[$action][] = $path;
79+
$this->output[$action][] = $path;
7980
}
8081

81-
return $output;
82+
return $this->output;
8283
}
8384

8485
public function types(): array
@@ -324,11 +325,30 @@ protected function getTablePath($tableName, Carbon $timestamp, $overwrite = fals
324325
$dir = 'database/migrations/';
325326
$name = '_create_'.$tableName.'_table.php';
326327

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+
}
330350

331-
return $file ? (string) $file : $dir.$timestamp->format('Y_m_d_His').$name;
351+
return $dir.$timestamp->format('Y_m_d_His').$name;
332352
}
333353

334354
protected function isLaravel7orNewer()

0 commit comments

Comments
 (0)