Use portable WKT envelope instead of ST_MakeEnvelope (fix MariaDB; fix MySQL SRID 4326)#248
Open
LeftoversTodayAppAdmin wants to merge 1 commit into
Conversation
ST_MakeEnvelope is a MySQL-only convenience function (5.7.6+) and is
not implemented in MariaDB. It also restricts inputs to SRID 0 on
MySQL, which conflicts with the SRID 4326 geometries Fleetbase's
spatial casts emit, so bounds filtering can fail on MySQL 8 too.
Replace each ST_MakeEnvelope call with an equivalent WKT POLYGON
passed through ST_GeomFromText(?, 4326). Behavior on MySQL is
preserved (and corrected for SRID 4326 columns); MariaDB is now
supported. %F is used in sprintf so locales with comma decimal
separators do not corrupt the WKT.
Affected endpoints: GET /v1/internal/live/{drivers,vehicles,places}.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #247.
Summary
The bounds filter in
LiveController::drivers(),vehicles(), andplaces()is built withST_MakeEnvelope, which is a MySQL-only convenience function. This PR replaces it with a portable WKT polygon passed throughST_GeomFromText(?, 4326)so the code runs unchanged on MySQL 8 / 8.4 and MariaDB 10.5+ / 11.x.Why this is also a correctness fix on MySQL
ST_MakeEnvelopein MySQL only accepts SRID 0 geometries. Fleetbase'sPoint,Polygon, andMultiPolygoncasts (seeserver/src/Casts/) emit SRID 4326 — they construct geometries viaUtils::createSpatialExpressionFromGeoJson→\Fleetbase\LaravelMysqlSpatial\Types\Geometry::fromJson(...), and recent migrations such as2025_10_27_171322_fix_device_column_names.phpexplicitly normalize positions toST_SRID(POINT(0, 0), 4326). Mixing the SRID-0 envelope with SRID-4326 column values raisesER_GIS_DIFFERENT_SRIDS(3618) orER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS(3680) on MySQL 8 the moment bounds filtering is hit on a populated table.Repro on current
mainDocumentation references:
ST_MakeEnvelope): https://mariadb.com/docs/server/reference/sql-statements/geometry-constructors/miscellaneous-gis-functionsST_MakeEnvelopeis defined for SRID 0 only.Change
Each of the three call sites now builds the bounds rectangle as a WKT polygon and passes it through
ST_GeomFromText(?, 4326), keeping theST_Within(location, …)form so a futureSPATIAL INDEXonlocationremains usable.Notes:
%F(locale-independent float) is used so locales with comma decimal separators don't produce invalid WKT.4326is hardcoded to match Fleetbase's storage convention. If a future refactor allows variable SRIDs, the literal can become a bound parameter.Testing
php -l server/src/Http/Controllers/Internal/v1/LiveController.php— clean (verified withphp:8.2-cli).grep -nF 'ST_MakeEnvelope(' server/src/— zero matches (the only remaining references are explanatory comments noting why we're not using it).vendor/bin/phpunitfrom inside the standalone package; happy to do that or stand up a fixture if you'd prefer./v1/internal/live/{drivers,vehicles,places}?bounds[]=…on a MySQL 8 instance with SRID 4326 rows — now returns 200 with correctly filtered results (previously 500). Same against MariaDB 11 — now works (previously aFUNCTION does not existerror).Affected endpoints
GET /v1/internal/live/driversGET /v1/internal/live/vehiclesGET /v1/internal/live/places