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

Avoid calling query() twice by adding a local var, fixes #4133 and #4140 #4221

Open
wants to merge 5 commits into
base: 3.1
Choose a base branch
from

Conversation

Synchro
Copy link

@Synchro Synchro commented Oct 16, 2024

1️⃣ Why should it be added? What are the benefits of this change?

This is a fix for an issue reported in the Laravel Nova Excel project - though it turned out that the issue was here, not there.

The problem is that if query() makes non-idempotent changes to the $query, it's likely to result in a corrupt query. So to avoid this, we need to make sure we only call query() once, and once we have an instance, take care not to alter it further. I found that calling chunk() on $query altered it, making a test fail. I solved that by cloning it, so the changes caused by chunk can be discarded.

2️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out.

No, just one.

3️⃣ Does it include tests, if possible?

Yes

4️⃣ Any drawbacks? Possible breaking changes?

If there are other places that query() is called in a similar way, the same problem may occur there too. I ran into the same thing inside a test.

5️⃣ Mark the following tasks as done:

  • Checked the codebase to ensure that your feature doesn't already exist.
  • Take note of the contributing guidelines.
  • Checked the pull requests to ensure that another person hasn't already submitted a fix.
  • Added tests to ensure against regression.

6️⃣ Thanks for contributing! 🙌

@Synchro
Copy link
Author

Synchro commented Oct 16, 2024

Uhhh, failing tests... When was clone added to Eloquent?

@patrickbrouwers
Copy link
Member

Is the clone necessary?

@Synchro
Copy link
Author

Synchro commented Oct 17, 2024

It seems to be – if you remove it, the test fails because the query has the chunking clause attached to it, so returns a smaller number of items than the full query, so the results don't match. Using a clone means the original is left intact.

@patrickbrouwers
Copy link
Member

Perhaps we can do a check if the clone method exists. If not we just call the entire query method again so it has the current behavior. Support for older Laravel versions will be dropped anyway in the near future so I don't mind if the issue remains for the legacy versions.

@Synchro
Copy link
Author

Synchro commented Oct 17, 2024

Instead of that, I used clone directly, which works in all Laravel versions

@Synchro
Copy link
Author

Synchro commented Oct 17, 2024

As I mentioned in the original PR description, it's quite likely that this problem exists in other places too; we've just not run into them yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants