From bfb8148a38996ee8ef4af009dc7831ae29dc7ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Wed, 13 Nov 2024 13:22:02 +0100 Subject: [PATCH 1/6] [11.x] Introduce method `Blueprint::custom()` --- src/Illuminate/Database/Schema/Blueprint.php | 12 ++++++++ .../Database/Schema/Grammars/Grammar.php | 11 +++++++ .../Database/DatabaseSchemaBlueprintTest.php | 30 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 28dc472cf252..9a81edf28cac 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -1606,6 +1606,18 @@ public function rememberToken() return $this->string('remember_token', 100)->nullable(); } + /** + * Create a new custom column on the table. + * + * @param string $column + * @param string $statement + * @return \Illuminate\Database\Schema\ColumnDefinition + */ + public function custom($column, $statement) + { + return $this->addColumn('custom', $column, compact('statement')); + } + /** * Add a comment to the table. * diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index 12a4b367146a..08551f55a251 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -247,6 +247,17 @@ protected function typeVector(Fluent $column) throw new RuntimeException('This database driver does not support the vector type.'); } + /** + * Create the column definition for a custom column type. + * + * @param \Illuminate\Support\Fluent $column + * @return string + */ + protected function typeCustom(Fluent $column) + { + return $column->offsetGet('statement'); + } + /** * Add the column modifiers to the definition. * diff --git a/tests/Database/DatabaseSchemaBlueprintTest.php b/tests/Database/DatabaseSchemaBlueprintTest.php index b9048e2a2bed..491cdefb2142 100755 --- a/tests/Database/DatabaseSchemaBlueprintTest.php +++ b/tests/Database/DatabaseSchemaBlueprintTest.php @@ -570,6 +570,36 @@ public function testTinyTextNullableColumn() ], $blueprint->toSql($connection, new SqlServerGrammar)); } + public function testCustomColumn() + { + $base = new Blueprint('posts', function ($table) { + $table->custom('legacy_boolean', 'INT(1)')->nullable(); + }); + + $connection = m::mock(Connection::class); + + $blueprint = clone $base; + $this->assertEquals([ + 'alter table `posts` add `legacy_boolean` INT(1)', + ], $blueprint->toSql($connection, new MySqlGrammar)); + + $blueprint = clone $base; + $connection->shouldReceive('getServerVersion')->andReturn('3.35'); + $this->assertEquals([ + 'alter table "posts" add column "legacy_boolean" INT(1)', + ], $blueprint->toSql($connection, new SQLiteGrammar)); + + $blueprint = clone $base; + $this->assertEquals([ + 'alter table "posts" add column "legacy_boolean" INT(1) null', + ], $blueprint->toSql($connection, new PostgresGrammar)); + + $blueprint = clone $base; + $this->assertEquals([ + 'alter table "posts" add "legacy_boolean" INT(1) null', + ], $blueprint->toSql($connection, new SqlServerGrammar)); + } + public function testTableComment() { $base = new Blueprint('posts', function (Blueprint $table) { From f47dbf9a7791162cb1aa2bf602402f4d1ef9af78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Wed, 13 Nov 2024 13:56:50 +0100 Subject: [PATCH 2/6] Fix test --- tests/Database/DatabaseSchemaBlueprintTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/DatabaseSchemaBlueprintTest.php b/tests/Database/DatabaseSchemaBlueprintTest.php index 491cdefb2142..f1693fdbcc84 100755 --- a/tests/Database/DatabaseSchemaBlueprintTest.php +++ b/tests/Database/DatabaseSchemaBlueprintTest.php @@ -580,7 +580,7 @@ public function testCustomColumn() $blueprint = clone $base; $this->assertEquals([ - 'alter table `posts` add `legacy_boolean` INT(1)', + 'alter table `posts` add `legacy_boolean` INT(1) null', ], $blueprint->toSql($connection, new MySqlGrammar)); $blueprint = clone $base; From 379d0dafc563696c641dcffa20f412ce90c44dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Thu, 14 Nov 2024 20:37:25 +0100 Subject: [PATCH 3/6] Change name to `rawColumn` & parameter to `definition` --- src/Illuminate/Database/Schema/Blueprint.php | 6 +++--- src/Illuminate/Database/Schema/Grammars/Grammar.php | 6 +++--- tests/Database/DatabaseSchemaBlueprintTest.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 9a81edf28cac..1781185e9017 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -1610,12 +1610,12 @@ public function rememberToken() * Create a new custom column on the table. * * @param string $column - * @param string $statement + * @param string $definition * @return \Illuminate\Database\Schema\ColumnDefinition */ - public function custom($column, $statement) + public function rawColumn($column, $definition) { - return $this->addColumn('custom', $column, compact('statement')); + return $this->addColumn('raw', $column, compact('definition')); } /** diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index 08551f55a251..b21fcf23b65d 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -248,14 +248,14 @@ protected function typeVector(Fluent $column) } /** - * Create the column definition for a custom column type. + * Create the column definition for a raw column type. * * @param \Illuminate\Support\Fluent $column * @return string */ - protected function typeCustom(Fluent $column) + protected function typeRaw(Fluent $column) { - return $column->offsetGet('statement'); + return $column->offsetGet('definition'); } /** diff --git a/tests/Database/DatabaseSchemaBlueprintTest.php b/tests/Database/DatabaseSchemaBlueprintTest.php index f1693fdbcc84..1565ebc9c897 100755 --- a/tests/Database/DatabaseSchemaBlueprintTest.php +++ b/tests/Database/DatabaseSchemaBlueprintTest.php @@ -570,10 +570,10 @@ public function testTinyTextNullableColumn() ], $blueprint->toSql($connection, new SqlServerGrammar)); } - public function testCustomColumn() + public function testRawColumn() { $base = new Blueprint('posts', function ($table) { - $table->custom('legacy_boolean', 'INT(1)')->nullable(); + $table->rawColumn('legacy_boolean', 'INT(1)')->nullable(); }); $connection = m::mock(Connection::class); From 47bb20fb9991e9381ad35a9a71d808a00b67be9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Thu, 14 Nov 2024 20:40:25 +0100 Subject: [PATCH 4/6] Add support for callback --- src/Illuminate/Database/Schema/Grammars/Grammar.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index b21fcf23b65d..21bb62ac111f 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -251,11 +251,11 @@ protected function typeVector(Fluent $column) * Create the column definition for a raw column type. * * @param \Illuminate\Support\Fluent $column - * @return string + * @return string|(\Closure(\Illuminate\Database\Connection): string) */ protected function typeRaw(Fluent $column) { - return $column->offsetGet('definition'); + return value($column->offsetGet('definition'), $this->connection); } /** From b46e6c82c5159929805de91932b01bfd402d46a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Thu, 14 Nov 2024 20:42:30 +0100 Subject: [PATCH 5/6] Add support for callback --- .../Database/DatabaseSchemaBlueprintTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/Database/DatabaseSchemaBlueprintTest.php b/tests/Database/DatabaseSchemaBlueprintTest.php index 1565ebc9c897..165428cdea93 100755 --- a/tests/Database/DatabaseSchemaBlueprintTest.php +++ b/tests/Database/DatabaseSchemaBlueprintTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Database; use Illuminate\Database\Connection; +use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Grammars\MariaDbGrammar; @@ -600,6 +601,40 @@ public function testRawColumn() ], $blueprint->toSql($connection, new SqlServerGrammar)); } + public function testRawColumnUsingCallback() + { + $connectionMock = m::mock(Connection::class); + + $base = new Blueprint('posts', function ($table) use ($connectionMock) { + $table->rawColumn('legacy_boolean', function (ConnectionInterface $connection) use ($connectionMock) { + $this->assertSame($connection, $connectionMock); + + return 'INT(1)'; + })->nullable(); + }); + + $blueprint = clone $base; + $this->assertEquals([ + 'alter table `posts` add `legacy_boolean` INT(1) null', + ], $blueprint->toSql($connectionMock, new MySqlGrammar)); + + $blueprint = clone $base; + $connectionMock->shouldReceive('getServerVersion')->andReturn('3.35'); + $this->assertEquals([ + 'alter table "posts" add column "legacy_boolean" INT(1)', + ], $blueprint->toSql($connectionMock, new SQLiteGrammar)); + + $blueprint = clone $base; + $this->assertEquals([ + 'alter table "posts" add column "legacy_boolean" INT(1) null', + ], $blueprint->toSql($connectionMock, new PostgresGrammar)); + + $blueprint = clone $base; + $this->assertEquals([ + 'alter table "posts" add "legacy_boolean" INT(1) null', + ], $blueprint->toSql($connectionMock, new SqlServerGrammar)); + } + public function testTableComment() { $base = new Blueprint('posts', function (Blueprint $table) { From e184156bf8baa4c6eb689f1bec8284871197a89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Potock=C3=BD?= Date: Thu, 14 Nov 2024 21:03:16 +0100 Subject: [PATCH 6/6] Revert support for callback --- .../Database/Schema/Grammars/Grammar.php | 4 +-- .../Database/DatabaseSchemaBlueprintTest.php | 35 ------------------- 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index 21bb62ac111f..b21fcf23b65d 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -251,11 +251,11 @@ protected function typeVector(Fluent $column) * Create the column definition for a raw column type. * * @param \Illuminate\Support\Fluent $column - * @return string|(\Closure(\Illuminate\Database\Connection): string) + * @return string */ protected function typeRaw(Fluent $column) { - return value($column->offsetGet('definition'), $this->connection); + return $column->offsetGet('definition'); } /** diff --git a/tests/Database/DatabaseSchemaBlueprintTest.php b/tests/Database/DatabaseSchemaBlueprintTest.php index 165428cdea93..1565ebc9c897 100755 --- a/tests/Database/DatabaseSchemaBlueprintTest.php +++ b/tests/Database/DatabaseSchemaBlueprintTest.php @@ -3,7 +3,6 @@ namespace Illuminate\Tests\Database; use Illuminate\Database\Connection; -use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Grammars\MariaDbGrammar; @@ -601,40 +600,6 @@ public function testRawColumn() ], $blueprint->toSql($connection, new SqlServerGrammar)); } - public function testRawColumnUsingCallback() - { - $connectionMock = m::mock(Connection::class); - - $base = new Blueprint('posts', function ($table) use ($connectionMock) { - $table->rawColumn('legacy_boolean', function (ConnectionInterface $connection) use ($connectionMock) { - $this->assertSame($connection, $connectionMock); - - return 'INT(1)'; - })->nullable(); - }); - - $blueprint = clone $base; - $this->assertEquals([ - 'alter table `posts` add `legacy_boolean` INT(1) null', - ], $blueprint->toSql($connectionMock, new MySqlGrammar)); - - $blueprint = clone $base; - $connectionMock->shouldReceive('getServerVersion')->andReturn('3.35'); - $this->assertEquals([ - 'alter table "posts" add column "legacy_boolean" INT(1)', - ], $blueprint->toSql($connectionMock, new SQLiteGrammar)); - - $blueprint = clone $base; - $this->assertEquals([ - 'alter table "posts" add column "legacy_boolean" INT(1) null', - ], $blueprint->toSql($connectionMock, new PostgresGrammar)); - - $blueprint = clone $base; - $this->assertEquals([ - 'alter table "posts" add "legacy_boolean" INT(1) null', - ], $blueprint->toSql($connectionMock, new SqlServerGrammar)); - } - public function testTableComment() { $base = new Blueprint('posts', function (Blueprint $table) {