From 1ff0e5ecc9d1f045158354130757f95c8250377a Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 4 Aug 2022 06:36:35 +0430 Subject: [PATCH 1/7] map column types using database platform --- .../Database/Schema/Grammars/ChangeColumn.php | 90 ++++++++++--------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 70ec66652eb9..198e463bb188 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -2,10 +2,13 @@ namespace Illuminate\Database\Schema\Grammars; +use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; +use Doctrine\DBAL\Platforms\AbstractPlatform; 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; @@ -35,10 +38,10 @@ public static function compile($grammar, Blueprint $blueprint, Fluent $command, $schema = $connection->getDoctrineSchemaManager(); $databasePlatform = $schema->getDatabasePlatform(); - $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); + $databasePlatform->registerDoctrineTypeMapping('enum', Types::STRING); $tableDiff = static::getChangedDiff( - $grammar, $blueprint, $schema + $grammar, $blueprint, $schema, $databasePlatform ); if ($tableDiff !== false) { @@ -54,14 +57,20 @@ public static function compile($grammar, Blueprint $blueprint, Fluent $command, * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @return \Doctrine\DBAL\Schema\TableDiff|bool */ - protected static function getChangedDiff($grammar, Blueprint $blueprint, SchemaManager $schema) + protected static function getChangedDiff( + $grammar, + Blueprint $blueprint, + SchemaManager $schema, + AbstractPlatform $platform + ) { $current = $schema->listTableDetails($grammar->getTablePrefix().$blueprint->getTable()); return (new Comparator)->diffTable( - $current, static::getTableWithColumnChanges($blueprint, $current) + $current, static::getTableWithColumnChanges($blueprint, $platform, $current) ); } @@ -69,15 +78,16 @@ protected static function getChangedDiff($grammar, Blueprint $blueprint, SchemaM * Get a copy of the given Doctrine table after making the column changes. * * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param \Doctrine\DBAL\Schema\Table $table * @return \Doctrine\DBAL\Schema\Table */ - protected static function getTableWithColumnChanges(Blueprint $blueprint, Table $table) + protected static function getTableWithColumnChanges(Blueprint $blueprint, AbstractPlatform $platform, Table $table) { $table = clone $table; foreach ($blueprint->getChangedColumns() as $fluent) { - $column = static::getDoctrineColumn($table, $fluent); + $column = static::getDoctrineColumn($platform, $table, $fluent); // Here we will spin through each fluent column definition and map it to the proper // Doctrine column definitions - which is necessary because Laravel and Doctrine @@ -100,28 +110,32 @@ protected static function getTableWithColumnChanges(Blueprint $blueprint, Table /** * Get the Doctrine column instance for a column change. * + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param \Doctrine\DBAL\Schema\Table $table * @param \Illuminate\Support\Fluent $fluent * @return \Doctrine\DBAL\Schema\Column */ - protected static function getDoctrineColumn(Table $table, Fluent $fluent) + protected static function getDoctrineColumn(AbstractPlatform $platform, Table $table, Fluent $fluent) { return $table->changeColumn( - $fluent['name'], static::getDoctrineColumnChangeOptions($fluent) + $fluent['name'], static::getDoctrineColumnChangeOptions($platform, $fluent) )->getColumn($fluent['name']); } /** * Get the Doctrine column change options. * + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param \Illuminate\Support\Fluent $fluent * @return array */ - protected static function getDoctrineColumnChangeOptions(Fluent $fluent) + protected static function getDoctrineColumnChangeOptions(AbstractPlatform $platform, Fluent $fluent) { - $options = ['type' => static::getDoctrineColumnType($fluent['type'])]; + $dcType = static::getDoctrineColumnType($platform, $fluent['type']); - if (in_array($fluent['type'], ['text', 'mediumText', 'longText'])) { + $options = ['type' => $dcType]; + + if ($dcType->getName() === Types::TEXT) { $options['length'] = static::calculateDoctrineTextLength($fluent['type']); } @@ -129,7 +143,13 @@ protected static function getDoctrineColumnChangeOptions(Fluent $fluent) $options['fixed'] = true; } - if (static::doesntNeedCharacterOptions($fluent['type'])) { + if ($fluent['type'] === 'timestamp') { + $options['platformOptions'] = [ + 'version' => true, + ]; + } + + if (static::doesntNeedCharacterOptions($dcType->getName())) { $options['customSchemaOptions'] = [ 'collation' => '', 'charset' => '', @@ -142,22 +162,26 @@ protected static function getDoctrineColumnChangeOptions(Fluent $fluent) /** * Get the doctrine column type. * + * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param string $type * @return \Doctrine\DBAL\Types\Type */ - protected static function getDoctrineColumnType($type) + protected static function getDoctrineColumnType(AbstractPlatform $platform, $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, + 'smallinteger' => Types::SMALLINT, + 'binary' => Types::BLOB, + 'uuid' => Types::GUID, default => $type, - }); + }; + + return Type::getType(Type::hasType($type) + ? $type + : $platform->getDoctrineTypeMapping($type) + ); } /** @@ -169,9 +193,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 +208,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]); } /** From 656241c7caecd28c438376a737bc12e0dc309baf Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 4 Aug 2022 06:50:19 +0430 Subject: [PATCH 2/7] support jsonb column modifying --- src/Illuminate/Database/Schema/Grammars/ChangeColumn.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 198e463bb188..4399fbbd6e6f 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -149,6 +149,12 @@ protected static function getDoctrineColumnChangeOptions(AbstractPlatform $platf ]; } + if ($fluent['type'] === 'jsonb') { + $options['platformOptions'] = [ + 'jsonb' => true, + ]; + } + if (static::doesntNeedCharacterOptions($dcType->getName())) { $options['customSchemaOptions'] = [ 'collation' => '', From cefc1c134648bf23290d002dde36f223e7781a76 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 4 Aug 2022 07:07:13 +0430 Subject: [PATCH 3/7] fix modifying binary column type --- src/Illuminate/Database/Schema/Grammars/ChangeColumn.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 4399fbbd6e6f..322bd916659c 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -139,6 +139,10 @@ protected static function getDoctrineColumnChangeOptions(AbstractPlatform $platf $options['length'] = static::calculateDoctrineTextLength($fluent['type']); } + if ($fluent['type'] === 'binary') { + $options['length'] = AbstractMySQLPlatform::LENGTH_LIMIT_BLOB; + } + if ($fluent['type'] === 'char') { $options['fixed'] = true; } From 24fff9425d547a21d364455d12ddb84c0214da6a Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 4 Aug 2022 07:19:52 +0430 Subject: [PATCH 4/7] fix code style --- src/Illuminate/Database/Schema/Grammars/ChangeColumn.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 322bd916659c..1411d24b26d0 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -65,8 +65,7 @@ protected static function getChangedDiff( Blueprint $blueprint, SchemaManager $schema, AbstractPlatform $platform - ) - { + ) { $current = $schema->listTableDetails($grammar->getTablePrefix().$blueprint->getTable()); return (new Comparator)->diffTable( From 1b038e0a9545b5088086392fefc91ea4d3766d10 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 4 Aug 2022 16:47:34 +0430 Subject: [PATCH 5/7] define database platform as a static property --- .../Database/Schema/Grammars/ChangeColumn.php | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 1411d24b26d0..9d7d64b8f025 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -3,7 +3,6 @@ namespace Illuminate\Database\Schema\Grammars; use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; -use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Table; @@ -16,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. * @@ -37,11 +43,11 @@ public static function compile($grammar, Blueprint $blueprint, Fluent $command, } $schema = $connection->getDoctrineSchemaManager(); - $databasePlatform = $schema->getDatabasePlatform(); + static::$platform = $databasePlatform = $schema->getDatabasePlatform(); $databasePlatform->registerDoctrineTypeMapping('enum', Types::STRING); $tableDiff = static::getChangedDiff( - $grammar, $blueprint, $schema, $databasePlatform + $grammar, $blueprint, $schema ); if ($tableDiff !== false) { @@ -57,19 +63,14 @@ public static function compile($grammar, Blueprint $blueprint, Fluent $command, * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @return \Doctrine\DBAL\Schema\TableDiff|bool */ - protected static function getChangedDiff( - $grammar, - Blueprint $blueprint, - SchemaManager $schema, - AbstractPlatform $platform - ) { + protected static function getChangedDiff($grammar, Blueprint $blueprint, SchemaManager $schema) + { $current = $schema->listTableDetails($grammar->getTablePrefix().$blueprint->getTable()); return (new Comparator)->diffTable( - $current, static::getTableWithColumnChanges($blueprint, $platform, $current) + $current, static::getTableWithColumnChanges($blueprint, $current) ); } @@ -77,16 +78,15 @@ protected static function getChangedDiff( * Get a copy of the given Doctrine table after making the column changes. * * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param \Doctrine\DBAL\Schema\Table $table * @return \Doctrine\DBAL\Schema\Table */ - protected static function getTableWithColumnChanges(Blueprint $blueprint, AbstractPlatform $platform, Table $table) + protected static function getTableWithColumnChanges(Blueprint $blueprint, Table $table) { $table = clone $table; foreach ($blueprint->getChangedColumns() as $fluent) { - $column = static::getDoctrineColumn($platform, $table, $fluent); + $column = static::getDoctrineColumn($table, $fluent); // Here we will spin through each fluent column definition and map it to the proper // Doctrine column definitions - which is necessary because Laravel and Doctrine @@ -109,28 +109,26 @@ protected static function getTableWithColumnChanges(Blueprint $blueprint, Abstra /** * Get the Doctrine column instance for a column change. * - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param \Doctrine\DBAL\Schema\Table $table * @param \Illuminate\Support\Fluent $fluent * @return \Doctrine\DBAL\Schema\Column */ - protected static function getDoctrineColumn(AbstractPlatform $platform, Table $table, Fluent $fluent) + protected static function getDoctrineColumn(Table $table, Fluent $fluent) { return $table->changeColumn( - $fluent['name'], static::getDoctrineColumnChangeOptions($platform, $fluent) + $fluent['name'], static::getDoctrineColumnChangeOptions($fluent) )->getColumn($fluent['name']); } /** * Get the Doctrine column change options. * - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param \Illuminate\Support\Fluent $fluent * @return array */ - protected static function getDoctrineColumnChangeOptions(AbstractPlatform $platform, Fluent $fluent) + protected static function getDoctrineColumnChangeOptions(Fluent $fluent) { - $dcType = static::getDoctrineColumnType($platform, $fluent['type']); + $dcType = static::getDoctrineColumnType($fluent['type']); $options = ['type' => $dcType]; @@ -171,11 +169,10 @@ protected static function getDoctrineColumnChangeOptions(AbstractPlatform $platf /** * Get the doctrine column type. * - * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform * @param string $type * @return \Doctrine\DBAL\Types\Type */ - protected static function getDoctrineColumnType(AbstractPlatform $platform, $type) + protected static function getDoctrineColumnType($type) { $type = strtolower($type); @@ -189,7 +186,7 @@ protected static function getDoctrineColumnType(AbstractPlatform $platform, $typ return Type::getType(Type::hasType($type) ? $type - : $platform->getDoctrineTypeMapping($type) + : static::$platform->getDoctrineTypeMapping($type) ); } From 029381632f5c2f2ac813fbdc654dbc6106a25f19 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 6 Aug 2022 20:23:36 +0430 Subject: [PATCH 6/7] add 2 more types --- src/Illuminate/Database/Schema/Grammars/ChangeColumn.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 9d7d64b8f025..75fdd7e5273f 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -178,7 +178,9 @@ protected static function getDoctrineColumnType($type) $type = match ($type) { 'biginteger' => Types::BIGINT, + 'mediuminteger' => 'mediumint', 'smallinteger' => Types::SMALLINT, + 'tinyinteger' => 'tinyint', 'binary' => Types::BLOB, 'uuid' => Types::GUID, default => $type, From 9520ae1a2c09190ca10742e3ddba7ca3f29086d5 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Sat, 6 Aug 2022 22:22:55 +0430 Subject: [PATCH 7/7] better var name --- src/Illuminate/Database/Schema/Grammars/ChangeColumn.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php index 75fdd7e5273f..dc1093834f55 100644 --- a/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php +++ b/src/Illuminate/Database/Schema/Grammars/ChangeColumn.php @@ -128,11 +128,11 @@ protected static function getDoctrineColumn(Table $table, Fluent $fluent) */ protected static function getDoctrineColumnChangeOptions(Fluent $fluent) { - $dcType = static::getDoctrineColumnType($fluent['type']); + $doctrineType = static::getDoctrineColumnType($fluent['type']); - $options = ['type' => $dcType]; + $options = ['type' => $doctrineType]; - if ($dcType->getName() === Types::TEXT) { + if ($doctrineType->getName() === Types::TEXT) { $options['length'] = static::calculateDoctrineTextLength($fluent['type']); } @@ -156,7 +156,7 @@ protected static function getDoctrineColumnChangeOptions(Fluent $fluent) ]; } - if (static::doesntNeedCharacterOptions($dcType->getName())) { + if (static::doesntNeedCharacterOptions($doctrineType->getName())) { $options['customSchemaOptions'] = [ 'collation' => '', 'charset' => '',