Skip to content

Commit d46c708

Browse files
author
Volodymyr Panivko
committed
fix: add deterministic ordering to history queries and fix revert test
Add id DESC tiebreaker to all ORDER BY time queries with LIMIT to ensure consistent results after MyISAM to InnoDB migration. Fix BikeLastUsageTest to assert revert code matches the current rent code, not the previous one.
1 parent 424abb6 commit d46c708

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/Repository/HistoryRepository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function findLastBikeRentByUser(int $bikeNumber, int $userId): ?array
104104
WHERE bikeNum = :bikeNumber
105105
AND userId = :userId
106106
AND action = :rentAction
107-
ORDER BY time DESC
107+
ORDER BY time DESC, id DESC
108108
LIMIT 1",
109109
[
110110
'bikeNumber' => $bikeNumber,
@@ -149,7 +149,7 @@ public function findConfirmationRequest(string $checkCode, int $userId): ?array
149149
WHERE action = :phoneConfirmRequestAction
150150
AND parameter = :checkCode
151151
AND userId = :userId
152-
ORDER BY time DESC
152+
ORDER BY time DESC, id DESC
153153
LIMIT 1",
154154
[
155155
'checkCode' => $checkCode,
@@ -263,7 +263,7 @@ public function findLastReturnStand(int $bikeNum): ?array
263263
LEFT JOIN history ON stands.standId = parameter
264264
WHERE bikeNum = :bikeNum
265265
AND action IN (:returnAction, :forceReturnAction)
266-
ORDER BY time DESC
266+
ORDER BY time DESC, history.id DESC
267267
LIMIT 1",
268268
[
269269
'bikeNum' => $bikeNum,
@@ -291,7 +291,7 @@ public function findLastRentCode(int $bikeNum): ?string
291291
FROM history
292292
WHERE bikeNum = :bikeNum
293293
AND action IN (:rentAction, :forceRentAction)
294-
ORDER BY time DESC
294+
ORDER BY time DESC, id DESC
295295
LIMIT 1",
296296
[
297297
'bikeNum' => $bikeNum,
@@ -352,17 +352,17 @@ public function findUserTripHistory(int $userId, int $limit = 10): array
352352
(SELECT r.time FROM history r
353353
WHERE r.userId = h.userId AND r.bikeNum = h.bikeNum
354354
AND r.action IN (:returnAction, :forceReturnAction) AND r.time >= h.time
355-
ORDER BY r.time ASC LIMIT 1) AS returnTime,
355+
ORDER BY r.time ASC, r.id ASC LIMIT 1) AS returnTime,
356356
(SELECT s.standName FROM history r
357357
LEFT JOIN stands s ON s.standId = r.parameter
358358
WHERE r.userId = h.userId AND r.bikeNum = h.bikeNum
359359
AND r.action IN (:returnAction2, :forceReturnAction2) AND r.time >= h.time
360-
ORDER BY r.time ASC LIMIT 1) AS standName,
360+
ORDER BY r.time ASC, r.id ASC LIMIT 1) AS standName,
361361
(SELECT s2.standName FROM history r2
362362
LEFT JOIN stands s2 ON s2.standId = r2.parameter
363363
WHERE r2.bikeNum = h.bikeNum
364364
AND r2.action IN (:returnAction3, :forceReturnAction3) AND r2.time < h.time
365-
ORDER BY r2.time DESC LIMIT 1) AS fromStandName
365+
ORDER BY r2.time DESC, r2.id DESC LIMIT 1) AS fromStandName
366366
FROM history h
367367
WHERE h.userId = :userId AND h.action IN (:rentAction, :forceRentAction)
368368
ORDER BY h.time DESC, h.id DESC

tests/Application/Controller/Api/Bike/BikeLastUsageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testTrip(): void
5959

6060
$this->assertSame(
6161
$responseData['history'][2]['parameter'], //REVERT
62-
$responseData['history'][5]['parameter'], //RENT
62+
$responseData['history'][3]['parameter'], //RENT (the one being reverted)
6363
'Incorrect code after revert. Full history' . json_encode($responseData['history'])
6464
);
6565
}

0 commit comments

Comments
 (0)