-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
RefreshDatabase
+ double wrapping in DB::transaction()
breaks $afterCommit
functionality
#48451
Comments
created
does not run when using createOrFirst + DB transactions in tests
Confirmed. Thanks for reporting |
It appears to be a problem related to the |
I have disabled the savepoint fixes and double-wrap in public function withSavepointIfNeeded(Closure $scope): mixed
{
+ return $scope();
- return $this->getQuery()->getConnection()->transactionLevel() > 0
- ? $this->getQuery()->getConnection()->transaction($scope)
- : $scope();
} DB::transaction(fn () => DB::transaction(fn () => $user->notes()->createOrFirst(['notes' => 'abc']))); This is a more serious problem than we thought. Related to: |
@ryanldy Would you rename the issue into the following?
|
@pabloelcolombiano has also mentioned this problem: #35422 (comment) |
created
does not run when using createOrFirst + DB transactions in testsRefreshDatabase
+ double wrapping in DB::transaction()
breaks $afterCommit
functionality
@mpyw thank you. Done renaming the issue. |
@taylorotwell already tried to fix this issue in #41782, but it was not enough when transactions were nested. Would you review the changes? |
@tonysm I'm not sure... At least, I don't think that dealing specifically for |
Perhaps we should pass the following test with DB::transaction(fn () => DB::transaction(fn () => $user->notes()->create(['notes' => 'abc']))); |
I don't think it's a problem in real apps. It's only when testing (because the tests run in a transaction), so I think this should be a good fix for now. |
I think it is not bad as a temporary fix, but not enough as a permanent solution. Let @taylorotwell decide on that. |
@tonysm How about leaving |
Hmmm... I don't have any concrete ideas, but I think it would be better if RefreshDatabase Trait can do something about it. |
A better solution is not to use |
Fair enough, but it's still just a work-around. I think this is clearly a bug and would be happy to see a fundamental fix. |
Commented on the PR: #48466 (comment) |
While working on this issue I found a new bug. It appears that multiple connections are not being handled correctly. |
Laravel Version
10.23.1
PHP Version
8.2.10
Database Driver & Version
8.0.33
Description
In tests, model observer
created
does not run if all condition applies:RefreshDatabase
trait.createOrFirst()
orfirstOrCreate()
method and the method is insideDB::transaction()
.This issue started to appear in laravel/framework 10.21. I think it was introduced in #48144
cc: @tonysm / @mpyw
Steps To Reproduce
vendor/bin/phpunit
. Test should pass but it fails.NoteObserverTest
. Test now passes.The text was updated successfully, but these errors were encountered: