From 8205360fc26b386bac54a81c132f5b617b6740b5 Mon Sep 17 00:00:00 2001 From: Gianluca Bine Date: Fri, 1 May 2020 15:28:04 -0300 Subject: [PATCH 1/3] Fix duplicated pivot table generation --- src/Generators/MigrationGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 4f4ff3c4..ea1cce62 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -53,7 +53,7 @@ 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])) { + if (in_array($pivotTable, $created_pivot_tables)) { continue; } From 2a83f572d662a65104120c664b5f9e5e28deec1c Mon Sep 17 00:00:00 2001 From: Gianluca Bine Date: Fri, 1 May 2020 15:28:23 -0300 Subject: [PATCH 2/3] Added tests for duplicated pivot table generation --- .../Generator/MigrationGeneratorTest.php | 64 +++++++++++++++++++ .../belongs-to-many-duplicated-pivot.bp | 9 +++ ...gs-to-many-duplicated-company-laravel6.php | 32 ++++++++++ .../belongs-to-many-duplicated-company.php | 32 ++++++++++ ...ngs-to-many-duplicated-people-laravel6.php | 32 ++++++++++ .../belongs-to-many-duplicated-people.php | 32 ++++++++++ ...ongs-to-many-duplicated-pivot-laravel6.php | 31 +++++++++ .../belongs-to-many-duplicated-pivot.php | 31 +++++++++ 8 files changed, 263 insertions(+) create mode 100644 tests/fixtures/definitions/belongs-to-many-duplicated-pivot.bp create mode 100644 tests/fixtures/migrations/belongs-to-many-duplicated-company-laravel6.php create mode 100644 tests/fixtures/migrations/belongs-to-many-duplicated-company.php create mode 100644 tests/fixtures/migrations/belongs-to-many-duplicated-people-laravel6.php create mode 100644 tests/fixtures/migrations/belongs-to-many-duplicated-people.php create mode 100644 tests/fixtures/migrations/belongs-to-many-duplicated-pivot-laravel6.php create mode 100644 tests/fixtures/migrations/belongs-to-many-duplicated-pivot.php diff --git a/tests/Feature/Generator/MigrationGeneratorTest.php b/tests/Feature/Generator/MigrationGeneratorTest.php index 86b50779..5cb1bcf5 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::createFromTimestamp(1588354812); + Carbon::setTestNow($now); + + $company_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php'); + $people_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354812)->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php'); + $pivot_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->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, $pivot_migration, $people_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::createFromTimestamp(1588354812); + Carbon::setTestNow($now); + + $company_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php'); + $people_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354812)->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php'); + $pivot_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->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, $pivot_migration, $people_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'); + } +} From aa798f9d195b68f4eca46b93c648c906bc4efc60 Mon Sep 17 00:00:00 2001 From: Gianluca Bine Date: Fri, 1 May 2020 16:13:33 -0300 Subject: [PATCH 3/3] Fix migrations order --- src/Generators/MigrationGenerator.php | 16 +++++++-------- .../Generator/MigrationGeneratorTest.php | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index ea1cce62..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 (in_array($pivotTable, $created_pivot_tables)) { - 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 5cb1bcf5..2ec60143 100644 --- a/tests/Feature/Generator/MigrationGeneratorTest.php +++ b/tests/Feature/Generator/MigrationGeneratorTest.php @@ -257,12 +257,12 @@ public function output_does_not_duplicate_pivot_table_migration() ->with('migration.stub') ->andReturn(file_get_contents('stubs/migration.stub')); - $now = Carbon::createFromTimestamp(1588354812); + $now = Carbon::now(); Carbon::setTestNow($now); - $company_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php'); - $people_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354812)->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php'); - $pivot_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php'); + $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')); @@ -274,7 +274,7 @@ public function output_does_not_duplicate_pivot_table_migration() $tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many-duplicated-pivot.bp')); $tree = $this->blueprint->analyze($tokens); - $this->assertEquals(['created' => [$company_migration, $pivot_migration, $people_migration]], $this->subject->output($tree)); + $this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree)); } /** @@ -292,12 +292,12 @@ public function output_does_not_duplicate_pivot_table_migration_laravel6() ->with('migration.stub') ->andReturn(file_get_contents('stubs/migration.stub')); - $now = Carbon::createFromTimestamp(1588354812); + $now = Carbon::now(); Carbon::setTestNow($now); - $company_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php'); - $people_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354812)->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php'); - $pivot_migration = str_replace('timestamp', Carbon::createFromTimestamp(1588354811)->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php'); + $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')); @@ -309,7 +309,7 @@ public function output_does_not_duplicate_pivot_table_migration_laravel6() $tokens = $this->blueprint->parse($this->fixture('definitions/belongs-to-many-duplicated-pivot.bp')); $tree = $this->blueprint->analyze($tokens); - $this->assertEquals(['created' => [$company_migration, $pivot_migration, $people_migration]], $this->subject->output($tree)); + $this->assertEquals(['created' => [$company_migration, $people_migration, $pivot_migration]], $this->subject->output($tree)); } public function modelTreeDataProvider()