Skip to content

Commit b71611f

Browse files
fix adding columns
1 parent d70c72a commit b71611f

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Schema/Grammars/GrammarTable.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tpetry\PostgresqlEnhanced\Schema\Grammars;
66

77
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Facades\App;
89
use Illuminate\Support\Fluent;
910

1011
trait GrammarTable
@@ -16,6 +17,37 @@ trait GrammarTable
1617
*/
1718
public function compileAdd(Blueprint $blueprint, Fluent $command): array
1819
{
20+
if (version_compare(App::version(), '11.15.0', '>=')) {
21+
$sql = [];
22+
23+
$column = $command->column;
24+
$attributes = $column->getAttributes();
25+
26+
if (\array_key_exists('initial', $attributes)) {
27+
if (\array_key_exists('default', $attributes)) {
28+
$sql[] = sprintf('alter table %s alter column %s set default %s',
29+
$this->wrapTable($blueprint),
30+
$this->wrap($column),
31+
$this->getDefaultValue($column['default'])
32+
);
33+
} else {
34+
$sql[] = sprintf('alter table %s alter column %s drop default',
35+
$this->wrapTable($blueprint),
36+
$this->wrap($column),
37+
);
38+
}
39+
40+
$column['default'] = $column['initial'];
41+
}
42+
43+
$sql[] = sprintf('alter table %s add column %s',
44+
$this->wrapTable($blueprint),
45+
$this->getColumn($blueprint, $command->column)
46+
);
47+
48+
return array_reverse($sql);
49+
}
50+
1951
$sql = [];
2052

2153
foreach (array_reverse($blueprint->getAddedColumns()) as $column) {

tests/Migration/TableTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ public function testAddColumnInitialWithDefault(): void
4444
'alter table "test" alter column "col_437065" set default \'val_121964\'',
4545
], array_column($queries, 'query'));
4646
}
47+
48+
public function testAddColumnsInitialWithDefault(): void
49+
{
50+
$queries = $this->withQueryLog(function (): void {
51+
Schema::table('test', function (Blueprint $table): void {
52+
$table->text('col_123')->initial('val_098');
53+
$table->text('col_456')->initial('val_765')->default('val_432');
54+
});
55+
});
56+
57+
$this->assertEquals([
58+
'alter table "test" add column "col_123" text not null default \'val_098\'',
59+
'alter table "test" alter column "col_123" drop default',
60+
'alter table "test" add column "col_456" text not null default \'val_765\'',
61+
'alter table "test" alter column "col_456" set default \'val_432\'',
62+
], array_column($queries, 'query'));
63+
}
4764
}

0 commit comments

Comments
 (0)