Skip to content

Commit

Permalink
[11.x] Remove redundant code from MariaDbGrammar (#50907)
Browse files Browse the repository at this point in the history
* remove redundant code

* force re-run tests

* Update MySqlGrammar.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
hafezdivandari and taylorotwell authored Apr 3, 2024
1 parent 1b453a8 commit b9e4c7a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 63 deletions.
31 changes: 1 addition & 30 deletions src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
77 changes: 45 additions & 32 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b9e4c7a

Please sign in to comment.