Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ public function output(array $tree): array
if (!empty($model->pivotTables())) {
foreach ($model->pivotTables() as $pivotSegments) {
$pivotTable = $this->getPivotTableName($pivotSegments);
if (isset($created_pivot_tables[$pivotTable])) {
continue;
}

$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp);
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
$created_pivot_tables[] = $pivotTable;
$output['created'][] = $path;
$created_pivot_tables[$pivotTable] = $pivotSegments;
}
}
}

foreach ($created_pivot_tables as $pivotTable => $pivotSegments) {
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp);
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
$created_pivot_tables[] = $pivotTable;
$output['created'][] = $path;
}

return $output;
}

Expand Down
64 changes: 64 additions & 0 deletions tests/Feature/Generator/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,70 @@ public function output_also_creates_constraints_for_pivot_table_migration_larave
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_does_not_duplicate_pivot_table_migration()
{
$this->files->expects('stub')
->with('migration.stub')
->andReturn(file_get_contents('stubs/migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$company_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
$people_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php');

$this->files->expects('put')
->with($company_migration, $this->fixture('migrations/belongs-to-many-duplicated-company.php'));
$this->files->expects('put')
->with($people_migration, $this->fixture('migrations/belongs-to-many-duplicated-people.php'));
$this->files->expects('put')
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-duplicated-pivot.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many-duplicated-pivot.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_does_not_duplicate_pivot_table_migration_laravel6()
{
$app = \Mockery::mock();
$app->shouldReceive('version')
->withNoArgs()
->andReturn('6.0.0');
App::swap($app);

$this->files->expects('stub')
->with('migration.stub')
->andReturn(file_get_contents('stubs/migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$company_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
$people_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php');

$this->files->expects('put')
->with($company_migration, $this->fixture('migrations/belongs-to-many-duplicated-company-laravel6.php'));
$this->files->expects('put')
->with($people_migration, $this->fixture('migrations/belongs-to-many-duplicated-people-laravel6.php'));
$this->files->expects('put')
->with($pivot_migration, $this->fixture('migrations/belongs-to-many-duplicated-pivot-laravel6.php'));

$tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many-duplicated-pivot.bp'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree));
}

public function modelTreeDataProvider()
{
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
models:
Company:
name: string
relationships:
belongsToMany: Person
Person:
name: string
relationships:
belongsToMany: Company
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('companies');
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/migrations/belongs-to-many-duplicated-company.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('companies');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePeopleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('people', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('people');
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/migrations/belongs-to-many-duplicated-people.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePeopleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('people', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('people');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCompanyPersonTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('company_person', function (Blueprint $table) {
$table->unsignedBigInteger('company_id');
$table->unsignedBigInteger('person_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('company_person');
}
}
31 changes: 31 additions & 0 deletions tests/fixtures/migrations/belongs-to-many-duplicated-pivot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCompanyPersonTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('company_person', function (Blueprint $table) {
$table->foreignId('company_id');
$table->foreignId('person_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('company_person');
}
}