Skip to content

Commit 80e4761

Browse files
committed
Complete onDelete + streamline config options
1 parent 1d067bf commit 80e4761

File tree

6 files changed

+146
-24
lines changed

6 files changed

+146
-24
lines changed

config/blueprint.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@
6464
| within the generated migration. This will relate these records
6565
| together to add structure and integrity to your database.
6666
|
67+
| In addition, you may specify the action to perform `ON DELETE`. By
68+
| default Blueprint will use `cascade`. However, you may set this
69+
| to 'restrict', 'no_action', or 'null' as well as inline
70+
| by defining your `foreign` key column with an `onDelete`.
71+
|
6772
*/
6873
'use_constraints' => false,
6974

75+
'on_delete' => 'cascade',
76+
7077
/*
7178
|--------------------------------------------------------------------------
7279
| Fake Nullables
@@ -95,23 +102,4 @@
95102

96103
'use_guarded' => false,
97104

98-
/*
99-
|--------------------------------------------------------------------------
100-
| Foreign Key ON DELETE action
101-
|--------------------------------------------------------------------------
102-
|
103-
| By default, Blueprint will set the `ON DELETE` action to 'cascade'.
104-
|
105-
| restrict / no action:
106-
| - No action is performed with the child data when the parent data is deleted.
107-
| cascade:
108-
| - The child data is either deleted when the parent data is deleted.
109-
| set null:
110-
| - The child data is set to NULL when the parent data is deleted.
111-
|
112-
| Supported: 'cascade', 'set_null', 'restrict', 'no_action'
113-
|
114-
*/
115-
116-
'on_delete' => 'cascade',
117105
];

src/Generators/MigrationGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MigrationGenerator implements Generator
2020
const ON_DELETE_CLAUSES = [
2121
'cascade' => "->onDelete('cascade')",
2222
'restrict' => "->onDelete('restrict')",
23-
'set_null' => "->onDelete('set null')",
23+
'null' => "->onDelete('set null')",
2424
'no_action' => "->onDelete('no action')",
2525
];
2626

@@ -240,8 +240,8 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
240240
$table = Str::lower(Str::plural($attributes[0]));
241241
}
242242

243-
$on_delete_clause = config('blueprint.on_delete', 'cascade');
244-
243+
$on_delete_clause = collect($modifiers)->firstWhere('onDelete');
244+
$on_delete_clause = $on_delete_clause ? $on_delete_clause['onDelete'] : config('blueprint.on_delete', 'cascade');
245245
$on_delete_suffix = self::ON_DELETE_CLAUSES[$on_delete_clause];
246246

247247
if ($this->isLaravel7orNewer() && $type === 'id') {

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public function output_also_creates_pivot_table_migration_with_custom_name_larav
453453
*/
454454
public function output_creates_foreign_keys_with_nullable_chained_correctly()
455455
{
456-
$this->app->config->set('blueprint.on_delete', 'set_null');
456+
$this->app->config->set('blueprint.on_delete', 'null');
457457

458458
$this->files->expects('stub')
459459
->with('migration.stub')
@@ -479,7 +479,7 @@ public function output_creates_foreign_keys_with_nullable_chained_correctly()
479479
*/
480480
public function output_creates_foreign_keys_with_nullable_chained_correctly_laravel6()
481481
{
482-
$this->app->config->set('blueprint.on_delete', 'set_null');
482+
$this->app->config->set('blueprint.on_delete', 'null');
483483

484484
$app = \Mockery::mock();
485485
$app->shouldReceive('version')
@@ -506,6 +506,60 @@ public function output_creates_foreign_keys_with_nullable_chained_correctly_lara
506506
$this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree));
507507
}
508508

509+
/**
510+
* @test
511+
*/
512+
public function output_creates_foreign_keys_with_on_delete()
513+
{
514+
$this->files->expects('stub')
515+
->with('migration.stub')
516+
->andReturn(file_get_contents('stubs/migration.stub'));
517+
518+
$now = Carbon::now();
519+
Carbon::setTestNow($now);
520+
521+
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_comments_table.php');
522+
523+
$this->files
524+
->expects('put')
525+
->with($model_migration, $this->fixture('migrations/foreign-key-on-delete.php'));
526+
527+
$tokens = $this->blueprint->parse($this->fixture('definitions/foreign-key-on-delete.bp'));
528+
$tree = $this->blueprint->analyze($tokens);
529+
530+
$this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree));
531+
}
532+
533+
/**
534+
* @test
535+
*/
536+
public function output_creates_foreign_keys_with_on_delete_laravel6()
537+
{
538+
$app = \Mockery::mock();
539+
$app->shouldReceive('version')
540+
->withNoArgs()
541+
->andReturn('6.0.0');
542+
App::swap($app);
543+
544+
$this->files->expects('stub')
545+
->with('migration.stub')
546+
->andReturn(file_get_contents('stubs/migration.stub'));
547+
548+
$now = Carbon::now();
549+
Carbon::setTestNow($now);
550+
551+
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_comments_table.php');
552+
553+
$this->files
554+
->expects('put')
555+
->with($model_migration, $this->fixture('migrations/foreign-key-on-delete-laravel6.php'));
556+
557+
$tokens = $this->blueprint->parse($this->fixture('definitions/foreign-key-on-delete.bp'));
558+
$tree = $this->blueprint->analyze($tokens);
559+
560+
$this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree));
561+
}
562+
509563
/**
510564
* @test
511565
*/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
models:
2+
Comment:
3+
site_id: foreign
4+
post_id: foreign onDelete:null
5+
author_id: foreign:user ondelete:restrict
6+
approver_id: foreign:user ondelete:no_action
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('comments', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('site_id');
19+
$table->foreign('site_id')->references('id')->on('sites')->onDelete('cascade');
20+
$table->unsignedBigInteger('post_id');
21+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('set null');
22+
$table->unsignedBigInteger('author_id');
23+
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
24+
$table->unsignedBigInteger('approver_id');
25+
$table->foreign('approver_id')->references('id')->on('users')->onDelete('no action');
26+
$table->timestamps();
27+
});
28+
}
29+
30+
/**
31+
* Reverse the migrations.
32+
*
33+
* @return void
34+
*/
35+
public function down()
36+
{
37+
Schema::dropIfExists('comments');
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('comments', function (Blueprint $table) {
17+
$table->id();
18+
$table->foreignId('site_id')->constrained()->cascadeOnDelete();
19+
$table->foreignId('post_id')->constrained()->onDelete('set null');
20+
$table->foreignId('author_id')->constrained('users')->onDelete('restrict');
21+
$table->foreignId('approver_id')->constrained('users')->onDelete('no action');
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('comments');
34+
}
35+
}

0 commit comments

Comments
 (0)