Skip to content

Commit b9e4c7a

Browse files
[11.x] Remove redundant code from MariaDbGrammar (#50907)
* remove redundant code * force re-run tests * Update MySqlGrammar.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 1b453a8 commit b9e4c7a

File tree

3 files changed

+48
-63
lines changed

3 files changed

+48
-63
lines changed

src/Illuminate/Database/Schema/Grammars/MariaDbGrammar.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace Illuminate\Database\Schema\Grammars;
44

55
use Illuminate\Database\Connection;
6-
use Illuminate\Database\Query\Expression;
76
use Illuminate\Database\Schema\Blueprint;
8-
use Illuminate\Database\Schema\ColumnDefinition;
97
use Illuminate\Support\Fluent;
108

119
class MariaDbGrammar extends MySqlGrammar
@@ -21,34 +19,7 @@ class MariaDbGrammar extends MySqlGrammar
2119
public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
2220
{
2321
if (version_compare($connection->getServerVersion(), '10.5.2', '<')) {
24-
$column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable()))
25-
->firstWhere('name', $command->from);
26-
27-
$modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([
28-
'change' => true,
29-
'type' => match ($column['type_name']) {
30-
'bigint' => 'bigInteger',
31-
'int' => 'integer',
32-
'mediumint' => 'mediumInteger',
33-
'smallint' => 'smallInteger',
34-
'tinyint' => 'tinyInteger',
35-
default => $column['type_name'],
36-
},
37-
'nullable' => $column['nullable'],
38-
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
39-
? new Expression($column['default'])
40-
: $column['default'],
41-
'autoIncrement' => $column['auto_increment'],
42-
'collation' => $column['collation'],
43-
'comment' => $column['comment'],
44-
]));
45-
46-
return sprintf('alter table %s change %s %s %s',
47-
$this->wrapTable($blueprint),
48-
$this->wrap($command->from),
49-
$this->wrap($command->to),
50-
$modifiers
51-
);
22+
return $this->compileLegacyRenameColumn($blueprint, $command, $connection);
5223
}
5324

5425
return parent::compileRenameColumn($blueprint, $command, $connection);

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -324,43 +324,56 @@ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Conne
324324

325325
if (($connection->isMaria() && version_compare($version, '10.5.2', '<')) ||
326326
(! $connection->isMaria() && version_compare($version, '8.0.3', '<'))) {
327-
$column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable()))
328-
->firstWhere('name', $command->from);
329-
330-
$modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([
331-
'change' => true,
332-
'type' => match ($column['type_name']) {
333-
'bigint' => 'bigInteger',
334-
'int' => 'integer',
335-
'mediumint' => 'mediumInteger',
336-
'smallint' => 'smallInteger',
337-
'tinyint' => 'tinyInteger',
338-
default => $column['type_name'],
339-
},
340-
'nullable' => $column['nullable'],
341-
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
342-
? new Expression($column['default'])
343-
: $column['default'],
344-
'autoIncrement' => $column['auto_increment'],
345-
'collation' => $column['collation'],
346-
'comment' => $column['comment'],
347-
'virtualAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'virtual'
348-
? $column['generation']['expression'] : null,
349-
'storedAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'stored'
350-
? $column['generation']['expression'] : null,
351-
]));
352-
353-
return sprintf('alter table %s change %s %s %s',
354-
$this->wrapTable($blueprint),
355-
$this->wrap($command->from),
356-
$this->wrap($command->to),
357-
$modifiers
358-
);
327+
return $this->compileLegacyRenameColumn($blueprint, $command, $connection);
359328
}
360329

361330
return parent::compileRenameColumn($blueprint, $command, $connection);
362331
}
363332

333+
/**
334+
* Compile a rename column command for legacy versions of MySQL.
335+
*
336+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
337+
* @param \Illuminate\Support\Fluent $command
338+
* @param \Illuminate\Database\Connection $connection
339+
* @return string
340+
*/
341+
protected function compileLegacyRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
342+
{
343+
$column = collect($connection->getSchemaBuilder()->getColumns($blueprint->getTable()))
344+
->firstWhere('name', $command->from);
345+
346+
$modifiers = $this->addModifiers($column['type'], $blueprint, new ColumnDefinition([
347+
'change' => true,
348+
'type' => match ($column['type_name']) {
349+
'bigint' => 'bigInteger',
350+
'int' => 'integer',
351+
'mediumint' => 'mediumInteger',
352+
'smallint' => 'smallInteger',
353+
'tinyint' => 'tinyInteger',
354+
default => $column['type_name'],
355+
},
356+
'nullable' => $column['nullable'],
357+
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
358+
? new Expression($column['default'])
359+
: $column['default'],
360+
'autoIncrement' => $column['auto_increment'],
361+
'collation' => $column['collation'],
362+
'comment' => $column['comment'],
363+
'virtualAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'virtual'
364+
? $column['generation']['expression'] : null,
365+
'storedAs' => ! is_null($column['generation']) && $column['generation']['type'] === 'stored'
366+
? $column['generation']['expression'] : null,
367+
]));
368+
369+
return sprintf('alter table %s change %s %s %s',
370+
$this->wrapTable($blueprint),
371+
$this->wrap($command->from),
372+
$this->wrap($command->to),
373+
$modifiers
374+
);
375+
}
376+
364377
/**
365378
* Compile a change column command into a series of SQL statements.
366379
*

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Connection;
66
use Illuminate\Database\Schema\Blueprint;
77
use Illuminate\Database\Schema\Builder;
8+
use Illuminate\Database\Schema\Grammars\MariaDbGrammar;
89
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
910
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
1011
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
@@ -229,7 +230,7 @@ public function testNativeRenameColumnOnLegacyMariaDB()
229230
"alter table `users` change `name` `title` varchar(255) collate 'utf8mb4_unicode_ci' null default 'foo'",
230231
"alter table `users` change `id` `key` bigint unsigned not null auto_increment comment 'lorem ipsum'",
231232
'alter table `users` change `generated` `new_generated` int as (expression) stored not null',
232-
], $blueprint->toSql($connection, new MySqlGrammar));
233+
], $blueprint->toSql($connection, new MariaDbGrammar));
233234
}
234235

235236
public function testDropColumn()

0 commit comments

Comments
 (0)