@@ -39,7 +39,7 @@ public static function fromLockId(
39
39
$ lockStringId = (string )$ lockId ;
40
40
41
41
return new self (
42
- id: self ::generateIdFromString ($ lockStringId ),
42
+ id: self ::convertStringToSignedInt32 ($ lockStringId ),
43
43
humanReadableValue: $ lockStringId ,
44
44
);
45
45
}
@@ -51,15 +51,19 @@ public static function fromLockId(
51
51
* The crc32 function returns an unsigned 32-bit integer (0 to 4_294_967_295).
52
52
* Postgres advisory locks require a signed 32-bit integer (-2_147_483_648 to 2_147_483_647).
53
53
*
54
- * This method shifts the crc32 output into the signed int32 range by subtracting (2^31) + 1 ,
55
- * ensuring the result fits within Postgres's required range and preserves uniqueness .
54
+ * This method converts the unsigned crc32 result to a signed 32-bit integer ,
55
+ * matching the way PostgreSQL interprets int4 values internally .
56
56
*
57
- * @param string $string The input string to hash into an int32 lock ID.
57
+ * @param string $string The input string to hash into a signed int32 lock ID.
58
58
* @return int The signed 32-bit integer suitable for Postgres advisory locks.
59
59
*/
60
- private static function generateIdFromString (
60
+ private static function convertStringToSignedInt32 (
61
61
string $ string ,
62
62
): int {
63
- return crc32 ($ string ) - self ::DB_INT32_VALUE_MAX - 1 ;
63
+ $ unsignedInt = crc32 ($ string );
64
+
65
+ return $ unsignedInt > 0x7FFFFFFF
66
+ ? $ unsignedInt - 0x100000000
67
+ : $ unsignedInt ;
64
68
}
65
69
}
0 commit comments