Giter Site home page Giter Site logo

Comments (12)

brendt avatar brendt commented on May 14, 2024 2

Turns out Blueprint is macroable. So we're able to add a dedicated method for creating a binary UUID column. I've pushed a branch called v2, containing this change: a3fada6

We'll be able to use this solution if there's no built in binary support once Laravel 5.6 arrives.

from laravel-binary-uuid.

freekmurze avatar freekmurze commented on May 14, 2024

I like the idea, but I don't see how we can implement this now in a non breaking way. Closing for now, but will take a look again when creating a new major version

from laravel-binary-uuid.

faustbrian avatar faustbrian commented on May 14, 2024

What would you recommend as a workaround then? Currently you can either use this package for primary keys and use unoptimised field types for other UUID fields or just don't use this package at all.

from laravel-binary-uuid.

freekmurze avatar freekmurze commented on May 14, 2024

Let's do this is in a non breaking way. Let's leave typeUuid as it is for new and add typeUuidText.

In the next major version we could remove the override of typeUuid and add typeUuidBinary.

I'd accept a PR for this.

from laravel-binary-uuid.

faustbrian avatar faustbrian commented on May 14, 2024

#15
#16

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

There's a little bit more to it than this I'm afraid.

Adding a custom type was also the first solution I went for, but it only solves half the problem.

#15 and #16 add the type to the Grammar classes, but there's no support yet to call that method from the blueprint.

Schema::create('table_name', function (Blueprint $table) {
    $table->uuid('uuid');
    $table->primary('uuid');

    $table->uuidText('other_uuid'); // won't work
});

You could extend Laravel's Blueprint class to add the functionality, but that class internally is always re-created on the fly by Schema. So you'll still never get your Blueprint implementation because Schema internally uses the Laravel implementation.

One possible workaround could be to override the Schema dependency at whole, but that's can have a very big impact on the codebase. I feel this package shouldn't carry that responsibility.

This problem is the exact reason we're overriding the uuid type on the Grammar level. I'd much more prefer to see something like this:

Schema::create('table_name', function (Blueprint $table) {
    $table->uuidBinary('uuid');
});

But it's not possible.

Also note that using the ->binary method on a blueprint actually doesn't create a BINARY field, but rather a BLOB field, which can't be indexed because there's no way to configure its length (which is required for MySQL indices).

The way to add normal UUID fields with the ->char method.

Schema::create('table_name', function (Blueprint $table) {
    $table->char('textual_uuid', 36);
});

The only way to really solve this issue is if there was a possibility to configure which Blueprint class is used, which requires a change in Laravel itself.

For reference, I'm talking about this line in Laravel: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Schema/Builder.php#L268

Oh and about that resolver: you cannot set it from a service provider, because a Builder is always made with new: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Eloquent/Model.php#L904

from laravel-binary-uuid.

faustbrian avatar faustbrian commented on May 14, 2024

This could be a possible solution @brendt

<?php

class MysqlServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('db.connection.mysql', SpatieMySqlConnection::class);
    }
}
<?php

use Illuminate\Database\MySqlConnection as BaseMySqlConnection;
use Illuminate\Database\Schema\MySqlBuilder;

class MySqlConnection extends BaseMySqlConnection
{
    public function getSchemaBuilder()
    {
        if (is_null($this->schemaGrammar)) {
            $this->useDefaultSchemaGrammar();
        }

        $builder = new MySqlBuilder($this);

        $builder->blueprintResolver(function ($table, $callback) {
            return new SpatieBlueprint($table, $callback);
        });

        return $builder;
    }
}
<?php

use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Illuminate\Support\Facades\DB;

class Blueprint extends BaseBlueprint
{
    public function uuid()
    {
        return $this->addColumn('uuid', $column);
    }
    
    public function uuidBinary()
    {
        return $this->addColumn('uuidBinary', $column);
    }
}

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

Yes. Though I'd like to avoid overriding a class like MySqlConnection. It doesn't feel right if this package starts messing with such a key component.

from laravel-binary-uuid.

steve3d avatar steve3d commented on May 14, 2024

agreed with @brendt , you should use $table->char('textual_uuid', 36) to add a normal text uuid.

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

FYI the possibilities to add better support in the core can be found here: laravel/ideas#645

There's no consensus though, yet.

from laravel-binary-uuid.

francislavoie avatar francislavoie commented on May 14, 2024

Mildly related question, if I wanted to extend the grammar myself to add something else, how would I do it such that it's still compatible with binary UUID? I want to add MEDIUMBLOB support to the grammar since I only use MySQL

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

Because Grammar classes are not macroable; the only possibility is to extend Spatie\BinaryUuid\MySqlGrammar and manually register it on the connection.

$connection = app('db')->connection();
$connection->setSchemaGrammar($this->createGrammarFromConnection($connection));

See https://github.com/spatie/laravel-binary-uuid/blob/master/src/UuidServiceProvider.php#L27-L45 for the full implementation we're using.

from laravel-binary-uuid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.