diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 4f4ff3c4..2a12e21b 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -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; } diff --git a/tests/Feature/Generator/MigrationGeneratorTest.php b/tests/Feature/Generator/MigrationGeneratorTest.php index 86b50779..2ec60143 100644 --- a/tests/Feature/Generator/MigrationGeneratorTest.php +++ b/tests/Feature/Generator/MigrationGeneratorTest.php @@ -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 [ diff --git a/tests/fixtures/definitions/belongs-to-many-duplicated-pivot.bp b/tests/fixtures/definitions/belongs-to-many-duplicated-pivot.bp new file mode 100644 index 00000000..4448ab39 --- /dev/null +++ b/tests/fixtures/definitions/belongs-to-many-duplicated-pivot.bp @@ -0,0 +1,9 @@ +models: + Company: + name: string + relationships: + belongsToMany: Person + Person: + name: string + relationships: + belongsToMany: Company diff --git a/tests/fixtures/migrations/belongs-to-many-duplicated-company-laravel6.php b/tests/fixtures/migrations/belongs-to-many-duplicated-company-laravel6.php new file mode 100644 index 00000000..5b04b671 --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-duplicated-company-laravel6.php @@ -0,0 +1,32 @@ +bigIncrements('id'); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('companies'); + } +} diff --git a/tests/fixtures/migrations/belongs-to-many-duplicated-company.php b/tests/fixtures/migrations/belongs-to-many-duplicated-company.php new file mode 100644 index 00000000..7c2c2886 --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-duplicated-company.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('companies'); + } +} diff --git a/tests/fixtures/migrations/belongs-to-many-duplicated-people-laravel6.php b/tests/fixtures/migrations/belongs-to-many-duplicated-people-laravel6.php new file mode 100644 index 00000000..a9b00799 --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-duplicated-people-laravel6.php @@ -0,0 +1,32 @@ +bigIncrements('id'); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('people'); + } +} diff --git a/tests/fixtures/migrations/belongs-to-many-duplicated-people.php b/tests/fixtures/migrations/belongs-to-many-duplicated-people.php new file mode 100644 index 00000000..5485b44a --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-duplicated-people.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('people'); + } +} diff --git a/tests/fixtures/migrations/belongs-to-many-duplicated-pivot-laravel6.php b/tests/fixtures/migrations/belongs-to-many-duplicated-pivot-laravel6.php new file mode 100644 index 00000000..e7822311 --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-duplicated-pivot-laravel6.php @@ -0,0 +1,31 @@ +unsignedBigInteger('company_id'); + $table->unsignedBigInteger('person_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('company_person'); + } +} diff --git a/tests/fixtures/migrations/belongs-to-many-duplicated-pivot.php b/tests/fixtures/migrations/belongs-to-many-duplicated-pivot.php new file mode 100644 index 00000000..c83f695d --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-duplicated-pivot.php @@ -0,0 +1,31 @@ +foreignId('company_id'); + $table->foreignId('person_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('company_person'); + } +}