Skip to content

Commit 6cca54d

Browse files
Do not generate foreign key for simple UUID column (#418)
1 parent 677c842 commit 6cca54d

11 files changed

+147
-2
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column)
400400
return true;
401401
}
402402

403-
return in_array($column->dataType(), ['id', 'uuid']) && config('blueprint.use_constraints');
403+
return config('blueprint.use_constraints')
404+
&& ($column->dataType() === 'id' || $column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'));
404405
}
405406
}

src/Lexers/ModelLexer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ private function buildModel(string $name, array $columns)
187187
return collect($modifier)->containsStrict('foreign') || collect($modifier)->has('foreign');
188188
})->flatten()->first();
189189

190-
if (($column->name() !== 'id') && ($column->dataType() === 'id') || $foreign) {
190+
if (($column->name() !== 'id' && $column->dataType() === 'id')
191+
|| ($column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'))
192+
|| $foreign
193+
) {
191194
$reference = $column->name();
192195

193196
if ($foreign && $foreign !== 'foreign') {

tests/Feature/BlueprintTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function it_parses_uuid_shorthand()
130130
'Person' => [
131131
'id' => 'uuid primary',
132132
'timestamps' => 'timestamps',
133+
'company_id' => 'uuid',
133134
],
134135
],
135136
], $this->subject->parse($blueprint));

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,64 @@ public function output_works_with_polymorphic_relationships_laravel6()
780780
$this->assertEquals(['created' => [$post_migration, $user_migration, $image_migration]], $this->subject->output($tree));
781781
}
782782

783+
/**
784+
* @test
785+
*/
786+
public function output_does_not_generate_relationship_for_uuid()
787+
{
788+
$this->app->config->set('blueprint.use_constraints', true);
789+
790+
$this->files->expects('stub')
791+
->with('migration.stub')
792+
->andReturn($this->stub('migration.stub'));
793+
794+
$now = Carbon::now();
795+
Carbon::setTestNow($now);
796+
797+
$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_vats_table.php';
798+
799+
$this->files->expects('exists')
800+
->with($timestamp_path)
801+
->andReturn(false);
802+
803+
$this->files->expects('put')
804+
->with($timestamp_path, $this->fixture('migrations/uuid-without-relationship.php'));
805+
806+
$tokens = $this->blueprint->parse($this->fixture('drafts/uuid-without-relationship.yaml'));
807+
$tree = $this->blueprint->analyze($tokens);
808+
809+
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
810+
}
811+
812+
/**
813+
* @test
814+
*/
815+
public function output_generates_constraint_for_uuid()
816+
{
817+
$this->app->config->set('blueprint.use_constraints', true);
818+
819+
$this->files->expects('stub')
820+
->with('migration.stub')
821+
->andReturn($this->stub('migration.stub'));
822+
823+
$now = Carbon::now();
824+
Carbon::setTestNow($now);
825+
826+
$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_people_table.php';
827+
828+
$this->files->expects('exists')
829+
->with($timestamp_path)
830+
->andReturn(false);
831+
832+
$this->files->expects('put')
833+
->with($timestamp_path, $this->fixture('migrations/uuid-shorthand-constraint.php'));
834+
835+
$tokens = $this->blueprint->parse($this->fixture('drafts/uuid-shorthand.yaml'));
836+
$tree = $this->blueprint->analyze($tokens);
837+
838+
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
839+
}
840+
783841
public function modelTreeDataProvider()
784842
{
785843
return [
@@ -797,6 +855,7 @@ public function modelTreeDataProvider()
797855
['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'],
798856
['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'],
799857
['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'],
858+
['drafts/uuid-without-relationship.yaml', 'database/migrations/timestamp_create_vats_table.php', 'migrations/uuid-without-relationship.php'],
800859
['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'],
801860
['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
802861
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],

tests/fixtures/drafts/model-relationships.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
models:
22
Subscription:
33
user_id: id
4+
product_id: uuid
45
relationships:
56
belongsToMany: Team
67
hasMany: Order
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
models:
22
Person:
33
uuid
4+
company_id: uuid
45
timestamps
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
models:
2+
Vat:
3+
uuid: uuid
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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::disableForeignKeyConstraints();
17+
18+
Schema::create('people', function (Blueprint $table) {
19+
$table->uuid('id')->primary();
20+
$table->uuid('company_id');
21+
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
22+
$table->timestamps();
23+
});
24+
25+
Schema::enableForeignKeyConstraints();
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::dropIfExists('people');
36+
}
37+
}

tests/fixtures/migrations/uuid-shorthand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
{
1616
Schema::create('people', function (Blueprint $table) {
1717
$table->uuid('id')->primary();
18+
$table->uuid('company_id');
1819
$table->timestamps();
1920
});
2021
}
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 CreateVatsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('vats', function (Blueprint $table) {
17+
$table->id();
18+
$table->uuid('uuid');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('vats');
31+
}
32+
}

0 commit comments

Comments
 (0)