Skip to content

Commit 29907b2

Browse files
authored
[11.x] Introduce method Blueprint::rawColumn() (#53496)
* [11.x] Introduce method `Blueprint::custom()` * Fix test * Change name to `rawColumn` & parameter to `definition` * Add support for callback * Add support for callback * Revert support for callback
1 parent 47e5f76 commit 29907b2

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/Illuminate/Database/Schema/Blueprint.php

+12
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,18 @@ public function rememberToken()
16141614
return $this->string('remember_token', 100)->nullable();
16151615
}
16161616

1617+
/**
1618+
* Create a new custom column on the table.
1619+
*
1620+
* @param string $column
1621+
* @param string $definition
1622+
* @return \Illuminate\Database\Schema\ColumnDefinition
1623+
*/
1624+
public function rawColumn($column, $definition)
1625+
{
1626+
return $this->addColumn('raw', $column, compact('definition'));
1627+
}
1628+
16171629
/**
16181630
* Add a comment to the table.
16191631
*

src/Illuminate/Database/Schema/Grammars/Grammar.php

+11
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ protected function typeVector(Fluent $column)
247247
throw new RuntimeException('This database driver does not support the vector type.');
248248
}
249249

250+
/**
251+
* Create the column definition for a raw column type.
252+
*
253+
* @param \Illuminate\Support\Fluent $column
254+
* @return string
255+
*/
256+
protected function typeRaw(Fluent $column)
257+
{
258+
return $column->offsetGet('definition');
259+
}
260+
250261
/**
251262
* Add the column modifiers to the definition.
252263
*

tests/Database/DatabaseSchemaBlueprintTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,36 @@ public function testTinyTextNullableColumn()
603603
], $blueprint->toSql($connection, new SqlServerGrammar));
604604
}
605605

606+
public function testRawColumn()
607+
{
608+
$base = new Blueprint('posts', function ($table) {
609+
$table->rawColumn('legacy_boolean', 'INT(1)')->nullable();
610+
});
611+
612+
$connection = m::mock(Connection::class);
613+
614+
$blueprint = clone $base;
615+
$this->assertEquals([
616+
'alter table `posts` add `legacy_boolean` INT(1) null',
617+
], $blueprint->toSql($connection, new MySqlGrammar));
618+
619+
$blueprint = clone $base;
620+
$connection->shouldReceive('getServerVersion')->andReturn('3.35');
621+
$this->assertEquals([
622+
'alter table "posts" add column "legacy_boolean" INT(1)',
623+
], $blueprint->toSql($connection, new SQLiteGrammar));
624+
625+
$blueprint = clone $base;
626+
$this->assertEquals([
627+
'alter table "posts" add column "legacy_boolean" INT(1) null',
628+
], $blueprint->toSql($connection, new PostgresGrammar));
629+
630+
$blueprint = clone $base;
631+
$this->assertEquals([
632+
'alter table "posts" add "legacy_boolean" INT(1) null',
633+
], $blueprint->toSql($connection, new SqlServerGrammar));
634+
}
635+
606636
public function testTableComment()
607637
{
608638
$base = new Blueprint('posts', function (Blueprint $table) {

0 commit comments

Comments
 (0)