Skip to content

Commit 4004813

Browse files
authored
Merge pull request #45 from ingenerator/fix-db-mutext-across-php-vers
Make DbBackedMutexWrapper locking type-safe with PDO_MYSQL
2 parents 5cd3e76 + 85655a1 commit 4004813

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Fix deprecation warning when passing NULL to date validator by casting
66
to empty string to maintain current behaviour.
77

8+
* Make DbBackedMutexWrapper locking type-safe across PHP versions
9+
810
### v1.17.0 (2022-10-14)
911

1012
* Support PHP 8.2

src/Mutex/DbBackedMutexWrapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ protected function getLock(string $name, int $timeout_seconds): void
4040
->query("SELECT GET_LOCK($name_param, $timeout_seconds)")
4141
->fetchAll(PDO::FETCH_COLUMN, 0);
4242

43-
if ($result[0] !== '1') {
43+
// Need to explicitly cast the result to an int for comparison as PDO value types vary between 8.0 and 8.1+
44+
// And we should keep this cast even when we drop 8.0, because the PDO int/string mode is actually
45+
// configurable with a PDO attribute so could vary at runtime too.
46+
if (1 !== (int) $result[0]) {
4447
throw new MutexTimedOutException($name, $timeout_seconds, $result);
4548
}
4649
}

test/unit/Mutex/BasicPDOStatementStub.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ public function __construct(array $result) { $this->result = $result; }
4040

4141
public function fetchAll(int $fetch_style = NULL, mixed ...$fetch_argument): array
4242
{
43+
if (PHP_VERSION_ID < 80100) {
44+
// Before 8.1, ints and floats were returned as strings
45+
// https://www.php.net/manual/en/migration81.incompatible.php#migration81.incompatible.pdo.mysql
46+
$this->result = array_map(
47+
fn($row) => array_map(
48+
fn($column) => (is_int($column) || \is_float($column)) ? (string) $column : $column,
49+
$row
50+
),
51+
$this->result
52+
);
53+
}
54+
4355
if ($fetch_style === NULL) {
4456
return $this->result;
4557
} elseif ($fetch_style === PDO::FETCH_COLUMN) {
@@ -51,4 +63,4 @@ public function fetchAll(int $fetch_style = NULL, mixed ...$fetch_argument): arr
5163

5264

5365
}
54-
}
66+
}

test/unit/Mutex/DbBackedMutexWrapperTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ public function test_it_requests_lock_and_throws_if_not_available_without_runnin
2929
[
3030
[
3131
'sql' => 'SELECT GET_LOCK({quoted-mylock}, 5)',
32-
'result' => [['0']]
32+
'result' => [[0]]
3333
],
3434
[
3535
'sql' => 'SELECT RELEASE_LOCK({quoted-mylock})',
36-
'result' => [['0']]
36+
'result' => [[0]]
3737
],
3838
]
3939
);
@@ -53,11 +53,11 @@ public function test_it_releases_lock_after_successful_callback_and_returns_resu
5353
[
5454
[
5555
'sql' => 'SELECT GET_LOCK({quoted-somelock}, 2)',
56-
'result' => [['1']]
56+
'result' => [[1]]
5757
],
5858
[
5959
'sql' => 'SELECT RELEASE_LOCK({quoted-somelock})',
60-
'result' => [['0']]
60+
'result' => [[0]]
6161
],
6262
]
6363
);
@@ -80,11 +80,11 @@ public function test_it_releases_lock_after_failed_callback_and_bubbles()
8080
[
8181
[
8282
'sql' => 'SELECT GET_LOCK({quoted-somelock}, 2)',
83-
'result' => [['1']]
83+
'result' => [[1]]
8484
],
8585
[
8686
'sql' => 'SELECT RELEASE_LOCK({quoted-somelock})',
87-
'result' => [['0']]
87+
'result' => [[0]]
8888
],
8989
]
9090
);

0 commit comments

Comments
 (0)