-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] UUID and ULID support for Eloquent (#44074)
* Eloquen UUID & ULID * wip * wip * Update tests/Integration/Database/EloquentPrimaryUlidTest.php * wip * wip * wip * wip * tweaking Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
65693c0
commit 93ff1bc
Showing
7 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
trait HasUlids | ||
{ | ||
/** | ||
* Boot the trait. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootHasUlids() | ||
{ | ||
static::creating(function (self $model) { | ||
foreach ($model->uniqueIds() as $column) { | ||
if (empty($model->{$column})) { | ||
$model->{$column} = $model->newUniqueId(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Generate a new ULID for the model. | ||
* | ||
* @return string | ||
*/ | ||
public function newUniqueId() | ||
{ | ||
return strtolower((string) Str::ulid()); | ||
} | ||
|
||
/** | ||
* Get the columns that should receive a unique identifier. | ||
* | ||
* @return array | ||
*/ | ||
public function uniqueIds() | ||
{ | ||
return [$this->getKeyName()]; | ||
} | ||
|
||
/** | ||
* Get the auto-incrementing key type. | ||
* | ||
* @return string | ||
*/ | ||
public function getKeyType() | ||
{ | ||
return 'string'; | ||
} | ||
|
||
/** | ||
* Get the value indicating whether the IDs are incrementing. | ||
* | ||
* @return bool | ||
*/ | ||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
trait HasUuids | ||
{ | ||
/** | ||
* Generate a primary UUID for the model. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootHasUuids() | ||
{ | ||
static::creating(function (self $model) { | ||
foreach ($model->uniqueIds() as $column) { | ||
if (empty($model->{$column})) { | ||
$model->{$column} = $model->newUniqueId(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Generate a new UUID for the model. | ||
* | ||
* @return string | ||
*/ | ||
public function newUniqueId() | ||
{ | ||
return (string) Str::orderedUuid(); | ||
} | ||
|
||
/** | ||
* Get the columns that should receive a unique identifier. | ||
* | ||
* @return array | ||
*/ | ||
public function uniqueIds() | ||
{ | ||
return [$this->getKeyName()]; | ||
} | ||
|
||
/** | ||
* Get the auto-incrementing key type. | ||
* | ||
* @return string | ||
*/ | ||
public function getKeyType() | ||
{ | ||
return 'string'; | ||
} | ||
|
||
/** | ||
* Get the value indicating whether the IDs are incrementing. | ||
* | ||
* @return bool | ||
*/ | ||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUlids; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
class EloquentUlidPrimaryKeyTest extends DatabaseTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->char('id', 26)->primary(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function testUserWithUlidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = UserWithUlidPrimaryKey::create(); | ||
|
||
$this->assertTrue(Str::isUlid($user->id)); | ||
} | ||
} | ||
|
||
class UserWithUlidPrimaryKey extends Eloquent | ||
{ | ||
use HasUlids; | ||
|
||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
|
||
class EloquentUuidPrimaryKeyTest extends DatabaseTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function testUserWithUuidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = UserWithUuidPrimaryKey::create(); | ||
|
||
$this->assertTrue(Str::isUuid($user->id)); | ||
} | ||
} | ||
|
||
class UserWithUuidPrimaryKey extends Eloquent | ||
{ | ||
use HasUuids; | ||
|
||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
} |