diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 70ec66652eb9..dc1093834f55 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -2,10 +2,12 @@ namespace Illuminate\Database\Schema\Grammars; +use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types; use Illuminate\Database\Connection; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Fluent; @@ -13,6 +15,13 @@ class ChangeColumn { + /** + * The database platform instance. + * + * @var \Doctrine\DBAL\Platforms\AbstractPlatform + */ + protected static $platform; + /** * Compile a change column command into a series of SQL statements. * @@ -34,8 +43,8 @@ public static function compile($grammar, Blueprint $blueprint, Fluent $command, } $schema = $connection->getDoctrineSchemaManager(); - $databasePlatform = $schema->getDatabasePlatform(); - $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); + static::$platform = $databasePlatform = $schema->getDatabasePlatform(); + $databasePlatform->registerDoctrineTypeMapping('enum', Types::STRING); $tableDiff = static::getChangedDiff( $grammar, $blueprint, $schema @@ -119,17 +128,35 @@ protected static function getDoctrineColumn(Table $table, Fluent $fluent) */ protected static function getDoctrineColumnChangeOptions(Fluent $fluent) { - $options = ['type' => static::getDoctrineColumnType($fluent['type'])]; + $doctrineType = static::getDoctrineColumnType($fluent['type']); - if (in_array($fluent['type'], ['text', 'mediumText', 'longText'])) { + $options = ['type' => $doctrineType]; + + if ($doctrineType->getName() === Types::TEXT) { $options['length'] = static::calculateDoctrineTextLength($fluent['type']); } + if ($fluent['type'] === 'binary') { + $options['length'] = AbstractMySQLPlatform::LENGTH_LIMIT_BLOB; + } + if ($fluent['type'] === 'char') { $options['fixed'] = true; } - if (static::doesntNeedCharacterOptions($fluent['type'])) { + if ($fluent['type'] === 'timestamp') { + $options['platformOptions'] = [ + 'version' => true, + ]; + } + + if ($fluent['type'] === 'jsonb') { + $options['platformOptions'] = [ + 'jsonb' => true, + ]; + } + + if (static::doesntNeedCharacterOptions($doctrineType->getName())) { $options['customSchemaOptions'] = [ 'collation' => '', 'charset' => '', @@ -149,15 +176,20 @@ protected static function getDoctrineColumnType($type) { $type = strtolower($type); - return Type::getType(match ($type) { - 'biginteger' => 'bigint', - 'smallinteger' => 'smallint', - 'mediumtext', 'longtext' => 'text', - 'binary' => 'blob', - 'uuid' => 'guid', - 'char' => 'string', + $type = match ($type) { + 'biginteger' => Types::BIGINT, + 'mediuminteger' => 'mediumint', + 'smallinteger' => Types::SMALLINT, + 'tinyinteger' => 'tinyint', + 'binary' => Types::BLOB, + 'uuid' => Types::GUID, default => $type, - }); + }; + + return Type::getType(Type::hasType($type) + ? $type + : static::$platform->getDoctrineTypeMapping($type) + ); } /** @@ -169,9 +201,10 @@ protected static function getDoctrineColumnType($type) protected static function calculateDoctrineTextLength($type) { return match ($type) { - 'mediumText' => 65535 + 1, - 'longText' => 16777215 + 1, - default => 255 + 1, + 'tinyText' => AbstractMySQLPlatform::LENGTH_LIMIT_TINYTEXT, + 'text' => AbstractMySQLPlatform::LENGTH_LIMIT_TEXT, + 'mediumText' => AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMTEXT, + default => null, }; } @@ -183,22 +216,7 @@ protected static function calculateDoctrineTextLength($type) */ protected static function doesntNeedCharacterOptions($type) { - return in_array($type, [ - 'bigInteger', - 'binary', - 'boolean', - 'date', - 'dateTime', - 'decimal', - 'double', - 'float', - 'integer', - 'json', - 'mediumInteger', - 'smallInteger', - 'time', - 'tinyInteger', - ]); + return ! in_array($type, [Types::STRING, Types::TEXT]); } /**