Skip to content

Commit eaac068

Browse files
committed
laravel 11 compatability
1 parent 501304d commit eaac068

14 files changed

+105
-71
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
"illuminate/container": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
2020
"illuminate/database": "^6.0|^7.0|^8.79|^9.0|^10.0|^11.0",
2121
"illuminate/events": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
22-
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0"
22+
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
23+
"spatie/regex": "^2.0|^3.0"
2324
},
2425
"require-dev": {
26+
"composer/semver": "^3.4",
2527
"friendsofphp/php-cs-fixer": "^2.19.3|^3.5.0",
2628
"nunomaduro/larastan": "^1.0|^2.1",
2729
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0|^9.0",

src/Schema/Grammars/GrammarTypes.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use Illuminate\Database\Connection;
88
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
9+
use Illuminate\Support\Arr;
910
use Illuminate\Support\Fluent;
10-
use Illuminate\Support\Str;
11-
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
11+
use Spatie\Regex\Regex;
1212

1313
trait GrammarTypes
1414
{
@@ -26,24 +26,33 @@ public function compileChange(BaseBlueprint $blueprint, Fluent $command, Connect
2626

2727
foreach ($blueprint->getChangedColumns() as $changedColumn) {
2828
$blueprintColumn = new BaseBlueprint($blueprint->getTable(), null, $prefix);
29-
$blueprintColumn->addColumn($changedColumn['type'], $changedColumn['name'], $changedColumn->toArray());
29+
$blueprintColumn->addColumn(
30+
$changedColumn['type'],
31+
$changedColumn['name'],
32+
Arr::except($changedColumn->toArray(), ['compression']),
33+
);
3034

31-
foreach (parent::compileChange($blueprintColumn, $command, $connection) as $sql) {
32-
if (filled($changedColumn['using']) && Str::is('ALTER TABLE * ALTER * TYPE *', $sql)) {
35+
foreach (Arr::wrap(parent::compileChange($blueprintColumn, $command, $connection)) as $sql) {
36+
$regex = Regex::match('/^ALTER table (?P<table>.*?) alter (column )?(?P<column>.*?) type (?P<type>\w+)(?P<modifiers>,.*)?/i', $sql);
37+
38+
if (filled($changedColumn['using']) && $regex->hasMatch()) {
3339
$using = match ($connection->getSchemaGrammar()->isExpression($changedColumn['using'])) {
3440
true => $connection->getSchemaGrammar()->getValue($changedColumn['using']),
3541
false => $changedColumn['using'],
3642
};
3743

38-
$queries[] = "{$sql} USING {$using}";
44+
$queries[] = match (filled($modifiers = $regex->groupOr('modifiers', ''))) {
45+
true => "alter table {$regex->group('table')} alter column {$regex->group('column')} type {$regex->group('type')} using {$using}{$modifiers}",
46+
false => "alter table {$regex->group('table')} alter column {$regex->group('column')} type {$regex->group('type')} using {$using}",
47+
};
3948
} else {
4049
$queries[] = $sql;
4150
}
4251
}
4352

4453
if (filled($changedColumn['compression'])) {
4554
$queries[] = sprintf(
46-
'ALTER TABLE %s ALTER %s SET COMPRESSION %s',
55+
'alter table %s alter %s set compression %s',
4756
$this->wrapTable($blueprint->getTable()),
4857
$this->wrap($changedColumn['name']),
4958
$this->wrap($changedColumn['compression']),
@@ -57,7 +66,7 @@ public function compileChange(BaseBlueprint $blueprint, Fluent $command, Connect
5766
/**
5867
* Get the SQL for a default column modifier.
5968
*/
60-
protected function modifyCompression(Blueprint $blueprint, Fluent $column): ?string
69+
protected function modifyCompression(BaseBlueprint $blueprint, Fluent $column): ?string
6170
{
6271
if (filled($column['compression'])) {
6372
return " compression {$column['compression']}";

tests/Connection/ReturningTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Tests\Connection;
66

7+
use Composer\Semver\Comparator;
78
use Tpetry\PostgresqlEnhanced\Tests\TestCase;
89

910
class ReturningTest extends TestCase
@@ -29,7 +30,7 @@ public function testExecutesNothingOnPretend(): void
2930
});
3031

3132
// The pretend mode has been changed in Laravel 10.30.0 to include the bindings in the query string
32-
if (version_compare($this->app->version(), '10.30.0', '>=')) {
33+
if (Comparator::greaterThanOrEqualTo($this->app->version(), '10.30.0')) {
3334
$this->assertEquals(["update example set str = 'IS7PD2jn' where str = '8lnreu2H' returning str"], array_column($queries, 'query'));
3435
} else {
3536
$this->assertEquals(['update example set str = ? where str = ? returning str'], array_column($queries, 'query'));

tests/Eloquent/BuilderReturningTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tpetry\PostgresqlEnhanced\Tests\Eloquent;
66

77
use Carbon\Carbon;
8+
use Composer\Semver\Comparator;
89
use Illuminate\Database\Eloquent\Collection;
910
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -307,7 +308,7 @@ public function testInsertUsingReturningSelection(): void
307308

308309
public function testUpdateFromReturningEmpty(): void
309310
{
310-
if (version_compare($this->app->version(), '8.65.0', '<')) {
311+
if (Comparator::lessThan($this->app->version(), '8.65.0')) {
311312
$this->markTestSkipped('UpdateFrom() has been added in a later Laravel version.');
312313
}
313314

@@ -326,7 +327,7 @@ public function testUpdateFromReturningEmpty(): void
326327

327328
public function testUpdateFromReturningSelection(): void
328329
{
329-
if (version_compare($this->app->version(), '8.65.0', '<')) {
330+
if (Comparator::lessThan($this->app->version(), '8.65.0')) {
330331
$this->markTestSkipped('UpdateFrom() has been added in a later Laravel version.');
331332
}
332333

@@ -493,7 +494,7 @@ public function testUpdateReturningSelectionwithTimestamps(): void
493494

494495
public function testUpsertReturningInsertAll(): void
495496
{
496-
if (version_compare($this->app->version(), '8.10.0', '<')) {
497+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
497498
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
498499
}
499500

@@ -511,7 +512,7 @@ public function testUpsertReturningInsertAll(): void
511512

512513
public function testUpsertReturningInsertAllWithTimestamps(): void
513514
{
514-
if (version_compare($this->app->version(), '8.10.0', '<')) {
515+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
515516
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
516517
}
517518

@@ -529,7 +530,7 @@ public function testUpsertReturningInsertAllWithTimestamps(): void
529530

530531
public function testUpsertReturningInsertSelection(): void
531532
{
532-
if (version_compare($this->app->version(), '8.10.0', '<')) {
533+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
533534
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
534535
}
535536

@@ -547,7 +548,7 @@ public function testUpsertReturningInsertSelection(): void
547548

548549
public function testUpsertReturningInsertSelectionWithTimestamps(): void
549550
{
550-
if (version_compare($this->app->version(), '8.10.0', '<')) {
551+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
551552
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
552553
}
553554

@@ -565,7 +566,7 @@ public function testUpsertReturningInsertSelectionWithTimestamps(): void
565566

566567
public function testUpsertReturningUpsertAll(): void
567568
{
568-
if (version_compare($this->app->version(), '8.10.0', '<')) {
569+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
569570
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
570571
}
571572

@@ -583,7 +584,7 @@ public function testUpsertReturningUpsertAll(): void
583584

584585
public function testUpsertReturningUpsertAllWithTimestamps(): void
585586
{
586-
if (version_compare($this->app->version(), '8.10.0', '<')) {
587+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
587588
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
588589
}
589590

@@ -601,7 +602,7 @@ public function testUpsertReturningUpsertAllWithTimestamps(): void
601602

602603
public function testUpsertReturningUpsertSelection(): void
603604
{
604-
if (version_compare($this->app->version(), '8.10.0', '<')) {
605+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
605606
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
606607
}
607608

@@ -619,7 +620,7 @@ public function testUpsertReturningUpsertSelection(): void
619620

620621
public function testUpsertReturningUpsertSelectionWithTimestamps(): void
621622
{
622-
if (version_compare($this->app->version(), '8.10.0', '<')) {
623+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
623624
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
624625
}
625626

@@ -637,7 +638,7 @@ public function testUpsertReturningUpsertSelectionWithTimestamps(): void
637638

638639
public function testUpsertReturningWithEmptyValuesAndNullUpdates(): void
639640
{
640-
if (version_compare($this->app->version(), '8.10.0', '<')) {
641+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
641642
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
642643
}
643644

@@ -654,7 +655,7 @@ public function testUpsertReturningWithEmptyValuesAndNullUpdates(): void
654655

655656
public function testUpsertReturningWithNullUpdates(): void
656657
{
657-
if (version_compare($this->app->version(), '8.10.0', '<')) {
658+
if (Comparator::lessThan($this->app->version(), '8.10.0')) {
658659
$this->markTestSkipped('Upsert() has been added in a later Laravel version.');
659660
}
660661

tests/Eloquent/IntegerArrayCastTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Tests\Eloquent;
66

7+
use Composer\Semver\Comparator;
78
use Illuminate\Database\Eloquent\Model;
89
use Tpetry\PostgresqlEnhanced\Eloquent\Casts\IntegerArrayCast;
910
use Tpetry\PostgresqlEnhanced\Tests\TestCase;
@@ -12,7 +13,7 @@ class IntegerArrayCastTest extends TestCase
1213
{
1314
public function testParseFromDatabaseValue(): void
1415
{
15-
if (version_compare($this->app->version(), '8.0.0', '<')) {
16+
if (Comparator::lessThan($this->app->version(), '8.0.0')) {
1617
$this->markTestSkipped('Cast classes have been added with Laravel 8.');
1718
}
1819

@@ -27,7 +28,7 @@ public function testParseFromDatabaseValue(): void
2728

2829
public function testTransformToDatabaseValue(): void
2930
{
30-
if (version_compare($this->app->version(), '8.0.0', '<')) {
31+
if (Comparator::lessThan($this->app->version(), '8.0.0')) {
3132
$this->markTestSkipped('Cast classes have been added with Laravel 8.');
3233
}
3334

tests/Eloquent/VectorArrayCastTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Tests\Eloquent;
66

7+
use Composer\Semver\Comparator;
78
use Illuminate\Database\Eloquent\Model;
89
use Tpetry\PostgresqlEnhanced\Eloquent\Casts\VectorArray;
910
use Tpetry\PostgresqlEnhanced\Tests\TestCase;
@@ -12,7 +13,7 @@ class VectorArrayCastTest extends TestCase
1213
{
1314
public function testParseFromDatabaseValue(): void
1415
{
15-
if (version_compare($this->app->version(), '8.0.0', '<')) {
16+
if (Comparator::lessThan($this->app->version(), '8.0.0')) {
1617
$this->markTestSkipped('Cast classes have been added with Laravel 8.');
1718
}
1819

@@ -26,7 +27,7 @@ public function testParseFromDatabaseValue(): void
2627

2728
public function testTransformToDatabaseValue(): void
2829
{
29-
if (version_compare($this->app->version(), '8.0.0', '<')) {
30+
if (Comparator::lessThan($this->app->version(), '8.0.0')) {
3031
$this->markTestSkipped('Cast classes have been added with Laravel 8.');
3132
}
3233

tests/Migration/FunctionTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Tests\Migration;
66

7+
use Composer\Semver\Comparator;
78
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
89
use Tpetry\PostgresqlEnhanced\Tests\TestCase;
910

@@ -49,7 +50,7 @@ public function testCreateFunctionLanguagePlpgsql(): void
4950

5051
public function testCreateFunctionLanguageSql(): void
5152
{
52-
if (version_compare($this->getConnection()->serverVersion(), '14') < 0) {
53+
if (Comparator::lessThan($this->getConnection()->serverVersion(), '14')) {
5354
$this->markTestSkipped('SQL function bodies are first supported with PostgreSQL 14.');
5455
}
5556

@@ -61,7 +62,7 @@ public function testCreateFunctionLanguageSql(): void
6162

6263
public function testCreateFunctionLanguageSqlExpression(): void
6364
{
64-
if (version_compare($this->getConnection()->serverVersion(), '14') < 0) {
65+
if (Comparator::lessThan($this->getConnection()->serverVersion(), '14')) {
6566
$this->markTestSkipped('SQL function bodies are first supported with PostgreSQL 14.');
6667
}
6768

@@ -73,7 +74,7 @@ public function testCreateFunctionLanguageSqlExpression(): void
7374

7475
public function testCreateFunctionLanguageSqlExpressionPg13(): void
7576
{
76-
if (version_compare($this->getConnection()->serverVersion(), '14') >= 0) {
77+
if (Comparator::greaterThanOrEqualTo($this->getConnection()->serverVersion(), '14')) {
7778
$this->markTestSkipped('SQL function bodies are supported with PostgreSQL 14 and will be preferred.');
7879
}
7980

@@ -85,7 +86,7 @@ public function testCreateFunctionLanguageSqlExpressionPg13(): void
8586

8687
public function testCreateFunctionLanguageSqlPg13(): void
8788
{
88-
if (version_compare($this->getConnection()->serverVersion(), '14') >= 0) {
89+
if (Comparator::greaterThanOrEqualTo($this->getConnection()->serverVersion(), '14')) {
8990
$this->markTestSkipped('SQL function bodies are supported with PostgreSQL 14 and will be preferred.');
9091
}
9192

@@ -160,7 +161,7 @@ public function testCreateFunctionOrReplaceLanguagePlpgsql(): void
160161

161162
public function testCreateFunctionOrReplaceLanguageSql(): void
162163
{
163-
if (version_compare($this->getConnection()->serverVersion(), '14') < 0) {
164+
if (Comparator::lessThan($this->getConnection()->serverVersion(), '14')) {
164165
$this->markTestSkipped('SQL function bodies are first supported with PostgreSQL 14.');
165166
}
166167

@@ -172,7 +173,7 @@ public function testCreateFunctionOrReplaceLanguageSql(): void
172173

173174
public function testCreateFunctionOrReplaceLanguageSqlExpression(): void
174175
{
175-
if (version_compare($this->getConnection()->serverVersion(), '14') < 0) {
176+
if (Comparator::lessThan($this->getConnection()->serverVersion(), '14')) {
176177
$this->markTestSkipped('SQL function bodies are first supported with PostgreSQL 14.');
177178
}
178179

@@ -184,7 +185,7 @@ public function testCreateFunctionOrReplaceLanguageSqlExpression(): void
184185

185186
public function testCreateFunctionOrReplaceLanguageSqlExpressionPg13(): void
186187
{
187-
if (version_compare($this->getConnection()->serverVersion(), '14') >= 0) {
188+
if (Comparator::greaterThanOrEqualTo($this->getConnection()->serverVersion(), '14')) {
188189
$this->markTestSkipped('SQL function bodies are supported with PostgreSQL 14 and will be preferred.');
189190
}
190191

@@ -196,7 +197,7 @@ public function testCreateFunctionOrReplaceLanguageSqlExpressionPg13(): void
196197

197198
public function testCreateFunctionOrReplaceLanguageSqlPg13(): void
198199
{
199-
if (version_compare($this->getConnection()->serverVersion(), '14') >= 0) {
200+
if (Comparator::greaterThanOrEqualTo($this->getConnection()->serverVersion(), '14')) {
200201
$this->markTestSkipped('SQL function bodies are supported with PostgreSQL 14 and will be preferred.');
201202
}
202203

tests/Migration/IndexColumnsTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Tests\Migration;
66

7+
use Composer\Semver\Comparator;
78
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
89
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
910
use Tpetry\PostgresqlEnhanced\Tests\TestCase;
@@ -64,7 +65,7 @@ public function testIndexPlainColumn(): void
6465

6566
public function testRawIndexEscapedColumn(): void
6667
{
67-
if (version_compare($this->app->version(), '7.7.0', '<')) {
68+
if (Comparator::lessThan($this->app->version(), '7.7.0')) {
6869
$this->markTestSkipped('Raw indexes have been added in a later Laravel version.');
6970
}
7071

@@ -81,7 +82,7 @@ public function testRawIndexEscapedColumn(): void
8182

8283
public function testRawIndexFunctionalExpression(): void
8384
{
84-
if (version_compare($this->app->version(), '7.7.0', '<')) {
85+
if (Comparator::lessThan($this->app->version(), '7.7.0')) {
8586
$this->markTestSkipped('Raw indexes have been added in a later Laravel version.');
8687
}
8788

@@ -98,7 +99,7 @@ public function testRawIndexFunctionalExpression(): void
9899

99100
public function testRawIndexParametrized(): void
100101
{
101-
if (version_compare($this->app->version(), '7.7.0', '<')) {
102+
if (Comparator::lessThan($this->app->version(), '7.7.0')) {
102103
$this->markTestSkipped('Raw indexes have been added in a later Laravel version.');
103104
}
104105

@@ -115,7 +116,7 @@ public function testRawIndexParametrized(): void
115116

116117
public function testRawIndexUnescapedColumn(): void
117118
{
118-
if (version_compare($this->app->version(), '7.7.0', '<')) {
119+
if (Comparator::lessThan($this->app->version(), '7.7.0')) {
119120
$this->markTestSkipped('Raw indexes have been added in a later Laravel version.');
120121
}
121122

0 commit comments

Comments
 (0)