Skip to content

Commit 08ec7c8

Browse files
committed
Backfill test for pivot migration
1 parent b4c6000 commit 08ec7c8

File tree

5 files changed

+118
-24
lines changed

5 files changed

+118
-24
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ class MigrationGenerator implements Generator
1818
];
1919

2020
const UNSIGNABLE_TYPES = [
21-
'bigInteger',
22-
'decimal',
23-
'integer',
24-
'mediumInteger',
25-
'smallInteger',
26-
'tinyInteger',
21+
'bigInteger',
22+
'decimal',
23+
'integer',
24+
'mediumInteger',
25+
'smallInteger',
26+
'tinyInteger',
2727
];
2828

2929
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
3030
private $files;
3131

32-
private $pivotTables = [];
33-
3432
public function __construct($files)
3533
{
3634
$this->files = $files;
@@ -39,6 +37,7 @@ public function __construct($files)
3937
public function output(array $tree): array
4038
{
4139
$output = [];
40+
$created_pivot_tables = [];
4241

4342
$stub = $this->files->stub('migration.stub');
4443

@@ -51,24 +50,18 @@ public function output(array $tree): array
5150

5251
$output['created'][] = $path;
5352

54-
if (!empty($modelPivots = $model->pivotTables())) {
55-
foreach ($modelPivots as $pivotSegments) {
53+
if (!empty($model->pivotTables())) {
54+
foreach ($model->pivotTables() as $pivotSegments) {
5655
$pivotTable = $this->getPivotTableName($pivotSegments);
57-
if (!isset($this->pivotTables[$pivotTable])) {
58-
$this->pivotTables[$pivotTable] = [
59-
'tableName' => $pivotTable,
60-
'segments' => $pivotSegments
61-
];
56+
if (isset($created_pivot_tables[$pivotTable])) {
57+
continue;
6258
}
63-
}
64-
}
65-
}
6659

67-
if (!empty($this->pivotTables)) {
68-
foreach ($this->pivotTables as $pivotTable) {
69-
$path = $this->getPivotTablePath($pivotTable['tableName'], $sequential_timestamp->addSecond());
70-
$this->files->put($path, $this->populatePivotStub($stub, $pivotTable['segments']));
71-
$output['created'][] = $path;
60+
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp);
61+
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
62+
$created_pivot_tables[] = $pivotTable;
63+
$output['created'][] = $path;
64+
}
7265
}
7366
}
7467

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function output_uses_past_timestamp_for_multiple_migrations()
9999
/**
100100
* @test
101101
*/
102-
public function output_uses_big_increments_for_id_column()
102+
public function output_uses_proper_data_type_for_id_column()
103103
{
104104
$app = \Mockery::mock();
105105
$app->expects('version')
@@ -125,6 +125,39 @@ public function output_uses_big_increments_for_id_column()
125125
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
126126
}
127127

128+
/**
129+
* @test
130+
*/
131+
public function output_also_creates_pivot_table_migration()
132+
{
133+
$app = \Mockery::mock();
134+
$app->expects('version')
135+
->withNoArgs()
136+
->andReturn('6.0.0');
137+
App::swap($app);
138+
139+
$this->files->expects('stub')
140+
->with('migration.stub')
141+
->andReturn(file_get_contents('stubs/migration.stub'));
142+
143+
$now = Carbon::now();
144+
Carbon::setTestNow($now);
145+
146+
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
147+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
148+
149+
$this->files->expects('put')
150+
->with($model_migration, $this->fixture('migrations/belongs-to-many.php'));
151+
152+
$this->files->expects('put')
153+
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-pivot.php'));
154+
155+
$tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many.bp'));
156+
$tree = $this->blueprint->analyze($tokens);
157+
158+
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
159+
}
160+
128161
public function modelTreeDataProvider()
129162
{
130163
return [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
Journey:
3+
name: string
4+
relationships:
5+
belongstoMany: Diary
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateDiaryJourneyTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('diary_journey', function (Blueprint $table) {
17+
$table->unsignedBigInteger('journey_id');
18+
$table->unsignedBigInteger('diary_id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('diary_journey');
30+
}
31+
}
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 CreateJourneysTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('journeys', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('name');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('journeys');
31+
}
32+
}

0 commit comments

Comments
 (0)