Skip to content

Commit

Permalink
[11.x] Implement HasV7Uuids to use with MariaDB native uuid data type (
Browse files Browse the repository at this point in the history
…#52029)

* implement HasV7UUids

* Implement all uuid4 methods to uuid7 for unified experience and add ability to define time for the uuid7

* formatting

* formatting

* remove property

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
Karem-sobhy and taylorotwell authored Jul 18, 2024
1 parent 95107a0 commit c49e901
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasVersion7Uuids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Support\Str;

trait HasVersion7Uuids
{
use HasUuids;

/**
* Generate a new UUID (version 7) for the model.
*
* @return string
*/
public function newUniqueId()
{
return (string) Str::uuid7();
}
}
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,19 @@ public static function uuid()
: Uuid::uuid4();
}

/**
* Generate a UUID (version 7).
*
* @param \DateTimeInterface|null $time
* @return \Ramsey\Uuid\UuidInterface
*/
public static function uuid7($time = null)
{
return static::$uuidFactory
? call_user_func(static::$uuidFactory)
: Uuid::uuid7($time);
}

/**
* Generate a time-ordered UUID.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ public function testUuid()
{
$this->assertInstanceOf(UuidInterface::class, Str::uuid());
$this->assertInstanceOf(UuidInterface::class, Str::orderedUuid());
$this->assertInstanceOf(UuidInterface::class, Str::uuid7());
}

public function testAsciiNull()
Expand Down Expand Up @@ -1354,7 +1355,7 @@ public function testItCanSpecifyASequenceOfUuidsToUtilise()
{
Str::createUuidsUsingSequence([
0 => ($zeroth = Str::uuid()),
1 => ($first = Str::uuid()),
1 => ($first = Str::uuid7()),
// just generate a random one here...
3 => ($third = Str::uuid()),
// continue to generate random uuids...
Expand Down

0 comments on commit c49e901

Please sign in to comment.