Skip to content
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

[10.x] Only stage committed transactions #49093

Merged
merged 19 commits into from
Nov 23, 2023
Merged
Prev Previous commit
Next Next commit
push failing test
  • Loading branch information
taylorotwell committed Nov 22, 2023
commit cd0212b5a1481a6b930d196028352bfe82c1a1e8
16 changes: 10 additions & 6 deletions src/Illuminate/Database/DatabaseTransactionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public function begin($connection, $level)
* Rollback the active database transaction.
*
* @param string $connection
* @param int $level
* @param int $newTransactionLevel
* @return void
*/
public function rollback($connection, $level)
public function rollback($connection, $newTransactionLevel)
{
$this->pendingTransactions = $this->pendingTransactions->reject(
fn ($transaction) => $transaction->connection == $connection && $transaction->level > $level
fn ($transaction) => $transaction->connection == $connection &&
$transaction->level > $newTransactionLevel
)->values();
}

Expand Down Expand Up @@ -95,18 +96,21 @@ public function addCallback($callback)
* Move relevant pending transactions to a committed state.
*
* @param string $connection
* @param int $levelBeingCommitted
* @return void
*/
public function stageTransactions($connection, $level)
public function stageTransactions($connection, $levelBeingCommitted)
{
$this->committedTransactions = $this->committedTransactions->merge(
$this->pendingTransactions->filter(
fn ($transaction) => $transaction->connection === $connection && $transaction->level >= $level
fn ($transaction) => $transaction->connection === $connection &&
$transaction->level >= $levelBeingCommitted
)
);

$this->pendingTransactions = $this->pendingTransactions->reject(
fn ($transaction) => $transaction->connection === $connection && $transaction->level >= $level
fn ($transaction) => $transaction->connection === $connection &&
$transaction->level >= $levelBeingCommitted
);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Events/ShouldDispatchAfterCommitEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,30 @@ public function testItDoesNotDispatchAfterCommitEventsImmediatelyIfASiblingTrans

$this->assertTrue(ShouldDispatchAfterCommitTestEvent::$ran);
}

public function testChildEventsAreNotDispatchedIfParentTransactionFails()
{
Event::listen(ShouldDispatchAfterCommitTestEvent::class, ShouldDispatchAfterCommitListener::class);

try {
DB::transaction(function () {
DB::transaction(function () {
Event::dispatch(new ShouldDispatchAfterCommitTestEvent);
});

$this->assertFalse(ShouldDispatchAfterCommitTestEvent::$ran);

throw new \Exception;
});
} catch (\Exception $e) {
//
}

DB::transaction(fn () => true);

// Should not have ran because parent transaction failed...
$this->assertFalse(ShouldDispatchAfterCommitTestEvent::$ran);
}
}

class TransactionUnawareTestEvent
Expand Down
Loading