Skip to content

[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

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 50 additions & 32 deletions src/Illuminate/Database/Schema/Grammars/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down Expand Up @@ -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;
Copy link
Member

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?

Copy link
Contributor Author

@hafezdivandari hafezdivandari Aug 16, 2022

}

if ($fluent['type'] === 'char') {
$options['fixed'] = true;
}

if (static::doesntNeedCharacterOptions($fluent['type'])) {
if ($fluent['type'] === 'timestamp') {
$options['platformOptions'] = [
'version' => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "version" = true even mean though?

Copy link
Contributor Author

@hafezdivandari hafezdivandari Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They use this option on MySQL and DB2 only on datetime type. That's the only way to convert datetime to timestamp. but I don't know why they call it "version" if that's the question.

];
}

if ($fluent['type'] === 'jsonb') {
$options['platformOptions'] = [
'jsonb' => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? Why is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

];
}

if (static::doesntNeedCharacterOptions($doctrineType->getName())) {
$options['customSchemaOptions'] = [
'collation' => '',
'charset' => '',
Expand All @@ -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)
);
}

/**
Expand All @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why MySQL specific?

Copy link
Contributor Author

default => null,
};
}

Expand All @@ -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]);
}

/**
Expand Down