Skip to content

Commit

Permalink
Merge pull request michaeldyrynda#13 from michaeldyrynda/feature/sepa…
Browse files Browse the repository at this point in the history
…rate-field-type

Separate field type
  • Loading branch information
michaeldyrynda authored Sep 5, 2019
2 parents 621236b + 80a80e9 commit 7f4390c
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 110 deletions.
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Laravel Efficient UUIDs
## v2.3.0
## v3.0.0

[![Build Status](https://travis-ci.org/michaeldyrynda/laravel-efficient-uuid.svg?branch=master)](https://travis-ci.org/michaeldyrynda/laravel-efficient-uuid)
[![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)
Expand All @@ -10,7 +10,9 @@

## Introduction

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)`.
This package extends the default grammar file for the given MySQL connection adding an `efficientUuid` blueprint method that creates a `binary(16)` field.

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).

> **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.
Expand All @@ -22,18 +24,6 @@ For more information, check out [this post](https://www.percona.com/blog/2014/12

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.

### Version compatibility

Laravel | Package
:-------|:--------
5.4.* | 1.0.*
5.5.* | 2.0.*
5.6.* | 2.1.*
5.7.* | 2.2.*
5.8.* | 2.3.*

## Installation

This package is installed via [Composer](https://getcomposer.org/). To install, run the following command.

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

namespace App;

use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
use Dyrynda\Database\Support\GeneratesUuid;

class Post extends Model
{
Expand Down
21 changes: 11 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{
"name": "dyrynda/laravel-efficient-uuid",
"description": "A package to override Laravel migrations to more efficiently store UUID fields in your database",
"type": "utility",
"description": "A package to extend Laravel migrations adding a more efficient storage of UUID fields in your database",
"license": "MIT",
"authors": [
{
"name": "Michael Dyrynda",
"email": "[email protected]",
"homepage": "https://dyrynda.com.au"
}
],
"require": {
"php": "^7.2",
"illuminate/container": "^6.0",
"illuminate/database": "^6.0"
"illuminate/database": "^6.0",
"orchestra/testbench": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
Expand All @@ -21,13 +30,5 @@
"Tests\\": "tests/"
}
},
"license": "MIT",
"authors": [
{
"name": "Michael Dyrynda",
"email": "[email protected]",
"homepage": "https://dyrynda.com.au"
}
],
"minimum-stability": "stable"
}
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
22 changes: 0 additions & 22 deletions src/Blueprint.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Connection/MySqlConnection.php

This file was deleted.

9 changes: 3 additions & 6 deletions src/LaravelEfficientUuidServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Dyrynda\Database;

use Dyrynda\Database\Connection\MySqlConnection;
use Illuminate\Database\Connection;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Schema\Blueprint;

class LaravelEfficientUuidServiceProvider extends ServiceProvider
{

/**
* Bootstrap any application services.
*
Expand All @@ -19,16 +17,15 @@ public function boot()
//
}


/**
* Register any application services.
*
* @return void
*/
public function register()
{
Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
return new MySqlConnection($connection, $database, $prefix, $config);
Blueprint::macro('efficientUuid', function ($column) {
$this->addColumn('efficientUuid', $column);
});
}
}
4 changes: 2 additions & 2 deletions src/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class MySqlGrammar extends BaseMySqlGrammar
*
* @return string
*/
protected function typeUuid(Fluent $column)
protected function typeEfficientUuid(Fluent $column)
{
return 'binary(16)';
return sprintf('binary(%d)', $column->length ?? 16);
}
}
31 changes: 31 additions & 0 deletions tests/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests;

use Mockery as m;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;
use Dyrynda\Database\Schema\Grammars\MySqlGrammar;

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)
);
}
}
15 changes: 15 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests;

use Dyrynda\Database\LaravelEfficientUuidServiceProvider;

class TestCase extends \Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
return [
LaravelEfficientUuidServiceProvider::class,
];
}
}
35 changes: 0 additions & 35 deletions tests/Tests/DatabaseMySqlSchemaGrammarTest.php

This file was deleted.

0 comments on commit 7f4390c

Please sign in to comment.