From b9e4c7a27f30f05a87563f82b4917c544f8eb1bd Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Wed, 3 Apr 2024 21:08:50 +0330 Subject: [PATCH] [11.x] Remove redundant code from MariaDbGrammar (#50907) * remove redundant code * force re-run tests * Update MySqlGrammar.php --------- Co-authored-by: Taylor Otwell --- .../Schema/Grammars/MariaDbGrammar.php | 31 +------- .../Database/Schema/Grammars/MySqlGrammar.php | 77 +++++++++++-------- .../Database/DatabaseSchemaBlueprintTest.php | 3 +- 3 files changed, 48 insertions(+), 63 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php b/src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php index 39ae68619127..2996406a27fb 100755 --- a/src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php @@ -3,9 +3,7 @@ namespace Illuminate\Database\Schema\Grammars; use Illuminate\Database\Connection; -use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Database\Schema\ColumnDefinition; use Illuminate\Support\Fluent; class MariaDbGrammar extends MySqlGrammar @@ -21,34 +19,7 @@ class MariaDbGrammar extends MySqlGrammar public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) { if (version_compare($connection->getServerVersion(), '10.5.2', '<')) { - $column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable())) - ->firstWhere('name', $command->from); - - $modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([ - 'change' => true, - 'type' => match ($column['type_name']) { - 'bigint' => 'bigInteger', - 'int' => 'integer', - 'mediumint' => 'mediumInteger', - 'smallint' => 'smallInteger', - 'tinyint' => 'tinyInteger', - default => $column['type_name'], - }, - 'nullable' => $column['nullable'], - 'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp') - ? new Expression($column['default']) - : $column['default'], - 'autoIncrement' => $column['auto_increment'], - 'collation' => $column['collation'], - 'comment' => $column['comment'], - ])); - - return sprintf('alter table %s change %s %s %s', - $this->wrapTable($blueprint), - $this->wrap($command->from), - $this->wrap($command->to), - $modifiers - ); + return $this->compileLegacyRenameColumn($blueprint, $command, $connection); } return parent::compileRenameColumn($blueprint, $command, $connection); diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index 7fed371ea109..0c39a4b1e3f5 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -324,43 +324,56 @@ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Conne if (($connection->isMaria() && version_compare($version, '10.5.2', '<')) || (! $connection->isMaria() && version_compare($version, '8.0.3', '<'))) { - $column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable())) - ->firstWhere('name', $command->from); - - $modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([ - 'change' => true, - 'type' => match ($column['type_name']) { - 'bigint' => 'bigInteger', - 'int' => 'integer', - 'mediumint' => 'mediumInteger', - 'smallint' => 'smallInteger', - 'tinyint' => 'tinyInteger', - default => $column['type_name'], - }, - 'nullable' => $column['nullable'], - 'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp') - ? new Expression($column['default']) - : $column['default'], - 'autoIncrement' => $column['auto_increment'], - 'collation' => $column['collation'], - 'comment' => $column['comment'], - 'virtualAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'virtual' - ? $column['generation']['expression'] : null, - 'storedAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'stored' - ? $column['generation']['expression'] : null, - ])); - - return sprintf('alter table %s change %s %s %s', - $this->wrapTable($blueprint), - $this->wrap($command->from), - $this->wrap($command->to), - $modifiers - ); + return $this->compileLegacyRenameColumn($blueprint, $command, $connection); } return parent::compileRenameColumn($blueprint, $command, $connection); } + /** + * Compile a rename column command for legacy versions of MySQL. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * @param \Illuminate\Database\Connection $connection + * @return string + */ + protected function compileLegacyRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection) + { + $column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable())) + ->firstWhere('name', $command->from); + + $modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([ + 'change' => true, + 'type' => match ($column['type_name']) { + 'bigint' => 'bigInteger', + 'int' => 'integer', + 'mediumint' => 'mediumInteger', + 'smallint' => 'smallInteger', + 'tinyint' => 'tinyInteger', + default => $column['type_name'], + }, + 'nullable' => $column['nullable'], + 'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp') + ? new Expression($column['default']) + : $column['default'], + 'autoIncrement' => $column['auto_increment'], + 'collation' => $column['collation'], + 'comment' => $column['comment'], + 'virtualAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'virtual' + ? $column['generation']['expression'] : null, + 'storedAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'stored' + ? $column['generation']['expression'] : null, + ])); + + return sprintf('alter table %s change %s %s %s', + $this->wrapTable($blueprint), + $this->wrap($command->from), + $this->wrap($command->to), + $modifiers + ); + } + /** * Compile a change column command into a series of SQL statements. * diff --git a/tests/Database/DatabaseSchemaBlueprintTest.php b/tests/Database/DatabaseSchemaBlueprintTest.php index 0fc9318d3269..6026b219b373 100755 --- a/tests/Database/DatabaseSchemaBlueprintTest.php +++ b/tests/Database/DatabaseSchemaBlueprintTest.php @@ -5,6 +5,7 @@ use Illuminate\Database\Connection; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; +use Illuminate\Database\Schema\Grammars\MariaDbGrammar; use Illuminate\Database\Schema\Grammars\MySqlGrammar; use Illuminate\Database\Schema\Grammars\PostgresGrammar; use Illuminate\Database\Schema\Grammars\SQLiteGrammar; @@ -229,7 +230,7 @@ public function testNativeRenameColumnOnLegacyMariaDB() "alter table `users` change `name` `title` varchar(255) collate 'utf8mb4_unicode_ci' null default 'foo'", "alter table `users` change `id` `key` bigint unsigned not null auto_increment comment 'lorem ipsum'", 'alter table `users` change `generated` `new_generated` int as (expression) stored not null', - ], $blueprint->toSql($connection, new MySqlGrammar)); + ], $blueprint->toSql($connection, new MariaDbGrammar)); } public function testDropColumn()