Skip to content

Commit 40c9bb1

Browse files
committed
fix: TriggerDefinition::when broke because of a new conflict with Laravel's Conditionable trait in v12.10.0
1 parent e4745fb commit 40c9bb1

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,13 @@ Schema::table('projects', function (Blueprint $table): void {
266266

267267
The following table contains all of the available trigger modifiers:
268268

269-
| Modifier | Description |
270-
|-----------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
271-
| `->forEachRow()` | The trigger will be called for every row. |
272-
| `->forEachStatement()` | The trigger will be called once for each statement *(default)*. |
273-
| `->transitionTables(`<br>` old: 'oldrows',`<br>` new: 'newrows',`<br>`)` | The forEachStatement-trigger will provide the before/after state of the affected rows in special tables. You can omit either option if not valid for this trigger. |
274-
| `->when('NEW.type = 4')`<br>`->when(fn ($query) => $query->where('NEW.type', 4))` | The trigger should only be called when the condition matches *(only with forEachRow)*. |
275-
| `->replace(true)` | The trigger will replace an existing one defined with the same name. |
269+
| Modifier | Description |
270+
|-----------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
271+
| `->forEachRow()` | The trigger will be called for every row. |
272+
| `->forEachStatement()` | The trigger will be called once for each statement *(default)*. |
273+
| `->transitionTables(`<br>` old: 'oldrows',`<br>` new: 'newrows',`<br>`)` | The forEachStatement-trigger will provide the before/after state of the affected rows in special tables. You can omit either option if not valid for this trigger. |
274+
| `->whenCondition('NEW.type = 4')`<br>`->whenCondition(fn ($query) => $query->where('NEW.type', 4))` | The trigger should only be called when the condition matches *(only with forEachRow)*. |
275+
| `->replace(true)` | The trigger will replace an existing one defined with the same name. |
276276

277277
> [!NOTE]
278278
> PostgreSQL always updates rows even if nothing changed, which may affect your performance. You can add the `suppress_redundant_updates_trigger()` trigger with a `BEFORE UPDATE` action to all tables.

src/Schema/Grammars/GrammarTrigger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function compileTrigger(Blueprint $blueprint, Fluent $command): string
4747
default => null,
4848
};
4949

50-
if (filled($command['when'])) {
51-
$condition = $command['when'];
50+
if (filled($command['whenCondition'])) {
51+
$condition = $command['whenCondition'];
5252
if ($condition instanceof Closure) {
5353
$query = ($condition)(DB::query());
5454
$condition = trim(str_replace('select * where', '', Query::toSql($query)));

src/Schema/TriggerDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Support\Fluent;
88

99
/**
10-
* @method $this when(string|callable $condition) Specify a condition when the trigger should be called.
10+
* @method $this whenCondition(string|callable $condition) Specify a condition when the trigger should be called.
1111
* @method $this replace(bool $value = true) Specify whether the trigger should replace an existing one.
1212
*/
1313
class TriggerDefinition extends Fluent

tests/Migration/TriggerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testCreateTriggerWhenBuilder(): void
110110
Schema::table('example', function (Blueprint $table): void {
111111
$table->trigger('noop_274029', 'noop()', 'after insert')
112112
->forEachRow()
113-
->when(fn (Builder $query) => $query->where('NEW.id', 42));
113+
->whenCondition(fn (Builder $query) => $query->where('NEW.id', 42));
114114
});
115115
});
116116
$this->assertEquals(['create trigger "noop_274029" after insert on "example" for each row when (NEW."id" = 42) execute function noop()'], array_column($queries, 'query'));
@@ -122,7 +122,7 @@ public function testCreateTriggerWhenSql(): void
122122
Schema::table('example', function (Blueprint $table): void {
123123
$table->trigger('noop_274029', 'noop()', 'after delete')
124124
->forEachRow()
125-
->when('OLD.id = 0815');
125+
->whenCondition('OLD.id = 0815');
126126
});
127127
});
128128
$this->assertEquals(['create trigger "noop_274029" after delete on "example" for each row when (OLD.id = 0815) execute function noop()'], array_column($queries, 'query'));

0 commit comments

Comments
 (0)