Skip to content

Commit 98d43c3

Browse files
authored
Support onupdate clauses for migrations (#425)
1 parent f6be94c commit 98d43c3

21 files changed

+64
-34
lines changed

config/blueprint.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
'use_constraints' => false,
7474

7575
'on_delete' => 'cascade',
76+
'on_update' => 'cascade',
77+
7678

7779
/*
7880
|--------------------------------------------------------------------------

src/Generators/MigrationGenerator.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class MigrationGenerator implements Generator
2727
'no_action' => "->onDelete('no action')",
2828
];
2929

30+
const ON_UPDATE_CLAUSES = [
31+
'cascade' => "->onUpdate('cascade')",
32+
'restrict' => "->onUpdate('restrict')",
33+
'null' => "->onUpdate('set null')",
34+
'no_action' => "->onUpdate('no action')",
35+
];
36+
3037
const UNSIGNABLE_TYPES = [
3138
'bigInteger',
3239
'decimal',
@@ -196,6 +203,7 @@ protected function buildDefinition(Model $model)
196203
$modifiers = collect($modifiers)->reject(function ($modifier) {
197204
return (is_array($modifier) && key($modifier) === 'foreign')
198205
|| (is_array($modifier) && key($modifier) === 'onDelete')
206+
|| (is_array($modifier) && key($modifier) === 'onUpdate')
199207
|| $modifier === 'foreign'
200208
|| ($modifier === 'nullable' && $this->isLaravel7orNewer());
201209
});
@@ -289,9 +297,18 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
289297
$table = Str::lower(Str::plural($attributes[0]));
290298
}
291299

300+
$on_delete_suffix = $on_update_suffix = null;
292301
$on_delete_clause = collect($modifiers)->firstWhere('onDelete');
293-
$on_delete_clause = $on_delete_clause ? $on_delete_clause['onDelete'] : config('blueprint.on_delete', 'cascade');
294-
$on_delete_suffix = self::ON_DELETE_CLAUSES[$on_delete_clause];
302+
if (config('blueprint.use_constraints') || $on_delete_clause) {
303+
$on_delete_clause = $on_delete_clause ? $on_delete_clause['onDelete'] : config('blueprint.on_delete', 'cascade');
304+
$on_delete_suffix = self::ON_DELETE_CLAUSES[$on_delete_clause];
305+
}
306+
307+
$on_update_clause = collect($modifiers)->firstWhere('onUpdate');
308+
if (config('blueprint.use_constraints') || $on_update_clause) {
309+
$on_update_clause = $on_update_clause ? $on_update_clause['onUpdate'] : config('blueprint.on_update', 'cascade');
310+
$on_update_suffix = self::ON_UPDATE_CLAUSES[$on_update_clause];
311+
}
295312

296313
if ($this->isLaravel7orNewer() && $type === 'id') {
297314
$prefix = in_array('nullable', $modifiers)
@@ -301,17 +318,21 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
301318
if ($on_delete_clause === 'cascade') {
302319
$on_delete_suffix = '->cascadeOnDelete()';
303320
}
321+
if ($on_update_clause === 'cascade') {
322+
$on_update_suffix = '->cascadeOnUpdate()';
323+
}
324+
304325
if ($column_name === Str::singular($table).'_'.$column) {
305-
return self::INDENT."{$prefix}->constrained(){$on_delete_suffix}";
326+
return self::INDENT."{$prefix}->constrained(){$on_delete_suffix}{$on_update_suffix}";
306327
}
307328
if ($column === 'id') {
308-
return self::INDENT."{$prefix}->constrained('{$table}'){$on_delete_suffix}";
329+
return self::INDENT."{$prefix}->constrained('{$table}'){$on_delete_suffix}{$on_update_suffix}";
309330
}
310331

311-
return self::INDENT."{$prefix}->constrained('{$table}', '{$column}'){$on_delete_suffix}";
332+
return self::INDENT."{$prefix}->constrained('{$table}', '{$column}'){$on_delete_suffix}{$on_update_suffix}";
312333
}
313334

314-
return self::INDENT.'$table->foreign'."('{$column_name}')->references('{$column}')->on('{$table}'){$on_delete_suffix}";
335+
return self::INDENT.'$table->foreign'."('{$column_name}')->references('{$column}')->on('{$table}'){$on_delete_suffix}{$on_update_suffix}";
315336
}
316337

317338
protected function disableForeignKeyConstraints($stub): string

src/Lexers/ModelLexer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class ModelLexer implements Lexer
9696
'primary' => 'primary',
9797
'foreign' => 'foreign',
9898
'ondelete' => 'onDelete',
99+
'onupdate' => 'onUpdate',
99100
'comment' => 'comment',
100101
];
101102

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ public function output_creates_pivot_table_migration_correctly_when_model_name_c
620620
*/
621621
public function output_creates_foreign_keys_with_nullable_chained_correctly()
622622
{
623+
$this->app->config->set('blueprint.use_constraints', true);
623624
$this->app->config->set('blueprint.on_delete', 'null');
624625

625626
$this->files->expects('stub')
@@ -649,6 +650,7 @@ public function output_creates_foreign_keys_with_nullable_chained_correctly()
649650
*/
650651
public function output_creates_foreign_keys_with_nullable_chained_correctly_laravel6()
651652
{
653+
$this->app->config->set('blueprint.use_constraints', true);
652654
$this->app->config->set('blueprint.on_delete', 'null');
653655

654656
$this->files->expects('stub')

tests/fixtures/drafts/foreign-key-on-delete.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ models:
22
Comment:
33
site_id: foreign
44
post_id: foreign onDelete:null
5+
comment_id: foreign onDelete:cascade
56
author_id: foreign:user ondelete:restrict
67
approver_id: foreign:user ondelete:no_action

tests/fixtures/migrations/belongs-to-many-key-constraints-laravel6.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function up()
1919
$table->bigIncrements('id');
2020
$table->string('name');
2121
$table->unsignedBigInteger('user_id');
22-
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
22+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
2323
$table->timestamps();
2424
});
2525

tests/fixtures/migrations/belongs-to-many-key-constraints.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function up()
1818
Schema::create('journeys', function (Blueprint $table) {
1919
$table->id();
2020
$table->string('name');
21-
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
21+
$table->foreignId('user_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
2222
$table->timestamps();
2323
});
2424

tests/fixtures/migrations/belongs-to-many-pivot-key-constraints-laravel6.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function up()
1717

1818
Schema::create('diary_journey', function (Blueprint $table) {
1919
$table->unsignedBigInteger('diary_id');
20-
$table->foreign('diary_id')->references('id')->on('diaries')->onDelete('cascade');
20+
$table->foreign('diary_id')->references('id')->on('diaries')->onDelete('cascade')->onUpdate('cascade');
2121
$table->unsignedBigInteger('journey_id');
22-
$table->foreign('journey_id')->references('id')->on('journeys')->onDelete('cascade');
22+
$table->foreign('journey_id')->references('id')->on('journeys')->onDelete('cascade')->onUpdate('cascade');
2323
});
2424

2525
Schema::enableForeignKeyConstraints();

tests/fixtures/migrations/belongs-to-many-pivot-key-constraints.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function up()
1616
Schema::disableForeignKeyConstraints();
1717

1818
Schema::create('diary_journey', function (Blueprint $table) {
19-
$table->foreignId('diary_id')->constrained()->cascadeOnDelete();
20-
$table->foreignId('journey_id')->constrained()->cascadeOnDelete();
19+
$table->foreignId('diary_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
20+
$table->foreignId('journey_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
2121
});
2222

2323
Schema::enableForeignKeyConstraints();

tests/fixtures/migrations/custom-indexes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function up()
1717

1818
Schema::create('cooltables', function (Blueprint $table) {
1919
$table->id();
20-
$table->foreignId('coolcool')->constrained('coolcool')->cascadeOnDelete()->index('custom_index_coolcool');
21-
$table->foreignId('foobar')->constrained('foobars')->cascadeOnDelete()->index('custom_index_foobar');
20+
$table->foreignId('coolcool')->constrained('coolcool')->index('custom_index_coolcool');
21+
$table->foreignId('foobar')->constrained('foobars')->index('custom_index_foobar');
2222
});
2323

2424
Schema::enableForeignKeyConstraints();

0 commit comments

Comments
 (0)