Skip to content

Commit

Permalink
[9.x] Added missing morphs methods for the ULID support (#44364)
Browse files Browse the repository at this point in the history
* Added missing morphs methods for the ULID support

* Update Builder.php

Co-authored-by: Marcin Lewandowski <m.lewandowski@upacjenta.pl>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people authored Sep 29, 2022
1 parent 71f2154 commit f414f9d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,8 @@ public function morphs($name, $indexName = null)
{
if (Builder::$defaultMorphKeyType === 'uuid') {
$this->uuidMorphs($name, $indexName);
} elseif (Builder::$defaultMorphKeyType === 'ulid') {
$this->ulidMorphs($name, $indexName);
} else {
$this->numericMorphs($name, $indexName);
}
Expand All @@ -1456,6 +1458,8 @@ public function nullableMorphs($name, $indexName = null)
{
if (Builder::$defaultMorphKeyType === 'uuid') {
$this->nullableUuidMorphs($name, $indexName);
} elseif (Builder::$defaultMorphKeyType === 'ulid') {
$this->nullableUlidMorphs($name, $indexName);
} else {
$this->nullableNumericMorphs($name, $indexName);
}
Expand Down Expand Up @@ -1525,6 +1529,38 @@ public function nullableUuidMorphs($name, $indexName = null)
$this->index(["{$name}_type", "{$name}_id"], $indexName);
}

/**
* Add the proper columns for a polymorphic table using ULIDs.
*
* @param string $name
* @param string|null $indexName
* @return void
*/
public function ulidMorphs($name, $indexName = null)
{
$this->string("{$name}_type");

$this->ulid("{$name}_id");

$this->index(["{$name}_type", "{$name}_id"], $indexName);
}

/**
* Add nullable columns for a polymorphic table using ULIDs.
*
* @param string $name
* @param string|null $indexName
* @return void
*/
public function nullableUlidMorphs($name, $indexName = null)
{
$this->string("{$name}_type")->nullable();

$this->ulid("{$name}_id")->nullable();

$this->index(["{$name}_type", "{$name}_id"], $indexName);
}

/**
* Adds the `remember_token` column to the table.
*
Expand Down
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static function defaultStringLength($length)
*/
public static function defaultMorphKeyType(string $type)
{
if (! in_array($type, ['int', 'uuid'])) {
throw new InvalidArgumentException("Morph key type must be 'int' or 'uuid'.");
if (! in_array($type, ['int', 'uuid', 'ulid'])) {
throw new InvalidArgumentException("Morph key type must be 'int', 'uuid', or 'ulid'.");
}

static::$defaultMorphKeyType = $type;
Expand All @@ -95,6 +95,16 @@ public static function morphUsingUuids()
return static::defaultMorphKeyType('uuid');
}

/**
* Set the default morph key type for migrations to ULIDs.
*
* @return void
*/
public static function morphUsingUlids()
{
return static::defaultMorphKeyType('ulid');
}

/**
* Create a database in the schema.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,42 @@ public function testDefaultUsingNullableUuidMorph()
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testDefaultUsingUlidMorph()
{
Builder::defaultMorphKeyType('ulid');

$base = new Blueprint('comments', function ($table) {
$table->morphs('commentable');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` char(26) not null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testDefaultUsingNullableUlidMorph()
{
Builder::defaultMorphKeyType('ulid');

$base = new Blueprint('comments', function ($table) {
$table->nullableMorphs('commentable');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;

$this->assertEquals([
'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` char(26) null',
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testGenerateRelationshipColumnWithIncrementalModel()
{
$base = new Blueprint('posts', function ($table) {
Expand Down

0 comments on commit f414f9d

Please sign in to comment.