Skip to content

limit sql length to avoid breaking the json #326

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 4 commits into from
Mar 11, 2024
Merged

Conversation

kima92
Copy link
Contributor

@kima92 kima92 commented Mar 6, 2024

Issue

We've identified a bug within Laravel Pulse where if the slow query detector captures an excessively large SQL statement, it leads to a disruption in the JSON schema stored in the database. This disruption makes it impossible to read the affected data, impacting the tool's ability to provide accurate insights into application performance.

Root Cause

The root cause of this issue lies in the default length of the database column designated for storing SQL statements. When a captured SQL statement exceeds this default length (65535), the resulting JSON encoding process creates a payload that violates the column's size constraints, leading to corruption of the JSON schema and subsequent data retrieval problems.

Proposed Solution

To address this issue, I propose implementing a check to truncate SQL statements to a maximum character length before encoding them into JSON. This approach ensures that the payload size remains within the database field size constraints, thus preserving the integrity of the JSON schema.

Changes Made

Start using Str::limit($sql, 65000) to make sure we are not getting too close to the limit.

Impact

This fix ensures that Laravel Pulse can handle large SQL statements without compromising the JSON schema's integrity in the database, thereby maintaining the tool's efficacy in providing performance insights.

@taylorotwell taylorotwell requested a review from jessarcher March 6, 2024 16:02
@taylorotwell
Copy link
Member

Drafting pending review from @jessarcher or @timacdonald

@taylorotwell taylorotwell marked this pull request as draft March 6, 2024 16:02
@timacdonald
Copy link
Member

@kima92, it sounds like you have the old migration where the key was a TEXT column. We have migrated it to a MEDIUMTEXT, which should mean you can store ~16,777,215 characters, instead of the ~65,535 characters you are hitting up against.

I would recommend updating your schema to match the latest migrations.

That being said, I do feel it would be a nice additional to allow the maximum query length to be controlled. In most cases I would think that there isn't much value in having Pulse retrieve potentially megabytes worth of SQL string.

I would prefer this be a configurable option rather than a hardcoded length.

@kima92 kima92 marked this pull request as ready for review March 10, 2024 16:19
@kima92
Copy link
Contributor Author

kima92 commented Mar 11, 2024

Thank you @timacdonald, I've updated the schema and it works great now.
Also added config with env to the code

@taylorotwell taylorotwell merged commit 6fc88b3 into laravel:1.x Mar 11, 2024
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.

3 participants