-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Migrate efficient UUID support (#130)
* Merge dyrynda/laravel-efficient-uuid into this package It has long-since been a maintenance problem to have the two packages separately and dependant on each other. This will combine them into one package, simplifying maintenance and testing. dyrynda/laravel-efficient-uuid has been marked abandoned, suggesting use of this package. * fix namespaces * typo * tweaks to make sure we're using the right fields types in right places in testing * don't cast uuid on efficient uuid post * don't cast custom_uuid on efficient uuid post
- Loading branch information
1 parent
bc0ba23
commit efb9dd3
Showing
14 changed files
with
335 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Dyrynda\Database\Support\Casts; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class EfficientUuid implements CastsAttributes | ||
{ | ||
/** | ||
* Transform the attribute from the underlying model values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function get($model, string $key, $value, array $attributes) | ||
{ | ||
if (blank($value)) { | ||
return; | ||
} | ||
|
||
return Uuid::fromBytes($value)->toString(); | ||
} | ||
|
||
/** | ||
* Transform the attribute to its underlying model values. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param mixed $value | ||
* @return array | ||
*/ | ||
public function set($model, string $key, $value, array $attributes) | ||
{ | ||
if (blank($value)) { | ||
return; | ||
} | ||
|
||
return [ | ||
$key => Uuid::fromString(strtolower($value))->getBytes(), | ||
]; | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Dyrynda\Database\Support\Exceptions; | ||
|
||
use Exception; | ||
|
||
class UnknownGrammarClass extends Exception | ||
{ | ||
/** @var string */ | ||
protected $message = 'Unknown grammar class, unable to define database type.'; | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Dyrynda\Database\Support\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class EfficientUuidExists implements Rule | ||
{ | ||
/** @var \Dyrynda\Database\Support\GeneratesUuid */ | ||
protected $model; | ||
|
||
/** @var string */ | ||
protected $column; | ||
|
||
public function __construct(string $model, string $column = 'uuid') | ||
{ | ||
$this->model = new $model; | ||
|
||
$this->column = $column; | ||
} | ||
|
||
public function passes($attribute, $value): bool | ||
{ | ||
if (Uuid::isValid($value ?: '')) { | ||
$binaryUuid = Uuid::fromString(strtolower($value))->getBytes(); | ||
|
||
return $this->model->where($this->column, $binaryUuid)->exists(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return trans('validation.exists'); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Dyrynda\Database\Support\Exceptions\UnknownGrammarClass; | ||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Schema\Grammars\SqlServerGrammar; | ||
use Mockery as m; | ||
use Tests\TestCase; | ||
|
||
class DatabaseInvalidSchemaGrammarTest extends TestCase | ||
{ | ||
public function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testAddingUuid() | ||
{ | ||
$blueprint = new Blueprint('users', function ($table) { | ||
$table->uuid('foo'); | ||
$table->efficientUuid('bar'); | ||
}); | ||
|
||
$connection = m::mock(Connection::class); | ||
|
||
$this->expectExceptionObject(new UnknownGrammarClass()); | ||
|
||
$blueprint->toSql($connection, new SqlServerGrammar); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Schema\Grammars\MySqlGrammar; | ||
use Mockery as m; | ||
use Tests\TestCase; | ||
|
||
class DatabaseMySqlSchemaGrammarTest extends TestCase | ||
{ | ||
public function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testAddingUuid() | ||
{ | ||
$blueprint = new Blueprint('users', function ($table) { | ||
$table->uuid('foo'); | ||
$table->efficientUuid('bar'); | ||
}); | ||
|
||
$connection = m::mock(Connection::class); | ||
|
||
$this->assertEquals( | ||
['alter table `users` add `foo` char(36) not null, add `bar` binary(16) not null'], | ||
$blueprint->toSql($connection, new MySqlGrammar) | ||
); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Schema\Grammars\PostgresGrammar; | ||
use Mockery as m; | ||
use Tests\TestCase; | ||
|
||
class DatabasePostgresSchemaGrammarTest extends TestCase | ||
{ | ||
public function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testAddingUuid() | ||
{ | ||
$blueprint = new Blueprint('users', function ($table) { | ||
$table->uuid('foo'); | ||
$table->efficientUuid('bar'); | ||
}); | ||
|
||
$connection = m::mock(Connection::class); | ||
|
||
$this->assertEquals( | ||
['alter table "users" add column "foo" uuid not null, add column "bar" bytea not null'], | ||
$blueprint->toSql($connection, new PostgresGrammar) | ||
); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Schema\Grammars\SQLiteGrammar; | ||
use Mockery as m; | ||
use Tests\TestCase; | ||
|
||
class DatabaseSQLiteSchemaGrammarTest extends TestCase | ||
{ | ||
public function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testAddingUuid() | ||
{ | ||
$blueprint = new Blueprint('users', function ($table) { | ||
$table->uuid('foo'); | ||
$table->efficientUuid('bar'); | ||
}); | ||
|
||
$connection = m::mock(Connection::class); | ||
|
||
$this->assertEquals( | ||
['alter table "users" add column "foo" varchar not null', 'alter table "users" add column "bar" blob(256) not null'], | ||
$blueprint->toSql($connection, new SQLiteGrammar) | ||
); | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Dyrynda\Database\Support\Rules\EfficientUuidExists; | ||
use Ramsey\Uuid\Uuid; | ||
use Tests\Fixtures\EfficientUuidPost; | ||
use Tests\TestCase; | ||
|
||
class EfficientUuidExistsRuleTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_passes_valid_existing_uuid() | ||
{ | ||
/** @var \Tests\Fixtures\EfficientUuidPost $post */ | ||
$post = factory(EfficientUuidPost::class)->create(); | ||
|
||
$rule = new EfficientUuidExists(EfficientUuidPost::class, 'efficient_uuid'); | ||
|
||
$this->assertTrue($rule->passes('efficient_uuid', $post->efficient_uuid)); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_on_non_existing_uuid() | ||
{ | ||
$uuid = Uuid::uuid4(); | ||
|
||
$rule = new EfficientUuidExists(EfficientUuidPost::class); | ||
|
||
$this->assertFalse($rule->passes('post_id', $uuid)); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_on_any_non_uuid_invalid_strings() | ||
{ | ||
$uuid = '1235123564354633'; | ||
|
||
$rule = new EfficientUuidExists(EfficientUuidPost::class, 'uuid'); | ||
|
||
$this->assertFalse($rule->passes('post_id', $uuid)); | ||
} | ||
|
||
/** @test */ | ||
public function it_works_with_custom_uuid_column_name() | ||
{ | ||
/** @var \Tests\Fixtures\EfficientUuidPost $post */ | ||
$post = factory(EfficientUuidPost::class)->create(); | ||
|
||
$rule = new EfficientUuidExists(EfficientUuidPost::class, 'custom_efficient_uuid'); | ||
|
||
$this->assertTrue($rule->passes('custom_efficient_uuid', $post->custom_efficient_uuid)); | ||
} | ||
} |
Oops, something went wrong.