Skip to content

Commit 9df2071

Browse files
authored
Fix/duplicated pivot table (#169)
1 parent 4e55afc commit 9df2071

9 files changed

+271
-8
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ public function output(array $tree): array
5353
if (!empty($model->pivotTables())) {
5454
foreach ($model->pivotTables() as $pivotSegments) {
5555
$pivotTable = $this->getPivotTableName($pivotSegments);
56-
if (isset($created_pivot_tables[$pivotTable])) {
57-
continue;
58-
}
59-
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;
56+
$created_pivot_tables[$pivotTable] = $pivotSegments;
6457
}
6558
}
6659
}
6760

61+
foreach ($created_pivot_tables as $pivotTable => $pivotSegments) {
62+
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp);
63+
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
64+
$created_pivot_tables[] = $pivotTable;
65+
$output['created'][] = $path;
66+
}
67+
6868
return $output;
6969
}
7070

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,70 @@ public function output_also_creates_constraints_for_pivot_table_migration_larave
248248
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
249249
}
250250

251+
/**
252+
* @test
253+
*/
254+
public function output_does_not_duplicate_pivot_table_migration()
255+
{
256+
$this->files->expects('stub')
257+
->with('migration.stub')
258+
->andReturn(file_get_contents('stubs/migration.stub'));
259+
260+
$now = Carbon::now();
261+
Carbon::setTestNow($now);
262+
263+
$company_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
264+
$people_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
265+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php');
266+
267+
$this->files->expects('put')
268+
->with($company_migration, $this->fixture('migrations/belongs-to-many-duplicated-company.php'));
269+
$this->files->expects('put')
270+
->with($people_migration, $this->fixture('migrations/belongs-to-many-duplicated-people.php'));
271+
$this->files->expects('put')
272+
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-duplicated-pivot.php'));
273+
274+
$tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many-duplicated-pivot.bp'));
275+
$tree = $this->blueprint->analyze($tokens);
276+
277+
$this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree));
278+
}
279+
280+
/**
281+
* @test
282+
*/
283+
public function output_does_not_duplicate_pivot_table_migration_laravel6()
284+
{
285+
$app = \Mockery::mock();
286+
$app->shouldReceive('version')
287+
->withNoArgs()
288+
->andReturn('6.0.0');
289+
App::swap($app);
290+
291+
$this->files->expects('stub')
292+
->with('migration.stub')
293+
->andReturn(file_get_contents('stubs/migration.stub'));
294+
295+
$now = Carbon::now();
296+
Carbon::setTestNow($now);
297+
298+
$company_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
299+
$people_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
300+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php');
301+
302+
$this->files->expects('put')
303+
->with($company_migration, $this->fixture('migrations/belongs-to-many-duplicated-company-laravel6.php'));
304+
$this->files->expects('put')
305+
->with($people_migration, $this->fixture('migrations/belongs-to-many-duplicated-people-laravel6.php'));
306+
$this->files->expects('put')
307+
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-duplicated-pivot-laravel6.php'));
308+
309+
$tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many-duplicated-pivot.bp'));
310+
$tree = $this->blueprint->analyze($tokens);
311+
312+
$this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree));
313+
}
314+
251315
public function modelTreeDataProvider()
252316
{
253317
return [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
models:
2+
Company:
3+
name: string
4+
relationships:
5+
belongsToMany: Person
6+
Person:
7+
name: string
8+
relationships:
9+
belongsToMany: Company
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 CreateCompaniesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('companies', 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('companies');
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 CreateCompaniesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('companies', function (Blueprint $table) {
17+
$table->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('companies');
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 CreatePeopleTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('people', 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('people');
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 CreatePeopleTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('people', function (Blueprint $table) {
17+
$table->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('people');
31+
}
32+
}
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 CreateCompanyPersonTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('company_person', function (Blueprint $table) {
17+
$table->unsignedBigInteger('company_id');
18+
$table->unsignedBigInteger('person_id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('company_person');
30+
}
31+
}
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 CreateCompanyPersonTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('company_person', function (Blueprint $table) {
17+
$table->foreignId('company_id');
18+
$table->foreignId('person_id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('company_person');
30+
}
31+
}

0 commit comments

Comments
 (0)