Skip to content

Commit f414f9d

Browse files
martioMarcin Lewandowskitaylorotwell
authored
[9.x] Added missing morphs methods for the ULID support (#44364)
* Added missing morphs methods for the ULID support * Update Builder.php Co-authored-by: Marcin Lewandowski <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 71f2154 commit f414f9d

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,8 @@ public function morphs($name, $indexName = null)
14401440
{
14411441
if (Builder::$defaultMorphKeyType === 'uuid') {
14421442
$this->uuidMorphs($name, $indexName);
1443+
} elseif (Builder::$defaultMorphKeyType === 'ulid') {
1444+
$this->ulidMorphs($name, $indexName);
14431445
} else {
14441446
$this->numericMorphs($name, $indexName);
14451447
}
@@ -1456,6 +1458,8 @@ public function nullableMorphs($name, $indexName = null)
14561458
{
14571459
if (Builder::$defaultMorphKeyType === 'uuid') {
14581460
$this->nullableUuidMorphs($name, $indexName);
1461+
} elseif (Builder::$defaultMorphKeyType === 'ulid') {
1462+
$this->nullableUlidMorphs($name, $indexName);
14591463
} else {
14601464
$this->nullableNumericMorphs($name, $indexName);
14611465
}
@@ -1525,6 +1529,38 @@ public function nullableUuidMorphs($name, $indexName = null)
15251529
$this->index(["{$name}_type", "{$name}_id"], $indexName);
15261530
}
15271531

1532+
/**
1533+
* Add the proper columns for a polymorphic table using ULIDs.
1534+
*
1535+
* @param string $name
1536+
* @param string|null $indexName
1537+
* @return void
1538+
*/
1539+
public function ulidMorphs($name, $indexName = null)
1540+
{
1541+
$this->string("{$name}_type");
1542+
1543+
$this->ulid("{$name}_id");
1544+
1545+
$this->index(["{$name}_type", "{$name}_id"], $indexName);
1546+
}
1547+
1548+
/**
1549+
* Add nullable columns for a polymorphic table using ULIDs.
1550+
*
1551+
* @param string $name
1552+
* @param string|null $indexName
1553+
* @return void
1554+
*/
1555+
public function nullableUlidMorphs($name, $indexName = null)
1556+
{
1557+
$this->string("{$name}_type")->nullable();
1558+
1559+
$this->ulid("{$name}_id")->nullable();
1560+
1561+
$this->index(["{$name}_type", "{$name}_id"], $indexName);
1562+
}
1563+
15281564
/**
15291565
* Adds the `remember_token` column to the table.
15301566
*

src/Illuminate/Database/Schema/Builder.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public static function defaultStringLength($length)
7878
*/
7979
public static function defaultMorphKeyType(string $type)
8080
{
81-
if (! in_array($type, ['int', 'uuid'])) {
82-
throw new InvalidArgumentException("Morph key type must be 'int' or 'uuid'.");
81+
if (! in_array($type, ['int', 'uuid', 'ulid'])) {
82+
throw new InvalidArgumentException("Morph key type must be 'int', 'uuid', or 'ulid'.");
8383
}
8484

8585
static::$defaultMorphKeyType = $type;
@@ -95,6 +95,16 @@ public static function morphUsingUuids()
9595
return static::defaultMorphKeyType('uuid');
9696
}
9797

98+
/**
99+
* Set the default morph key type for migrations to ULIDs.
100+
*
101+
* @return void
102+
*/
103+
public static function morphUsingUlids()
104+
{
105+
return static::defaultMorphKeyType('ulid');
106+
}
107+
98108
/**
99109
* Create a database in the schema.
100110
*

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,42 @@ public function testDefaultUsingNullableUuidMorph()
260260
], $blueprint->toSql($connection, new MySqlGrammar));
261261
}
262262

263+
public function testDefaultUsingUlidMorph()
264+
{
265+
Builder::defaultMorphKeyType('ulid');
266+
267+
$base = new Blueprint('comments', function ($table) {
268+
$table->morphs('commentable');
269+
});
270+
271+
$connection = m::mock(Connection::class);
272+
273+
$blueprint = clone $base;
274+
275+
$this->assertEquals([
276+
'alter table `comments` add `commentable_type` varchar(255) not null, add `commentable_id` char(26) not null',
277+
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
278+
], $blueprint->toSql($connection, new MySqlGrammar));
279+
}
280+
281+
public function testDefaultUsingNullableUlidMorph()
282+
{
283+
Builder::defaultMorphKeyType('ulid');
284+
285+
$base = new Blueprint('comments', function ($table) {
286+
$table->nullableMorphs('commentable');
287+
});
288+
289+
$connection = m::mock(Connection::class);
290+
291+
$blueprint = clone $base;
292+
293+
$this->assertEquals([
294+
'alter table `comments` add `commentable_type` varchar(255) null, add `commentable_id` char(26) null',
295+
'alter table `comments` add index `comments_commentable_type_commentable_id_index`(`commentable_type`, `commentable_id`)',
296+
], $blueprint->toSql($connection, new MySqlGrammar));
297+
}
298+
263299
public function testGenerateRelationshipColumnWithIncrementalModel()
264300
{
265301
$base = new Blueprint('posts', function ($table) {

0 commit comments

Comments
 (0)