Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 7f4390c

Browse files
Merge pull request #13 from michaeldyrynda/feature/separate-field-type
Separate field type
2 parents 621236b + 80a80e9 commit 7f4390c

10 files changed

+69
-110
lines changed

README.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Laravel Efficient UUIDs
2-
## v2.3.0
2+
## v3.0.0
33

44
[![Build Status](https://travis-ci.org/michaeldyrynda/laravel-efficient-uuid.svg?branch=master)](https://travis-ci.org/michaeldyrynda/laravel-efficient-uuid)
55
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-efficient-uuid/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-efficient-uuid/?branch=master)
@@ -10,7 +10,9 @@
1010

1111
## Introduction
1212

13-
This package simply overrides the default grammar file for the given connection making the `uuid()` blueprint method return a `binary(16)` rather than the default `char(36)`.
13+
This package extends the default grammar file for the given MySQL connection adding an `efficientUuid` blueprint method that creates a `binary(16)` field.
14+
15+
As of 3.0, this package _no longer overrides_ Laravel's default `uuid` method, but rather adds a separate `efficientUuid` field, due to compatibility issues with Laravel Telescope (#11).
1416

1517
> **Note**: This package purposely does not use [package discovery](https://laravel.com/docs/5.8/packages#package-discovery), as it makes changes to the MySQL schema file, which is something you should explicitly enable.
1618
@@ -22,18 +24,6 @@ For more information, check out [this post](https://www.percona.com/blog/2014/12
2224

2325
Using UUIDs in Laravel is made super simple in combination with [laravel-model-uuid](https://github.com/michaeldyrynda/laravel-model-uuid). Note that when using `laravel-model-uuid`, if you are not casting your UUIDs or calling the query builder directly, you'll need to use the `getBytes` method when setting the UUID on the database, otherwise your values will be truncated. Depending on your MySQL/MariaDB configuration, this may lead to application errors due to strict settings. See #1 for more information.
2426

25-
### Version compatibility
26-
27-
Laravel | Package
28-
:-------|:--------
29-
5.4.* | 1.0.*
30-
5.5.* | 2.0.*
31-
5.6.* | 2.1.*
32-
5.7.* | 2.2.*
33-
5.8.* | 2.3.*
34-
35-
## Installation
36-
3727
This package is installed via [Composer](https://getcomposer.org/). To install, run the following command.
3828

3929
```bash
@@ -54,7 +44,7 @@ There is nothing special needed for this to function, simply declare a `uuid` co
5444
```php
5545
Schema::create('posts', function (Blueprint $table) {
5646
$table->increments('id');
57-
$table->uuid('uuid')->index();
47+
$table->efficientUuid('uuid')->index();
5848
$table->string('title');
5949
$table->text('body');
6050
$table->timestamps();
@@ -68,8 +58,8 @@ You will need to add a cast to your model when using [laravel-model-uuid](https:
6858

6959
namespace App;
7060

71-
use Dyrynda\Database\Support\GeneratesUuid;
7261
use Illuminate\Database\Eloquent\Model;
62+
use Dyrynda\Database\Support\GeneratesUuid;
7363

7464
class Post extends Model
7565
{

composer.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
{
22
"name": "dyrynda/laravel-efficient-uuid",
3-
"description": "A package to override Laravel migrations to more efficiently store UUID fields in your database",
43
"type": "utility",
4+
"description": "A package to extend Laravel migrations adding a more efficient storage of UUID fields in your database",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Michael Dyrynda",
9+
"email": "[email protected]",
10+
"homepage": "https://dyrynda.com.au"
11+
}
12+
],
513
"require": {
614
"php": "^7.2",
715
"illuminate/container": "^6.0",
8-
"illuminate/database": "^6.0"
16+
"illuminate/database": "^6.0",
17+
"orchestra/testbench": "^4.0"
918
},
1019
"require-dev": {
1120
"phpunit/phpunit": "^8.0",
@@ -21,13 +30,5 @@
2130
"Tests\\": "tests/"
2231
}
2332
},
24-
"license": "MIT",
25-
"authors": [
26-
{
27-
"name": "Michael Dyrynda",
28-
"email": "[email protected]",
29-
"homepage": "https://dyrynda.com.au"
30-
}
31-
],
3233
"minimum-stability": "stable"
3334
}

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
45
colors="true"
56
convertErrorsToExceptions="true"
67
convertNoticesToExceptions="true"

src/Blueprint.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Connection/MySqlConnection.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/LaravelEfficientUuidServiceProvider.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace Dyrynda\Database;
44

5-
use Dyrynda\Database\Connection\MySqlConnection;
6-
use Illuminate\Database\Connection;
75
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Database\Schema\Blueprint;
87

98
class LaravelEfficientUuidServiceProvider extends ServiceProvider
109
{
11-
1210
/**
1311
* Bootstrap any application services.
1412
*
@@ -19,16 +17,15 @@ public function boot()
1917
//
2018
}
2119

22-
2320
/**
2421
* Register any application services.
2522
*
2623
* @return void
2724
*/
2825
public function register()
2926
{
30-
Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
31-
return new MySqlConnection($connection, $database, $prefix, $config);
27+
Blueprint::macro('efficientUuid', function ($column) {
28+
$this->addColumn('efficientUuid', $column);
3229
});
3330
}
3431
}

src/Schema/Grammars/MySqlGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class MySqlGrammar extends BaseMySqlGrammar
1414
*
1515
* @return string
1616
*/
17-
protected function typeUuid(Fluent $column)
17+
protected function typeEfficientUuid(Fluent $column)
1818
{
19-
return 'binary(16)';
19+
return sprintf('binary(%d)', $column->length ?? 16);
2020
}
2121
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Mockery as m;
6+
use Illuminate\Database\Connection;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Dyrynda\Database\Schema\Grammars\MySqlGrammar;
9+
10+
class DatabaseMySqlSchemaGrammarTest extends TestCase
11+
{
12+
public function tearDown(): void
13+
{
14+
m::close();
15+
}
16+
17+
public function testAddingUuid()
18+
{
19+
$blueprint = new Blueprint('users', function ($table) {
20+
$table->uuid('foo');
21+
$table->efficientUuid('bar');
22+
});
23+
24+
$connection = m::mock(Connection::class);
25+
26+
$this->assertEquals(
27+
['alter table `users` add `foo` char(36) not null, add `bar` binary(16) not null'],
28+
$blueprint->toSql($connection, new MySqlGrammar)
29+
);
30+
}
31+
}

tests/TestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Dyrynda\Database\LaravelEfficientUuidServiceProvider;
6+
7+
class TestCase extends \Orchestra\Testbench\TestCase
8+
{
9+
protected function getPackageProviders($app)
10+
{
11+
return [
12+
LaravelEfficientUuidServiceProvider::class,
13+
];
14+
}
15+
}

tests/Tests/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)