-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[9.x] Enhance column modifying #43541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ff0e5e
656241c
cefc1c1
24fff94
1b038e0
0293816
9520ae1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,26 @@ | |
|
||
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; | ||
use RuntimeException; | ||
|
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Laravel
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does "version" = There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
]; | ||
} | ||
|
||
if ($fluent['type'] === 'jsonb') { | ||
$options['platformOptions'] = [ | ||
'jsonb' => true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this do? Why is it needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only PostgreSQL supports |
||
]; | ||
} | ||
|
||
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why MySQL specific? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only MySQL uses
|
||
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]); | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use MySQL platform? What if user is using SQL Server?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Laravel
binary
type maps to Doctrineblob
type and only MySQL useslength
option: