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
3 changes: 2 additions & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column)
return true;
}

return in_array($column->dataType(), ['id', 'uuid']) && config('blueprint.use_constraints');
return config('blueprint.use_constraints')
&& ($column->dataType() === 'id' || $column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'));
}
}
5 changes: 4 additions & 1 deletion src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ private function buildModel(string $name, array $columns)
return collect($modifier)->containsStrict('foreign') || collect($modifier)->has('foreign');
})->flatten()->first();

if (($column->name() !== 'id') && ($column->dataType() === 'id') || $foreign) {
if (($column->name() !== 'id' && $column->dataType() === 'id')
|| ($column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'))
|| $foreign
) {
$reference = $column->name();

if ($foreign && $foreign !== 'foreign') {
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function it_parses_uuid_shorthand()
'Person' => [
'id' => 'uuid primary',
'timestamps' => 'timestamps',
'company_id' => 'uuid',
],
],
], $this->subject->parse($blueprint));
Expand Down
59 changes: 59 additions & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,64 @@ public function output_works_with_polymorphic_relationships_laravel6()
$this->assertEquals(['created' => [$post_migration, $user_migration, $image_migration]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_does_not_generate_relationship_for_uuid()
{
$this->app->config->set('blueprint.use_constraints', true);

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

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

$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_vats_table.php';

$this->files->expects('exists')
->with($timestamp_path)
->andReturn(false);

$this->files->expects('put')
->with($timestamp_path, $this->fixture('migrations/uuid-without-relationship.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/uuid-without-relationship.yaml'));
$tree = $this->blueprint->analyze($tokens);

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

/**
* @test
*/
public function output_generates_constraint_for_uuid()
{
$this->app->config->set('blueprint.use_constraints', true);

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

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

$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_people_table.php';

$this->files->expects('exists')
->with($timestamp_path)
->andReturn(false);

$this->files->expects('put')
->with($timestamp_path, $this->fixture('migrations/uuid-shorthand-constraint.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/uuid-shorthand.yaml'));
$tree = $this->blueprint->analyze($tokens);

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

public function modelTreeDataProvider()
{
return [
Expand All @@ -797,6 +855,7 @@ public function modelTreeDataProvider()
['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'],
['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'],
['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'],
['drafts/uuid-without-relationship.yaml', 'database/migrations/timestamp_create_vats_table.php', 'migrations/uuid-without-relationship.php'],
['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'],
['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/drafts/model-relationships.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
models:
Subscription:
user_id: id
product_id: uuid
relationships:
belongsToMany: Team
hasMany: Order
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/drafts/uuid-shorthand.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
models:
Person:
uuid
company_id: uuid
timestamps
3 changes: 3 additions & 0 deletions tests/fixtures/drafts/uuid-without-relationship.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
models:
Vat:
uuid: uuid
37 changes: 37 additions & 0 deletions tests/fixtures/migrations/uuid-shorthand-constraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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::disableForeignKeyConstraints();

Schema::create('people', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('company_id');
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->timestamps();
});

Schema::enableForeignKeyConstraints();
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('people');
}
}
1 change: 1 addition & 0 deletions tests/fixtures/migrations/uuid-shorthand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function up()
{
Schema::create('people', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('company_id');
$table->timestamps();
});
}
Expand Down
32 changes: 32 additions & 0 deletions tests/fixtures/migrations/uuid-without-relationship.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 CreateVatsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('vats', function (Blueprint $table) {
$table->id();
$table->uuid('uuid');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('vats');
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/models/model-relationships-laravel8.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Subscription extends Model
*/
protected $fillable = [
'user_id',
'product_id',
];

/**
Expand Down Expand Up @@ -53,4 +54,9 @@ public function user()
{
return $this->belongsTo(\App\User::class);
}

public function product()
{
return $this->belongsTo(\App\Product::class);
}
}