7
7
use Illuminate \Database \Connection ;
8
8
use Illuminate \Database \Schema \Blueprint as BaseBlueprint ;
9
9
use Illuminate \Support \Arr ;
10
+ use Illuminate \Support \Facades \App ;
10
11
use Illuminate \Support \Fluent ;
11
12
use Spatie \Regex \Regex ;
12
13
@@ -17,6 +18,42 @@ trait GrammarTypes
17
18
*/
18
19
public function compileChange (BaseBlueprint $ blueprint , Fluent $ command , Connection $ connection ): array
19
20
{
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
+
20
57
$ queries = [];
21
58
22
59
// 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
32
69
Arr::except ($ changedColumn ->toArray (), ['compression ' ]),
33
70
);
34
71
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 ) {
42
73
$ regex = Regex::match ('/^ALTER table (?P<table>.*?) alter (column )?(?P<column>.*?) type (?P<type>\w+)(?P<modifiers>,.*)?/i ' , $ sql );
43
74
44
75
if (filled ($ changedColumn ['using ' ]) && $ regex ->hasMatch ()) {
0 commit comments