Skip to content

Commit a2cde81

Browse files
authored
Migrations with fixed timestamp (#319)
1 parent a4311c6 commit a2cde81

File tree

8 files changed

+161
-33
lines changed

8 files changed

+161
-33
lines changed

src/Blueprint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ public function analyze(array $tokens)
7070
return new Tree($registry);
7171
}
7272

73-
public function generate(Tree $tree, array $only = [], array $skip = []): array
73+
public function generate(Tree $tree, array $only = [], array $skip = [], $overwriteMigrations = false): array
7474
{
7575
$components = [];
7676

7777
foreach ($this->generators as $generator) {
7878
if ($this->shouldGenerate($generator->types(), $only, $skip)) {
79-
$components = array_merge_recursive($components, $generator->output($tree));
79+
$components = array_merge_recursive($components, $generator->output($tree, $overwriteMigrations));
8080
}
8181
}
8282

src/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Builder
88
{
9-
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '')
9+
public function execute(Blueprint $blueprint, Filesystem $files, string $draft, string $only = '', string $skip = '', $overwriteMigrations = false)
1010
{
1111
$cache = [];
1212
if ($files->exists('.blueprint')) {
@@ -20,7 +20,7 @@ public function execute(Blueprint $blueprint, Filesystem $files, string $draft,
2020
$only = array_filter(explode(',', $only));
2121
$skip = array_filter(explode(',', $skip));
2222

23-
$generated = $blueprint->generate($registry, $only, $skip);
23+
$generated = $blueprint->generate($registry, $only, $skip, $overwriteMigrations);
2424

2525
$models = array_merge($tokens['cache'], $tokens['models'] ?? []);
2626

src/Commands/BuildCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BuildCommand extends Command
2121
{draft? : The path to the draft file, default: draft.yaml or draft.yml }
2222
{--only= : Comma separated list of file classes to generate, skipping the rest }
2323
{--skip= : Comma separated list of file classes to skip, generating the rest }
24+
{--overwrite-migrations : Update existing migration files, if found }
2425
';
2526

2627
/**
@@ -59,9 +60,10 @@ public function handle()
5960

6061
$only = $this->option('only') ?: '';
6162
$skip = $this->option('skip') ?: '';
63+
$overwriteMigrations = $this->option('overwrite-migrations') ?: false;
6264

6365
$blueprint = resolve(Blueprint::class);
64-
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip);
66+
$generated = $this->builder->execute($blueprint, $this->files, $file, $only, $skip, $overwriteMigrations);
6567

6668
collect($generated)->each(function ($files, $action) {
6769
$this->line(Str::studly($action).':', $this->outputStyle($action));

src/Generators/MigrationGenerator.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct($files)
4242
$this->files = $files;
4343
}
4444

45-
public function output(Tree $tree): array
45+
public function output(Tree $tree, $overwrite = false): array
4646
{
4747
$output = [];
4848

@@ -54,10 +54,11 @@ public function output(Tree $tree): array
5454

5555
/** @var \Blueprint\Models\Model $model */
5656
foreach ($tree->models() as $model) {
57-
$path = $this->getPath($model, $sequential_timestamp->addSecond());
57+
$path = $this->getPath($model, $sequential_timestamp->addSecond(), $overwrite);
58+
$action = $this->files->exists($path) ? 'updated' : 'created';
5859
$this->files->put($path, $this->populateStub($stub, $model));
5960

60-
$output['created'][] = $path;
61+
$output[$action][] = $path;
6162

6263
if (! empty($model->pivotTables())) {
6364
foreach ($model->pivotTables() as $pivotSegments) {
@@ -68,10 +69,11 @@ public function output(Tree $tree): array
6869
}
6970

7071
foreach ($created_pivot_tables as $pivotTable => $pivotSegments) {
71-
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp);
72+
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp, $overwrite);
73+
$action = $this->files->exists($path) ? 'updated' : 'created';
7274
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
7375
$created_pivot_tables[] = $pivotTable;
74-
$output['created'][] = $path;
76+
$output[$action][] = $path;
7577
}
7678

7779
return $output;
@@ -276,14 +278,26 @@ protected function getClassName(Model $model)
276278
return 'Create'.Str::studly($model->tableName()).'Table';
277279
}
278280

279-
protected function getPath(Model $model, Carbon $timestamp)
281+
protected function getPath(Model $model, Carbon $timestamp, $overwrite = false)
280282
{
281-
return 'database/migrations/'.$timestamp->format('Y_m_d_His').'_create_'.$model->tableName().'_table.php';
283+
return $this->getTablePath($model->tableName(), $timestamp, $overwrite);
282284
}
283285

284-
protected function getPivotTablePath($tableName, Carbon $timestamp)
286+
protected function getPivotTablePath($tableName, Carbon $timestamp, $overwrite = false)
285287
{
286-
return 'database/migrations/'.$timestamp->format('Y_m_d_His').'_create_'.$tableName.'_table.php';
288+
return $this->getTablePath($tableName, $timestamp, $overwrite);
289+
}
290+
291+
protected function getTablePath($tableName, Carbon $timestamp, $overwrite = false)
292+
{
293+
$dir = 'database/migrations/';
294+
$name = '_create_'.$tableName.'_table.php';
295+
296+
$file = $overwrite ? collect($this->files->files($dir))->first(function ($file) use ($tableName) {
297+
return str_contains($file, $tableName);
298+
}) : false;
299+
300+
return $file ? (string) $file : $dir.$timestamp->format('Y_m_d_His').$name;
287301
}
288302

289303
protected function isLaravel7orNewer()

tests/Feature/BlueprintTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public function generate_uses_registered_generators_and_returns_generated_files(
379379
$tree = new Tree(['branch' => ['code', 'attributes']]);
380380

381381
$generatorOne->expects('output')
382-
->with($tree)
382+
->with($tree, false)
383383
->andReturn([
384384
'created' => ['one/new.php'],
385385
'updated' => ['one/existing.php'],
@@ -394,7 +394,7 @@ public function generate_uses_registered_generators_and_returns_generated_files(
394394

395395
$generatorTwo = \Mockery::mock(Generator::class);
396396
$generatorTwo->expects('output')
397-
->with($tree)
397+
->with($tree, false)
398398
->andReturn([
399399
'created' => ['two/new.php'],
400400
'updated' => ['two/existing.php'],
@@ -429,7 +429,7 @@ public function generate_uses_swapped_generator_and_returns_generated_files()
429429

430430
$generatorSwap = \Mockery::mock(Generator::class);
431431
$generatorSwap->expects('output')
432-
->with($tree)
432+
->with($tree, false)
433433
->andReturn([
434434
'created' => ['swapped/new.php'],
435435
'updated' => ['swapped/existing.php'],
@@ -464,7 +464,7 @@ public function generate_only_one_specific_type()
464464
$skip = [];
465465

466466
$generatorFoo->shouldReceive('output')
467-
->with($tree)
467+
->with($tree, false)
468468
->andReturn([
469469
'created' => ['foo.php'],
470470
]);
@@ -474,7 +474,7 @@ public function generate_only_one_specific_type()
474474

475475
$generatorBar = \Mockery::mock(Generator::class);
476476
$generatorBar->shouldReceive('output')
477-
->with($tree)
477+
->with($tree, false)
478478
->andReturn([
479479
'created' => ['bar.php'],
480480
]);
@@ -484,7 +484,7 @@ public function generate_only_one_specific_type()
484484

485485
$generatorBaz = \Mockery::mock(Generator::class);
486486
$generatorBaz->shouldReceive('output')
487-
->with($tree)
487+
->with($tree, false)
488488
->andReturn([
489489
'created' => ['baz.php'],
490490
]);
@@ -515,7 +515,7 @@ public function generate_only_specific_types()
515515
$skip = [];
516516

517517
$generatorFoo->shouldReceive('output')
518-
->with($tree)
518+
->with($tree, false)
519519
->andReturn([
520520
'created' => ['foo.php'],
521521
]);
@@ -525,7 +525,7 @@ public function generate_only_specific_types()
525525

526526
$generatorBar = \Mockery::mock(Generator::class);
527527
$generatorBar->shouldReceive('output')
528-
->with($tree)
528+
->with($tree, false)
529529
->andReturn([
530530
'created' => ['bar.php'],
531531
]);
@@ -535,7 +535,7 @@ public function generate_only_specific_types()
535535

536536
$generatorBaz = \Mockery::mock(Generator::class);
537537
$generatorBaz->shouldReceive('output')
538-
->with($tree)
538+
->with($tree, false)
539539
->andReturn([
540540
'created' => ['baz.php'],
541541
]);
@@ -566,7 +566,7 @@ public function generate_should_skip_one_specific_type()
566566
$skip = ['bar'];
567567

568568
$generatorFoo->shouldReceive('output')
569-
->with($tree)
569+
->with($tree, false)
570570
->andReturn([
571571
'created' => ['foo.php'],
572572
]);
@@ -576,7 +576,7 @@ public function generate_should_skip_one_specific_type()
576576

577577
$generatorBar = \Mockery::mock(Generator::class);
578578
$generatorBar->shouldReceive('output')
579-
->with($tree)
579+
->with($tree, false)
580580
->andReturn([
581581
'created' => ['bar.php'],
582582
]);
@@ -586,7 +586,7 @@ public function generate_should_skip_one_specific_type()
586586

587587
$generatorBaz = \Mockery::mock(Generator::class);
588588
$generatorBaz->shouldReceive('output')
589-
->with($tree)
589+
->with($tree, false)
590590
->andReturn([
591591
'created' => ['baz.php'],
592592
]);
@@ -617,7 +617,7 @@ public function generate_should_skip_specific_types()
617617
$skip = ['bar', 'baz'];
618618

619619
$generatorFoo->shouldReceive('output')
620-
->with($tree)
620+
->with($tree, false)
621621
->andReturn([
622622
'created' => ['foo.php'],
623623
]);
@@ -637,7 +637,7 @@ public function generate_should_skip_specific_types()
637637

638638
$generatorBaz = \Mockery::mock(Generator::class);
639639
$generatorBaz->shouldReceive('output')
640-
->with($tree)
640+
->with($tree, false)
641641
->andReturn([
642642
'created' => ['baz.php'],
643643
]);

tests/Feature/Commands/BuildCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function it_uses_the_default_draft_file()
2424
$builder = $this->mock(Builder::class);
2525

2626
$builder->shouldReceive('execute')
27-
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
27+
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '', false)
2828
->andReturn(collect([]));
2929

3030
$this->artisan('blueprint:build')
@@ -44,7 +44,7 @@ public function it_passes_the_command_args_to_the_builder_in_right_order()
4444
$builder = $this->mock(Builder::class);
4545

4646
$builder->shouldReceive('execute')
47-
->with(resolve(Blueprint::class), $filesystem, 'test.yml', 'a,b,c', 'x,y,z')
47+
->with(resolve(Blueprint::class), $filesystem, 'test.yml', 'a,b,c', 'x,y,z', false)
4848
->andReturn(collect([]));
4949

5050
$this->artisan('blueprint:build test.yml --only=a,b,c --skip=x,y,z')
@@ -82,7 +82,7 @@ public function it_shows_the_generated_files_groupbed_by_actions()
8282
$builder = $this->mock(Builder::class);
8383

8484
$builder->shouldReceive('execute')
85-
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '')
85+
->with(resolve(Blueprint::class), $filesystem, 'draft.yaml', '', '', false)
8686
->andReturn(collect([
8787
"created" => [
8888
"file1",

0 commit comments

Comments
 (0)