Skip to content

Fix adding / changing columns #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Schema/Grammars/GrammarTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tpetry\PostgresqlEnhanced\Schema\Grammars;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Fluent;

trait GrammarTable
Expand All @@ -16,6 +17,37 @@ trait GrammarTable
*/
public function compileAdd(Blueprint $blueprint, Fluent $command): array
{
if (version_compare(App::version(), '11.15.0', '>=')) {
$sql = [];

$column = $command->column;
$attributes = $column->getAttributes();

if (\array_key_exists('initial', $attributes)) {
if (\array_key_exists('default', $attributes)) {
$sql[] = sprintf('alter table %s alter column %s set default %s',
$this->wrapTable($blueprint),
$this->wrap($column),
$this->getDefaultValue($column['default'])
);
} else {
$sql[] = sprintf('alter table %s alter column %s drop default',
$this->wrapTable($blueprint),
$this->wrap($column),
);
}

$column['default'] = $column['initial'];
}

$sql[] = sprintf('alter table %s add column %s',
$this->wrapTable($blueprint),
$this->getColumn($blueprint, $command->column)
);

return array_reverse($sql);
}

$sql = [];

foreach (array_reverse($blueprint->getAddedColumns()) as $column) {
Expand Down
45 changes: 38 additions & 7 deletions src/Schema/Grammars/GrammarTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Fluent;
use Spatie\Regex\Regex;

Expand All @@ -17,6 +18,42 @@ trait GrammarTypes
*/
public function compileChange(BaseBlueprint $blueprint, Fluent $command, Connection $connection): array
{
if (version_compare(App::version(), '11.15.0', '>=')) {
$queries = [];

$changedColumn = $command->column;

if (filled($changedColumn['compression'])) {
$queries[] = sprintf(
'alter table %s alter %s set compression %s',
$this->wrapTable($blueprint->getTable()),
$this->wrap($changedColumn['name']),
$this->wrap($changedColumn['compression']),
);
}

$changedColumn->offsetUnset('compression');

$sql = parent::compileChange($blueprint, $command, $connection);
$regex = Regex::match('/^ALTER table (?P<table>.*?) alter (column )?(?P<column>.*?) type (?P<type>\w+)(?P<modifiers>,.*)?/i', $sql);

if (filled($changedColumn['using']) && $regex->hasMatch()) {
$using = match ($connection->getSchemaGrammar()->isExpression($changedColumn['using'])) {
true => $connection->getSchemaGrammar()->getValue($changedColumn['using']),
false => $changedColumn['using'],
};

$queries[] = match (filled($modifiers = $regex->groupOr('modifiers', ''))) {
true => "alter table {$regex->group('table')} alter column {$regex->group('column')} type {$regex->group('type')} using {$using}{$modifiers}",
false => "alter table {$regex->group('table')} alter column {$regex->group('column')} type {$regex->group('type')} using {$using}",
};
} else {
$queries[] = $sql;
}

return array_reverse($queries);
}

$queries = [];

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

// Remove Compression modifier because Laravel 11 won't work correctly with it (migrator builts incorrectly SQL).
$_modifiers = $this->modifiers;
$this->modifiers = array_filter($this->modifiers, fn ($str) => !\in_array($str, ['Compression']));
$changes = Arr::wrap(parent::compileChange($blueprintColumn, $command, $connection));
$this->modifiers = $_modifiers;

foreach ($changes as $sql) {
foreach (Arr::wrap(parent::compileChange($blueprintColumn, $command, $connection)) as $sql) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverting changes of ad09702

$regex = Regex::match('/^ALTER table (?P<table>.*?) alter (column )?(?P<column>.*?) type (?P<type>\w+)(?P<modifiers>,.*)?/i', $sql);

if (filled($changedColumn['using']) && $regex->hasMatch()) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Migration/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@ public function testAddColumnInitialWithDefault(): void
'alter table "test" alter column "col_437065" set default \'val_121964\'',
], array_column($queries, 'query'));
}

public function testAddColumnsInitialWithDefault(): void
{
$queries = $this->withQueryLog(function (): void {
Schema::table('test', function (Blueprint $table): void {
$table->text('col_123')->initial('val_098');
$table->text('col_456')->initial('val_765')->default('val_432');
});
});

$this->assertEquals([
'alter table "test" add column "col_123" text not null default \'val_098\'',
'alter table "test" alter column "col_123" drop default',
'alter table "test" add column "col_456" text not null default \'val_765\'',
'alter table "test" alter column "col_456" set default \'val_432\'',
], array_column($queries, 'query'));
}
}