-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Improve UUID and ULID support for Eloquent (#44146)
* Add ulid column type to Blueprint * Improve uuid and ulid support * Apply fixes from StyleCI * Adjust test * wip * formatting Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
85ca3fb
commit 9235af3
Showing
8 changed files
with
173 additions
and
94 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
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
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
tests/Integration/Database/EloquentUniqueStringPrimaryKeysTest.php
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,129 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUlids; | ||
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 EloquentUniqueStringPrimaryKeysTest extends DatabaseTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('users', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->uuid('foo'); | ||
$table->uuid('bar'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('posts', function (Blueprint $table) { | ||
$table->ulid('id')->primary(); | ||
$table->ulid('foo'); | ||
$table->ulid('bar'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('songs', function (Blueprint $table) { | ||
$table->id(); | ||
$table->uuid('foo'); | ||
$table->uuid('bar'); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('pictures', function (Blueprint $table) { | ||
$table->uuid('uuid')->primary(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function testModelWithUuidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = ModelWithUuidPrimaryKey::create(); | ||
|
||
$this->assertTrue(Str::isUuid($user->id)); | ||
$this->assertTrue(Str::isUuid($user->foo)); | ||
$this->assertTrue(Str::isUuid($user->bar)); | ||
} | ||
|
||
public function testModelWithUlidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = ModelWithUlidPrimaryKey::create(); | ||
|
||
$this->assertTrue(Str::isUlid($user->id)); | ||
$this->assertTrue(Str::isUlid($user->foo)); | ||
$this->assertTrue(Str::isUlid($user->bar)); | ||
} | ||
|
||
public function testModelWithoutUuidPrimaryKeyCanBeCreated() | ||
{ | ||
$user = ModelWithoutUuidPrimaryKey::create(); | ||
|
||
$this->assertTrue(is_int($user->id)); | ||
$this->assertTrue(Str::isUuid($user->foo)); | ||
$this->assertTrue(Str::isUuid($user->bar)); | ||
} | ||
|
||
public function testModelWithCustomUuidPrimaryKeyNameCanBeCreated() | ||
{ | ||
$user = ModelWithCustomUuidPrimaryKeyName::create(); | ||
|
||
$this->assertTrue(Str::isUuid($user->uuid)); | ||
} | ||
} | ||
|
||
class ModelWithUuidPrimaryKey extends Eloquent | ||
{ | ||
use HasUuids; | ||
|
||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
|
||
public function uniqueIds() | ||
{ | ||
return [$this->getKeyName(), 'foo', 'bar']; | ||
} | ||
} | ||
|
||
class ModelWithUlidPrimaryKey extends Eloquent | ||
{ | ||
use HasUlids; | ||
|
||
protected $table = 'posts'; | ||
|
||
protected $guarded = []; | ||
|
||
public function uniqueIds() | ||
{ | ||
return [$this->getKeyName(), 'foo', 'bar']; | ||
} | ||
} | ||
|
||
class ModelWithoutUuidPrimaryKey extends Eloquent | ||
{ | ||
use HasUuids; | ||
|
||
protected $table = 'songs'; | ||
|
||
protected $guarded = []; | ||
|
||
public function uniqueIds() | ||
{ | ||
return ['foo', 'bar']; | ||
} | ||
} | ||
|
||
class ModelWithCustomUuidPrimaryKeyName extends Eloquent | ||
{ | ||
use HasUuids; | ||
|
||
protected $table = 'pictures'; | ||
|
||
protected $guarded = []; | ||
|
||
protected $primaryKey = 'uuid'; | ||
} |
This file was deleted.
Oops, something went wrong.