diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 2e25cf0cd9dc..86d09741da03 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -4,6 +4,7 @@ use Closure; use LogicException; +use Doctrine\DBAL\Types\Type; use Illuminate\Database\Connection; class Builder @@ -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 (Type::hasType($name)) { + return; + } + + 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..2d1290bab1d1 100755 --- a/src/Illuminate/Database/Schema/MySqlBuilder.php +++ b/src/Illuminate/Database/Schema/MySqlBuilder.php @@ -2,8 +2,24 @@ namespace Illuminate\Database\Schema; +use Illuminate\Database\Connection; +use Illuminate\Database\Schema\Types\TinyInteger; + class MySqlBuilder extends Builder { + /** + * MySqlBuilder constructor. + * + * @param \Illuminate\Database\Connection $connection + * @throws \Doctrine\DBAL\DBALException + */ + public function __construct(Connection $connection) + { + parent::__construct($connection); + + $this->registerCustomDBALTypes(); + } + /** * Determine if the given table exists. * @@ -111,4 +127,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 new file mode 100644 index 000000000000..37e9b9575868 --- /dev/null +++ b/src/Illuminate/Database/Schema/Types/TinyInteger.php @@ -0,0 +1,38 @@ +assertTrue(true); } + + 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); + } }