Skip to content

Commit 41d14a6

Browse files
committed
feat: native uuidv7 generation (references #117)
1 parent 9024f15 commit 41d14a6

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,18 @@ Schema::create('comments', function (Blueprint $table) {
13651365
});
13661366
```
13671367

1368+
When using PostgreSQL 18, you can also use the more efficient native implementation within PostgreSQL instead of the one provided by the expression:
1369+
1370+
```php
1371+
use Tpetry\PostgresqlEnhanced\Expressions\Uuid7;
1372+
1373+
Schema::create('comments', function (Blueprint $table) {
1374+
$table->id();
1375+
$table->uuid()->default(new Uuid7(native: true))->unique();
1376+
$table->text('text');
1377+
});
1378+
```
1379+
13681380
# Supported Extensions
13691381

13701382
You can use any extension with this PostgreSQL you like but some have received a deeper Laravel integration.

src/Expressions/Uuid7.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ class Uuid7 implements Expression
1212
{
1313
public function __construct(
1414
private readonly ?DateTimeInterface $time = null,
15+
private readonly bool $native = false,
1516
) {
1617
}
1718

1819
public function getValue(Grammar $grammar): string
1920
{
21+
if ($this->native) {
22+
return match ($this->time) {
23+
null => 'uuidv7()',
24+
default => "uuidv7('{$this->time->format('Y-m-d H:i:s.uP')}'::timestamptz - clock_timestamp())",
25+
};
26+
}
27+
2028
// The UUIDv7 algorithm in pure PostgreSQL SQL is copied from:
2129
// https://gist.github.com/fabiolimace/515a0440e3e40efeb234e12644a6a346#file-uuidv7-sql
2230

tests/Expressions/Uuid7Test.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ protected function setUp(): void
2222
}
2323
}
2424

25-
public function testIncludesTimestampOrClock(): void
25+
public function testNative(): void
26+
{
27+
$time = CarbonImmutable::createFromFormat('Y-m-d H:i:s.uP', '2009-04-23 14:57:17.830618-04:00');
28+
29+
$this->assertEquals('uuidv7()', (new Uuid7(native: true))->getValue($this->getConnection()->getQueryGrammar()));
30+
$this->assertEquals("uuidv7('2009-04-23 14:57:17.830618-04:00'::timestamptz - clock_timestamp())", (new Uuid7($time, native: true))->getValue($this->getConnection()->getQueryGrammar()));
31+
}
32+
33+
public function testReimplementedIncludesTimestampOrClock(): void
2634
{
2735
$uuidNow = (new Uuid7())->getValue($this->getConnection()->getQueryGrammar());
2836
$this->assertStringContainsString('statement_timestamp()', $uuidNow);
@@ -36,7 +44,7 @@ public function testIncludesTimestampOrClock(): void
3644
* Two different expression invocations would always be unique because of different time.
3745
* So the time is fixed to check for the randomness.
3846
*/
39-
public function testIsRandom(): void
47+
public function testReimplementedIsRandom(): void
4048
{
4149
$uuid = new Uuid7(CarbonImmutable::now());
4250

@@ -46,7 +54,7 @@ public function testIsRandom(): void
4654
$this->assertNotEquals($value1, $value2);
4755
}
4856

49-
public function testTimeIncreases(): void
57+
public function testReimplementedTimeIncreases(): void
5058
{
5159
$uuid = new Uuid7();
5260

0 commit comments

Comments
 (0)