Skip to content

[12.x] Easily run pipelines inside of a transactions with Pipeline::withinTransaction() #56377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2025
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
24 changes: 23 additions & 1 deletion src/Illuminate/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class Pipeline implements PipelineContract
*/
protected $finally;

/**
* Indicates whether to wrap the pipeline in a database transaction.
*
* @var bool
*/
protected $withinTransactions = false;

/**
* Create a new class instance.
*
Expand Down Expand Up @@ -123,7 +130,9 @@ public function then(Closure $destination)
);

try {
return $pipeline($this->passable);
return $this->withinTransactions
? $this->container->make('db')->transaction(fn () => $pipeline($this->passable))
: $pipeline($this->passable);
} finally {
if ($this->finally) {
($this->finally)($this->passable);
Expand Down Expand Up @@ -245,6 +254,19 @@ protected function pipes()
return $this->pipes;
}

/**
* Execute each pipeline step within a database transaction.
*
* @param bool $withinTransactions
* @return $this
*/
public function withinTransactions(bool $withinTransactions = true)
{
$this->withinTransactions = $withinTransactions;

return $this;
}

/**
* Get the container instance.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Pipeline/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"Illuminate\\Pipeline\\": ""
}
},
"suggest": {
"illuminate/database": "Required to use database transactions (^12.0)."
},
"extra": {
"branch-alias": {
"dev-master": "12.x-dev"
Expand Down
56 changes: 56 additions & 0 deletions tests/Pipeline/PipelineTransactionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Illuminate\Tests\Pipeline;

use Exception;
use Illuminate\Database\Events\TransactionBeginning;
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Database\Events\TransactionRolledBack;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Pipeline;
use Orchestra\Testbench\TestCase;

class PipelineTransactionTest extends TestCase
{
public function testPipelineTransaction()
{
Event::fake();

$result = Pipeline::withinTransactions()
->send('some string')
->through([function ($value, $next) {
return $next($value);
}])
->thenReturn();

$this->assertEquals('some string', $result);
Event::assertDispatched(TransactionBeginning::class);
Event::assertDispatched(TransactionCommitted::class);
}

public function testExceptionThrownRollsBackTransaction()
{
Event::fake();

$finallyRan = false;
try {
Pipeline::withinTransactions()
->send('some string')
->through([
function ($value, $next) {
throw new Exception('I was thrown');
},
])
->finally(function () use (&$finallyRan) {
$finallyRan = true;
})
->thenReturn();
$this->fail('No exception was thrown');
} catch (Exception) {
}

$this->assertTrue($finallyRan);
Event::assertDispatched(TransactionBeginning::class);
Event::assertDispatched(TransactionRolledBack::class);
}
}