Skip to content

Commit eaa8300

Browse files
authored
Fix: pivot table naming when model name contains path prefix (#388)
1 parent 11f475c commit eaa8300

7 files changed

+193
-2
lines changed

src/Models/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function addRelationship(string $type, string $reference)
155155

156156
public function addPivotTable(string $reference)
157157
{
158-
$segments = [$this->name(), $reference];
158+
$segments = [$this->name(), class_basename($reference)];
159159
sort($segments);
160160
$this->pivotTables[] = $segments;
161161
}
@@ -164,7 +164,7 @@ public function indexes(): array
164164
{
165165
return $this->indexes;
166166
}
167-
167+
168168
public function addIndex(Index $index)
169169
{
170170
$this->indexes[] = $index;

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,63 @@ public function output_also_creates_pivot_table_migration_with_custom_name_larav
550550
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
551551
}
552552

553+
/**
554+
* @test
555+
*/
556+
public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix()
557+
{
558+
$this->files->expects('stub')
559+
->with('migration.stub')
560+
->andReturn($this->stub('migration.stub'));
561+
562+
$now = Carbon::now();
563+
Carbon::setTestNow($now);
564+
565+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_regions_table.php');
566+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_city_region_table.php');
567+
568+
$this->files->expects('exists')->twice()->andReturn(false);
569+
570+
$this->files->expects('put')
571+
->with($model_migration, $this->fixture('migrations/with-path-prefix-table-name-region.php'));
572+
$this->files->expects('put')
573+
->with($pivot_migration, $this->fixture('migrations/with-path-prefix-table-name-city-region.php'));
574+
575+
$tokens = $this->blueprint->parse($this->fixture('drafts/with-path-prefix.yaml'));
576+
$tree = $this->blueprint->analyze($tokens);
577+
578+
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
579+
}
580+
581+
/**
582+
* @test
583+
* @environment-setup useLaravel6
584+
*/
585+
public function output_creates_pivot_table_migration_correctly_when_model_name_contains_path_prefix_laravel6()
586+
{
587+
$this->files->expects('stub')
588+
->with('migration.stub')
589+
->andReturn($this->stub('migration.stub'));
590+
591+
$now = Carbon::now();
592+
Carbon::setTestNow($now);
593+
594+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_regions_table.php');
595+
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_city_region_table.php');
596+
597+
$this->files->expects('exists')->twice()->andReturn(false);
598+
599+
$this->files->expects('put')
600+
->with($model_migration, $this->fixture('migrations/with-path-prefix-table-name-region-laravel6.php'));
601+
$this->files->expects('put')
602+
->with($pivot_migration, $this->fixture('migrations/with-path-prefix-table-name-city-region-laravel6.php'));
603+
604+
$tokens = $this->blueprint->parse($this->fixture('drafts/with-path-prefix.yaml'));
605+
$tree = $this->blueprint->analyze($tokens);
606+
607+
$this->assertEquals(['created' => [$model_migration, $pivot_migration]], $this->subject->output($tree));
608+
}
609+
553610
/**
554611
* @test
555612
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
models:
2+
Lookup\Region:
3+
name_en: string:255 nullable
4+
softDeletes
5+
relationships:
6+
belongsToMany: Lookup\City
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 CreateCityRegionTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('city_region', function (Blueprint $table) {
17+
$table->unsignedBigInteger('city_id');
18+
$table->unsignedBigInteger('region_id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('city_region');
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 CreateCityRegionTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('city_region', function (Blueprint $table) {
17+
$table->foreignId('city_id');
18+
$table->foreignId('region_id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('city_region');
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateRegionsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('regions', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->string('name_en', 255)->nullable();
19+
$table->softDeletes();
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('regions');
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateRegionsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('regions', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name_en', 255)->nullable();
19+
$table->softDeletes();
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('regions');
32+
}
33+
}

0 commit comments

Comments
 (0)