From 17a1cfb2ed27efa8ac9d0725c3c2fadf5224b047 Mon Sep 17 00:00:00 2001 From: JacksonIV Date: Sun, 14 Apr 2019 10:41:46 +0200 Subject: [PATCH 1/9] Add the ability to register custom DBAL types in the schema builder. --- src/Illuminate/Database/Schema/Builder.php | 25 ++++++++ .../Database/Schema/MySqlBuilder.php | 17 +++++ .../Database/Schema/Types/TinyInteger.php | 64 +++++++++++++++++++ src/Illuminate/Support/Facades/Schema.php | 1 + .../Database/SchemaBuilderTest.php | 9 +++ 5 files changed, 116 insertions(+) create mode 100644 src/Illuminate/Database/Schema/Types/TinyInteger.php diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 2e25cf0cd9dc..eb33b2a86c06 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -3,6 +3,7 @@ namespace Illuminate\Database\Schema; use Closure; +use Doctrine\DBAL\Types\Type; use LogicException; use Illuminate\Database\Connection; @@ -317,4 +318,28 @@ public function blueprintResolver(Closure $resolver) { $this->resolver = $resolver; } + + /** + * Register your own Doctrine mapping type. + * + * @param string $class + * @param string $name + * @param string $type + * @return void + * + * @throws \Doctrine\DBAL\DBALException + */ + public function registerCustomDBALType($class, $name, $type) + { + if (! $this->connection->isDoctrineAvailable()) { + return; + } + if (! Type::hasType($name)) { + Type::addType($name, $class); + $this->connection + ->getDoctrineSchemaManager() + ->getDatabasePlatform() + ->registerDoctrineTypeMapping($type, $name); + } + } } diff --git a/src/Illuminate/Database/Schema/MySqlBuilder.php b/src/Illuminate/Database/Schema/MySqlBuilder.php index 85f3e92c25f7..a6f4aa184b4e 100755 --- a/src/Illuminate/Database/Schema/MySqlBuilder.php +++ b/src/Illuminate/Database/Schema/MySqlBuilder.php @@ -2,8 +2,25 @@ namespace Illuminate\Database\Schema; +use Illuminate\Database\Connection; +use Illuminate\Database\Schema\Types\TinyInteger; + class MySqlBuilder extends Builder { + /** + * MySqlBuilder constructor. + * + * @param Connection $connection + * + * @throws \Doctrine\DBAL\DBALException + */ + public function __construct(Connection $connection) + { + parent::__construct($connection); + + $this->registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT'); + } + /** * Determine if the given table exists. * diff --git a/src/Illuminate/Database/Schema/Types/TinyInteger.php b/src/Illuminate/Database/Schema/Types/TinyInteger.php new file mode 100644 index 000000000000..6801c5441cbe --- /dev/null +++ b/src/Illuminate/Database/Schema/Types/TinyInteger.php @@ -0,0 +1,64 @@ +assertTrue(true); } + + public function test_register_custom_DBAL_type() + { + Schema::registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT'); + + $this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap()); + } } From eb5de12c443b10c8eb4936a5d09977adc43a2a54 Mon Sep 17 00:00:00 2001 From: JacksonIV Date: Sun, 14 Apr 2019 10:43:37 +0200 Subject: [PATCH 2/9] Fix a styling issue. --- src/Illuminate/Database/Schema/Builder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index eb33b2a86c06..1e3ca3c00952 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -334,6 +334,7 @@ public function registerCustomDBALType($class, $name, $type) if (! $this->connection->isDoctrineAvailable()) { return; } + if (! Type::hasType($name)) { Type::addType($name, $class); $this->connection From 7c0d0f6baea9c89255e5da5a7df5ec0a0c5d9b82 Mon Sep 17 00:00:00 2001 From: JacksonIV Date: Sun, 14 Apr 2019 10:46:40 +0200 Subject: [PATCH 3/9] Fix a styling issue. --- src/Illuminate/Database/Schema/Builder.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 1e3ca3c00952..5dacb85d6145 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -334,9 +334,10 @@ public function registerCustomDBALType($class, $name, $type) if (! $this->connection->isDoctrineAvailable()) { return; } - + if (! Type::hasType($name)) { Type::addType($name, $class); + $this->connection ->getDoctrineSchemaManager() ->getDatabasePlatform() From c97a8572eca62441705d7d39af3ecae000c35386 Mon Sep 17 00:00:00 2001 From: JacksonIV Date: Sun, 14 Apr 2019 11:05:14 +0200 Subject: [PATCH 4/9] Fix a styling issue. --- src/Illuminate/Database/Schema/Builder.php | 4 ++-- src/Illuminate/Database/Schema/Types/TinyInteger.php | 2 +- tests/Integration/Database/SchemaBuilderTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 5dacb85d6145..6460277691ce 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -3,8 +3,8 @@ namespace Illuminate\Database\Schema; use Closure; -use Doctrine\DBAL\Types\Type; use LogicException; +use Doctrine\DBAL\Types\Type; use Illuminate\Database\Connection; class Builder @@ -337,7 +337,7 @@ public function registerCustomDBALType($class, $name, $type) if (! Type::hasType($name)) { Type::addType($name, $class); - + $this->connection ->getDoctrineSchemaManager() ->getDatabasePlatform() diff --git a/src/Illuminate/Database/Schema/Types/TinyInteger.php b/src/Illuminate/Database/Schema/Types/TinyInteger.php index 6801c5441cbe..ad3b9ed0c261 100644 --- a/src/Illuminate/Database/Schema/Types/TinyInteger.php +++ b/src/Illuminate/Database/Schema/Types/TinyInteger.php @@ -2,8 +2,8 @@ namespace Illuminate\Database\Schema\Types; -use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Platforms\AbstractPlatform; class TinyInteger extends Type { diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index da20b59786d9..54a1a095fefd 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -3,10 +3,10 @@ namespace Illuminate\Tests\Integration\Database\SchemaTest; use Doctrine\DBAL\Types\Type; -use Illuminate\Database\Schema\Types\TinyInteger; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Types\TinyInteger; use Illuminate\Tests\Integration\Database\DatabaseTestCase; /** From de5d0c4830edf3ac3c697e6c7d31e63c9309e4da Mon Sep 17 00:00:00 2001 From: JacksonIV Date: Sun, 14 Apr 2019 11:09:18 +0200 Subject: [PATCH 5/9] Fix a styling issue. --- src/Illuminate/Database/Schema/Types/TinyInteger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Types/TinyInteger.php b/src/Illuminate/Database/Schema/Types/TinyInteger.php index ad3b9ed0c261..3e6123f07710 100644 --- a/src/Illuminate/Database/Schema/Types/TinyInteger.php +++ b/src/Illuminate/Database/Schema/Types/TinyInteger.php @@ -61,4 +61,4 @@ public function getName() { return self::NAME; } -} \ No newline at end of file +} From daeee63b558617e8c3fdaa9849d771031cbc0450 Mon Sep 17 00:00:00 2001 From: jackson Date: Mon, 15 Apr 2019 22:32:54 +0200 Subject: [PATCH 6/9] Remove unnecessary method overrides and make a slight modification to the registration of the DBAL types. --- src/Illuminate/Database/Schema/Builder.php | 14 +++++----- .../Database/Schema/MySqlBuilder.php | 18 ++++++++++++- .../Database/Schema/Types/TinyInteger.php | 26 ------------------- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 6460277691ce..ba41ee1a4341 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -331,17 +331,15 @@ public function blueprintResolver(Closure $resolver) */ public function registerCustomDBALType($class, $name, $type) { - if (! $this->connection->isDoctrineAvailable()) { + if (Type::hasType($name)) { return; } - if (! Type::hasType($name)) { - Type::addType($name, $class); + Type::addType($name, $class); - $this->connection - ->getDoctrineSchemaManager() - ->getDatabasePlatform() - ->registerDoctrineTypeMapping($type, $name); - } + $this->connection + ->getDoctrineSchemaManager() + ->getDatabasePlatform() + ->registerDoctrineTypeMapping($type, $name); } } diff --git a/src/Illuminate/Database/Schema/MySqlBuilder.php b/src/Illuminate/Database/Schema/MySqlBuilder.php index a6f4aa184b4e..ba183a1e3b9d 100755 --- a/src/Illuminate/Database/Schema/MySqlBuilder.php +++ b/src/Illuminate/Database/Schema/MySqlBuilder.php @@ -18,7 +18,7 @@ public function __construct(Connection $connection) { parent::__construct($connection); - $this->registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT'); + $this->registerCustomDBALTypes(); } /** @@ -128,4 +128,20 @@ protected function getAllViews() $this->grammar->compileGetAllViews() ); } + + /** + * Register custom DBAL types for the MySQL builder. + * + * @return void + * + * @throws \Doctrine\DBAL\DBALException + */ + private function registerCustomDBALTypes() + { + if (! $this->connection->isDoctrineAvailable()) { + return; + } + + $this->registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT'); + } } diff --git a/src/Illuminate/Database/Schema/Types/TinyInteger.php b/src/Illuminate/Database/Schema/Types/TinyInteger.php index 3e6123f07710..cad5f1db1b83 100644 --- a/src/Illuminate/Database/Schema/Types/TinyInteger.php +++ b/src/Illuminate/Database/Schema/Types/TinyInteger.php @@ -26,32 +26,6 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla return 'TINYINT'; } - /** - * Converts a value from its database representation to its PHP representation - * of this type. - * - * @param mixed $value - * @param AbstractPlatform $platform - * @return mixed - */ - public function convertToPHPValue($value, AbstractPlatform $platform) - { - return $value; - } - - /** - * Converts a value from its PHP representation to its database representation - * of this type. - * - * @param mixed $value - * @param AbstractPlatform $platform - * @return mixed - */ - public function convertToDatabaseValue($value, AbstractPlatform $platform) - { - return $value; - } - /** * The name of the custom type. * From 0c1f39c7ddc58705b6457878008a2304d3aa8dab Mon Sep 17 00:00:00 2001 From: jackson Date: Tue, 16 Apr 2019 00:16:52 +0200 Subject: [PATCH 7/9] Add a test to verify the column type and the syntax. --- .../Database/SchemaBuilderTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 54a1a095fefd..8c09988c7ed1 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Integration\Database\SchemaTest; use Doctrine\DBAL\Types\Type; +use Illuminate\Database\Schema\Grammars\SQLiteGrammar; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; @@ -44,6 +45,28 @@ public function test_register_custom_DBAL_type() { Schema::registerCustomDBALType(TinyInteger::class, TinyInteger::NAME, 'TINYINT'); + Schema::create('test', function (Blueprint $table) { + $table->string('test_column'); + }); + + $blueprint = new Blueprint('test', function (Blueprint $table) { + $table->tinyInteger('test_column')->change(); + }); + + $expected = [ + 'CREATE TEMPORARY TABLE __temp__test AS SELECT test_column FROM test', + 'DROP TABLE test', + 'CREATE TABLE test (test_column TINYINT NOT NULL COLLATE BINARY)', + 'INSERT INTO test (test_column) SELECT test_column FROM __temp__test', + 'DROP TABLE __temp__test' + ]; + + $statements = $blueprint->toSql($this->getConnection(), new SQLiteGrammar()); + + $blueprint->build($this->getConnection(), new SQLiteGrammar()); + $this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap()); + $this->assertEquals('tinyinteger', Schema::getColumnType('test', 'test_column')); + $this->assertEquals($expected, $statements); } } From db8419641c6ccdc7d6e4673267027aba92868b53 Mon Sep 17 00:00:00 2001 From: jackson Date: Tue, 16 Apr 2019 00:18:59 +0200 Subject: [PATCH 8/9] Fix a styling issue. --- tests/Integration/Database/SchemaBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 8c09988c7ed1..df790b6d2e14 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -3,11 +3,11 @@ namespace Illuminate\Tests\Integration\Database\SchemaTest; use Doctrine\DBAL\Types\Type; -use Illuminate\Database\Schema\Grammars\SQLiteGrammar; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Types\TinyInteger; +use Illuminate\Database\Schema\Grammars\SQLiteGrammar; use Illuminate\Tests\Integration\Database\DatabaseTestCase; /** @@ -58,7 +58,7 @@ public function test_register_custom_DBAL_type() 'DROP TABLE test', 'CREATE TABLE test (test_column TINYINT NOT NULL COLLATE BINARY)', 'INSERT INTO test (test_column) SELECT test_column FROM __temp__test', - 'DROP TABLE __temp__test' + 'DROP TABLE __temp__test', ]; $statements = $blueprint->toSql($this->getConnection(), new SQLiteGrammar()); From 1bcad051b668b06ba3366560242d4f112e3a1860 Mon Sep 17 00:00:00 2001 From: jackson Date: Wed, 17 Apr 2019 13:24:53 +0200 Subject: [PATCH 9/9] Fix a styling issue. --- src/Illuminate/Database/Schema/Builder.php | 2 +- src/Illuminate/Database/Schema/MySqlBuilder.php | 3 +-- src/Illuminate/Database/Schema/Types/TinyInteger.php | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index ba41ee1a4341..86d09741da03 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -324,7 +324,7 @@ public function blueprintResolver(Closure $resolver) * * @param string $class * @param string $name - * @param string $type + * @param string $type * @return void * * @throws \Doctrine\DBAL\DBALException diff --git a/src/Illuminate/Database/Schema/MySqlBuilder.php b/src/Illuminate/Database/Schema/MySqlBuilder.php index ba183a1e3b9d..2d1290bab1d1 100755 --- a/src/Illuminate/Database/Schema/MySqlBuilder.php +++ b/src/Illuminate/Database/Schema/MySqlBuilder.php @@ -10,8 +10,7 @@ class MySqlBuilder extends Builder /** * MySqlBuilder constructor. * - * @param Connection $connection - * + * @param \Illuminate\Database\Connection $connection * @throws \Doctrine\DBAL\DBALException */ public function __construct(Connection $connection) diff --git a/src/Illuminate/Database/Schema/Types/TinyInteger.php b/src/Illuminate/Database/Schema/Types/TinyInteger.php index cad5f1db1b83..37e9b9575868 100644 --- a/src/Illuminate/Database/Schema/Types/TinyInteger.php +++ b/src/Illuminate/Database/Schema/Types/TinyInteger.php @@ -18,7 +18,7 @@ class TinyInteger extends Type * Gets the SQL declaration snippet for a field of this type. * * @param array $fieldDeclaration - * @param AbstractPlatform $platform + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @return string */ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)