Skip to content

Use portable WKT envelope instead of ST_MakeEnvelope (fix MariaDB; fix MySQL SRID 4326)#248

Open
LeftoversTodayAppAdmin wants to merge 1 commit into
fleetbase:mainfrom
LeftoversTodayAppAdmin:fix/portable-envelope-spatial-filter
Open

Use portable WKT envelope instead of ST_MakeEnvelope (fix MariaDB; fix MySQL SRID 4326)#248
LeftoversTodayAppAdmin wants to merge 1 commit into
fleetbase:mainfrom
LeftoversTodayAppAdmin:fix/portable-envelope-spatial-filter

Conversation

@LeftoversTodayAppAdmin
Copy link
Copy Markdown

Closes #247.

Summary

The bounds filter in LiveController::drivers(), vehicles(), and places() is built with ST_MakeEnvelope, which is a MySQL-only convenience function. This PR replaces it with a portable WKT polygon passed through ST_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_MakeEnvelope in MySQL only accepts SRID 0 geometries. Fleetbase's Point, Polygon, and MultiPolygon casts (see server/src/Casts/) emit SRID 4326 — they construct geometries via Utils::createSpatialExpressionFromGeoJson\Fleetbase\LaravelMysqlSpatial\Types\Geometry::fromJson(...), and recent migrations such as 2025_10_27_171322_fix_device_column_names.php explicitly normalize positions to ST_SRID(POINT(0, 0), 4326). Mixing the SRID-0 envelope with SRID-4326 column values raises ER_GIS_DIFFERENT_SRIDS (3618) or ER_NOT_IMPLEMENTED_FOR_GEOGRAPHIC_SRS (3680) on MySQL 8 the moment bounds filtering is hit on a populated table.

Repro on current main

-- MariaDB
SELECT ST_MakeEnvelope(POINT(-1, -1), POINT(1, 1));
-- ERROR 1305 (42000): FUNCTION ST_MakeEnvelope does not exist

-- MySQL 8 with SRID 4326 column
SELECT ST_Within(
  ST_SRID(POINT(0, 0), 4326),
  ST_MakeEnvelope(POINT(-1, -1), POINT(1, 1))
);
-- ERROR 3618 (or 3680): geometries have different SRIDs / not implemented for geographic SRS

Documentation references:

Change

Each of the three call sites now builds the bounds rectangle as a WKT polygon and passes it through ST_GeomFromText(?, 4326), keeping the ST_Within(location, …) form so a future SPATIAL INDEX on location remains usable.

$envelopeWkt = sprintf(
    'POLYGON((%1$F %2$F, %3$F %2$F, %3$F %4$F, %1$F %4$F, %1$F %2$F))',
    $west, $south, $east, $north
);
$query->whereRaw(
    'ST_Within(location, ST_GeomFromText(?, 4326))',
    [$envelopeWkt]
);

Notes:

  • %F (locale-independent float) is used so locales with comma decimal separators don't produce invalid WKT.
  • Polygon ring order: lower-left → lower-right → upper-right → upper-left → lower-left.
  • SRID 4326 is 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 with php: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).
  • Did not run vendor/bin/phpunit from inside the standalone package; happy to do that or stand up a fixture if you'd prefer.
  • Manual smoke recommendation for reviewers: hit /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 a FUNCTION does not exist error).

Affected endpoints

  • GET /v1/internal/live/drivers
  • GET /v1/internal/live/vehicles
  • GET /v1/internal/live/places

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}.
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.

ST_MakeEnvelope breaks MariaDB and fails on MySQL with SRID 4326 spatial columns

1 participant