Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[9.x] Added missing morphs methods for the ULID support #44364

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

Merged
merged 2 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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