-
Couldn't load subscription status.
- Fork 97
Fix: relationship join cache with query clones #202
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
Fix: relationship join cache with query clones #202
Conversation
This reverts commit ed09eea.
|
Thanks @ralphjsmit. Seems like this change is breaking a few tests, would you mind taking a look at those? |
|
Thanks @luisdalmolin! Yes I am taking a look. It seems that only the tests are failing on Laravel 10, for which it is logical that the added test fails, as it depends on the newly added |
|
That's interesting. I'll take a look at this then. We can consider dropping Laravel 10 support, since Laravel 12 is near. |
|
I just took a look, but without the |
|
I only need to stop now for today, I will try tomorrow if all tests pass when Laravel 10 is dropped and at least |
|
@luisdalmolin Can you trigger the tests again?
# Before:
Time: 00:01.858, Memory: 44.50 MB
# After:
Time: 00:01.864, Memory: 42.50 MB |
|
All good now 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, I only pushed a small change to the tests (executing the queries as well) but I'm merging this. Since this is dropping support for Laravel 10, I'll have to tag a new major version.
|
Well, the problem with a new major version is that Filament would have to update that as well. And it seems like Filament supports Laravel 10 still, so this would likely only be added on the new version? |
|
Thanks Luis! Looks good. I am not sure actually if this would need a major version, because, if we would release this a minor version, only the people with Laravel 11 would be able to upgrade to this version and for them it would work, and for the people on Laravel 10 this new version would not even be suggested, because Composer already knows itself that it isn't compatible with Laravel 10, so these people will remain on what is the current latest version. So personally I don't really see why it should be a breaking change, because Composer always ensures that this version in only installable on L11 systems... Does that make sense? |
|
Yeah good point, I think we can go with this approach, I like this better. |
|
This is tagged as 4.2.0 - please let me know if this works well for you. Thanks for this! |
|
Hey @luisdalmolin, just tried and looking good for me, thanks! |
Hello there, thank you for the nice library!
I am using it in e.g. the context of Filament and quite often I have a flaky test that fails on a "column X.Y does not exist". After much digging, the reason for this appears to be the "is relation already joined" cache, which depends on the
$modelinside the query. However, if a query is cloned, this$modelis not refreshed, leaving you with a situation where the clone shares the cache with the original query. This can be quite dangerous, because if one of the queries is modified or e.g. executed, this impacts the cache for the other query, leaving the other query with potentially a incorrect cache for "is relation already joined".I added tests with more examples, but a good example where you would have this query:
which could then be cloned:
...whereas after that on both queries separately a join is applied:
Two situations
There are generally two clone situations:
BuilderOn Clone callback support laravel/framework#54477 to reset the cache)I have extracted both checks into an
JoinsHelper::ensureModelIsUniqueToQuery()method, which is called on every interaction with the library, which has the responsibility of preventing both situations. If the first situation is happening, it will correct immediately (by having a cache of which Spl object IDs of which model belongs to which query, and one model can only belong to max. 1 query). For the second situation, a respective callback is added.The added TestCase
JoinRelationshipAfterCloneTestalso had examples of both situations. You can confirm these snippets fail on the currentmaster.Thanks, and let me know if there's any questions about this!