Skip to content

Commit fca4067

Browse files
fix changing columns
1 parent b71611f commit fca4067

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/Schema/Grammars/GrammarTypes.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Connection;
88
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
99
use Illuminate\Support\Arr;
10+
use Illuminate\Support\Facades\App;
1011
use Illuminate\Support\Fluent;
1112
use Spatie\Regex\Regex;
1213

@@ -17,6 +18,42 @@ trait GrammarTypes
1718
*/
1819
public function compileChange(BaseBlueprint $blueprint, Fluent $command, Connection $connection): array
1920
{
21+
if (version_compare(App::version(), '11.15.0', '>=')) {
22+
$queries = [];
23+
24+
$changedColumn = $command->column;
25+
26+
if (filled($changedColumn['compression'])) {
27+
$queries[] = sprintf(
28+
'alter table %s alter %s set compression %s',
29+
$this->wrapTable($blueprint->getTable()),
30+
$this->wrap($changedColumn['name']),
31+
$this->wrap($changedColumn['compression']),
32+
);
33+
}
34+
35+
$changedColumn->offsetUnset('compression');
36+
37+
$sql = parent::compileChange($blueprint, $command, $connection);
38+
$regex = Regex::match('/^ALTER table (?P<table>.*?) alter (column )?(?P<column>.*?) type (?P<type>\w+)(?P<modifiers>,.*)?/i', $sql);
39+
40+
if (filled($changedColumn['using']) && $regex->hasMatch()) {
41+
$using = match ($connection->getSchemaGrammar()->isExpression($changedColumn['using'])) {
42+
true => $connection->getSchemaGrammar()->getValue($changedColumn['using']),
43+
false => $changedColumn['using'],
44+
};
45+
46+
$queries[] = match (filled($modifiers = $regex->groupOr('modifiers', ''))) {
47+
true => "alter table {$regex->group('table')} alter column {$regex->group('column')} type {$regex->group('type')} using {$using}{$modifiers}",
48+
false => "alter table {$regex->group('table')} alter column {$regex->group('column')} type {$regex->group('type')} using {$using}",
49+
};
50+
} else {
51+
$queries[] = $sql;
52+
}
53+
54+
return array_reverse($queries);
55+
}
56+
2057
$queries = [];
2158

2259
// The table prefix is accessed differently based on Laravel version. In old version the $prefix was public,
@@ -32,13 +69,7 @@ public function compileChange(BaseBlueprint $blueprint, Fluent $command, Connect
3269
Arr::except($changedColumn->toArray(), ['compression']),
3370
);
3471

35-
// Remove Compression modifier because Laravel 11 won't work correctly with it (migrator builts incorrectly SQL).
36-
$_modifiers = $this->modifiers;
37-
$this->modifiers = array_filter($this->modifiers, fn ($str) => !\in_array($str, ['Compression']));
38-
$changes = Arr::wrap(parent::compileChange($blueprintColumn, $command, $connection));
39-
$this->modifiers = $_modifiers;
40-
41-
foreach ($changes as $sql) {
72+
foreach (Arr::wrap(parent::compileChange($blueprintColumn, $command, $connection)) as $sql) {
4273
$regex = Regex::match('/^ALTER table (?P<table>.*?) alter (column )?(?P<column>.*?) type (?P<type>\w+)(?P<modifiers>,.*)?/i', $sql);
4374

4475
if (filled($changedColumn['using']) && $regex->hasMatch()) {

0 commit comments

Comments
 (0)