Skip to content

Commit 0751a26

Browse files
Merge pull request #40 from laravel-shift/sequential-migrations
Sequence migration timestamp by models
2 parents 4471c2f + 105dca6 commit 0751a26

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Model;
7+
use Carbon\Carbon;
78
use Illuminate\Support\Str;
89

910
class MigrationGenerator implements Generator
@@ -24,9 +25,11 @@ public function output(array $tree): array
2425

2526
$stub = $this->files->get(STUBS_PATH . '/migration.stub');
2627

28+
$sequential_timestamp = \Carbon\Carbon::now()->subSeconds(count($tree['models']));
29+
2730
/** @var \Blueprint\Models\Model $model */
2831
foreach ($tree['models'] as $model) {
29-
$path = $this->getPath($model);
32+
$path = $this->getPath($model, $sequential_timestamp->addSecond());
3033
$this->files->put(
3134
$path,
3235
$this->populateStub($stub, $model)
@@ -99,8 +102,8 @@ protected function getClassName(Model $model)
99102
return 'Create' . Str::studly($model->tableName()) . 'Table';
100103
}
101104

102-
protected function getPath(Model $model)
105+
protected function getPath(Model $model, Carbon $timestamp)
103106
{
104-
return 'database/migrations/' . \Carbon\Carbon::now()->format('Y_m_d_His') . '_create_' . $model->tableName() . '_table.php';
107+
return 'database/migrations/' . $timestamp->format('Y_m_d_His') . '_create_' . $model->tableName() . '_table.php';
105108
}
106109
}

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
6969
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
7070
}
7171

72+
/**
73+
* @test
74+
*/
75+
public function output_uses_past_timestamp_for_multiple_migrations()
76+
{
77+
$this->files->expects('get')
78+
->with('stubs/migration.stub')
79+
->andReturn(file_get_contents('stubs/migration.stub'));
80+
81+
$now = Carbon::now();
82+
Carbon::setTestNow($now);
83+
84+
$post_path = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_posts_table.php');
85+
$comment_path = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_comments_table.php');
86+
87+
$this->files->expects('put')
88+
->with($post_path, $this->fixture('migrations/posts.php'));
89+
$this->files->expects('put')
90+
->with($comment_path, $this->fixture('migrations/comments.php'));
91+
92+
$tokens = $this->blueprint->parse($this->fixture('definitions/multiple-models.bp'));
93+
$tree = $this->blueprint->analyze($tokens);
94+
95+
$this->assertEquals(['created' => [$post_path, $comment_path]], $this->subject->output($tree));
96+
}
97+
7298
public function modelTreeDataProvider()
7399
{
74100
return [
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
models:
2+
Post:
3+
title: string
4+
5+
Comment:
6+
content: text
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('comments', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->text('content');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('comments');
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePostsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('posts', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('title');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('posts');
31+
}
32+
}

0 commit comments

Comments
 (0)