Skip to content

Commit 109b151

Browse files
committed
fixup! fixup! feat(snowflake): Use string as type for snowflake ids
1 parent 7a6cb9a commit 109b151

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/private/DB/Snowflake/SnowflakeGenerator.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,13 @@ public function parseId(string $id): array {
172172
* @internal For unit tests only.
173173
*/
174174
public function getCurrentMillisecond(): string {
175-
$time = microtime();
176-
$time = explode(' ', $time);
177-
return $time[1] . floor((float)$time[0] * 1000);
175+
if ($this->is32BitsSystem()) {
176+
$time = microtime();
177+
$time = explode(' ', $time);
178+
return $time[1] . str_pad((string)floor((float)$time[0] * 1000), 3, '0', STR_PAD_LEFT);
179+
} else {
180+
return (string)(floor(microtime(true) * 1000));
181+
}
178182
}
179183

180184
/**
@@ -183,6 +187,10 @@ public function getCurrentMillisecond(): string {
183187
*/
184188
public function setStartTimeStamp(int $second): self {
185189
if ($this->is32BitsSystem()) {
190+
if (!function_exists('gmp_init')) {
191+
throw new \RuntimeException('gmp is a required extension to run on 32 bit system.');
192+
}
193+
186194
$missTime = gmp_sub($this->getCurrentMillisecond(), gmp_mul($second, 1000));
187195

188196
if (gmp_cmp($missTime, 0) < 0) {
@@ -202,7 +210,7 @@ public function setStartTimeStamp(int $second): self {
202210
$missTime = (int)$this->getCurrentMillisecond() - $second * 1000;
203211

204212
if ($missTime < 0) {
205-
throw new \InvalidArgumentException('The start time cannot be greater than the current time');
213+
throw new \InvalidArgumentException('The start time cannot be greater than the current time. starttime=' . $second * 1000 . ' current=' . $this->getCurrentMillisecond());
206214
}
207215

208216
$maxTimeDiff = -1 ^ (-1 << self::MAX_TIMESTAMP_LENGTH);

tests/lib/DB/SnowflakeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class SnowflakeTest extends TestCase {
2323
#[TestWith(data: [false, true, 60 * 60 * 24 * 365 * 10])] // ~10 years
2424
#[TestWith(data: [false, false, 60 * 60 * 24 * 365 * 10])]
2525
public function testLayout(bool $isCLIExpected, bool $is32BitsSystem, int $timeDiff): void {
26+
if (!$is32BitsSystem && PHP_INT_SIZE < 8) {
27+
$this->markTestSkipped('Unable to run 64 bits code on 32 bits system.');
28+
}
29+
2630
$baseTimestamp = strtotime('2025-01-01');
2731
$resolver = $this->createMock(NextcloudSequenceResolver::class);
2832
$resolver->method('isAvailable')->willReturn(true);

0 commit comments

Comments
 (0)