Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Improve UUID and ULID support for Eloquent #44146

Merged
merged 6 commits into from
Sep 16, 2022
Merged

Conversation

driesvints
Copy link
Member

@driesvints driesvints commented Sep 15, 2022

Improves upon #44074 by:

  • Allowing to more easily use the trait to just be used on non-primary key columns
  • Adds a special ulid column on Blueprint that creates a char(26) behind the scenes
  • Automatically sets incrementing to false when a string type primary key is used
  • Adds more tests

I was doubting on checking to see if I could add support to use both traits at the same time (so you could both fill in UUID and ULID columns) but I guess that scenario isn't so common.

if ($this->getKeyType() === 'string') {
return false;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need to be rethought. In my mind a string key type column can never be incrementing but I might be wrong.

@driesvints driesvints marked this pull request as ready for review September 15, 2022 15:34
$query = m::mock(Builder::class);
$query->shouldReceive('insertGetId')->once()->with([], 'id')->andReturn('string id');
$query->shouldReceive('insert')->once()->with(['id' => 'string id']);
$query->shouldReceive('getConnection')->once();
$model->expects($this->once())->method('newModelQuery')->willReturn($query);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to rewrite the test here because of the change I did in the getIncrementing method. I think the current test wasn't realistic as I can't see how you can increment with string-based keys. With string-based keys you always need to set them beforehand afaik (like I adjusted the test to).

@driesvints driesvints changed the title [9.x] Improvde UUID and ULID support for Eloquent [9.x] Improve UUID and ULID support for Eloquent Sep 15, 2022
@taylorotwell
Copy link
Member

taylorotwell commented Sep 15, 2022

@driesvints why was the change to getKeyType not needed on HasUlids too?

@driesvints
Copy link
Member Author

driesvints commented Sep 16, 2022

@taylorotwell whoops, you're right. Pushed.

I first was thinking of doing it through a property but when you apply a trait with a property directly on a model you cannot overwrite it. The current solution is way better and more frictionless :)

@taylorotwell taylorotwell merged commit 9235af3 into 9.x Sep 16, 2022
@taylorotwell taylorotwell deleted the improve-uuid-support branch September 16, 2022 15:20
@martio
Copy link
Contributor

martio commented Sep 22, 2022

Good job! 👍

@martio
Copy link
Contributor

martio commented Sep 22, 2022

Maybe it's nice to add support for a binary column type for UUID? 😏

@The-Hasanov
Copy link

The-Hasanov commented Sep 22, 2022

Hi @driesvints . This update break jenssegers/laravel-mongodb
mongodb/laravel-mongodb#2451

public function getIncrementing()
{
    if ($this->getKeyType() === 'string') {
         return false;
       }

      return $this->incrementing;
 }

@driesvints
Copy link
Member Author

@The-Hasanov I replied on the related PR fix: mongodb/laravel-mongodb#2452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants