Skip to content

Commit

Permalink
Enable platformOptions to be considered for ColumnDiff (PostgreSQL js…
Browse files Browse the repository at this point in the history
…onb support) (#6693)

|      Q       |   A
|------------- | -----------
| Type         | bug
| Fixed issues | #2541

#### Summary

Changing `platformOptions` can also result in schema changes. In my
concrete example it was the `jsonb` option for the `JsonType`. I updated
first only PostgreSQLPlatform to get feedback if there are any pitfalls
in adding these `platformOptions` for consideration of schema changes,
wdyt?
  • Loading branch information
DanielBadura authored Jan 6, 2025
1 parent 8725a81 commit 2a13dd0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public function getAlterTableSQL(TableDiff $diff): array
|| $columnDiff->hasScaleChanged()
|| $columnDiff->hasFixedChanged()
|| $columnDiff->hasLengthChanged()
|| $columnDiff->hasPlatformOptionsChanged()
) {
$type = $newColumn->getType();

Expand Down
8 changes: 8 additions & 0 deletions src/Schema/ColumnDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function countChangedProperties(): int
+ (int) $this->hasNotNullChanged()
+ (int) $this->hasNameChanged()
+ (int) $this->hasTypeChanged()
+ (int) $this->hasPlatformOptionsChanged()
+ (int) $this->hasCommentChanged();
}

Expand Down Expand Up @@ -124,6 +125,13 @@ public function hasCommentChanged(): bool
});
}

public function hasPlatformOptionsChanged(): bool
{
return $this->hasPropertyChanged(static function (Column $column): array {
return $column->getPlatformOptions();
});
}

private function hasPropertyChanged(callable $property): bool
{
return $property($this->newColumn) !== $property($this->oldColumn);
Expand Down
19 changes: 19 additions & 0 deletions tests/Functional/Schema/PostgreSQL/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ public function testCompareBinariesOfDifferentLength(): void
});
}

public function testPlatformOptionsChangedColumnComparison(): void
{
$table = new Table('update_json_to_jsonb_table');
$table->addColumn('test', Types::JSON);

$onlineTable = clone $table;
$table->getColumn('test')
->setPlatformOption('jsonb', true);

$compareResult = $this->comparator->compareTables($onlineTable, $table);
self::assertCount(1, $compareResult->getChangedColumns());
self::assertCount(1, $compareResult->getModifiedColumns());

$changedColumn = $compareResult->getChangedColumns()['test'];

self::assertTrue($changedColumn->hasPlatformOptionsChanged());
self::assertEquals(1, $changedColumn->countChangedProperties());
}

private function testColumnModification(callable $addColumn, callable $modifyColumn): void
{
$table = new Table('comparator_test');
Expand Down
44 changes: 44 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -791,4 +793,46 @@ public function testGetListSequencesSQL(): void
$this->platform->getListSequencesSQL('test_db'),
);
}

public function testAlterTableChangeJsonToJsonb(): void
{
$table = new Table('mytable');
$table->addColumn('payload', Types::JSON);

$tableDiff = new TableDiff($table, changedColumns: [
'payload' => new ColumnDiff(
$table->getColumn('payload'),
(new Column(
'payload',
Type::getType(Types::JSON),
))->setPlatformOption('jsonb', true),
),
]);

self::assertSame(
['ALTER TABLE mytable ALTER payload TYPE JSONB'],
$this->platform->getAlterTableSQL($tableDiff),
);
}

public function testAlterTableChangeJsonbToJson(): void
{
$table = new Table('mytable');
$table->addColumn('payload', Types::JSON)->setPlatformOption('jsonb', true);

$tableDiff = new TableDiff($table, changedColumns: [
'payload' => new ColumnDiff(
$table->getColumn('payload'),
(new Column(
'payload',
Type::getType(Types::JSON),
)),
),
]);

self::assertSame(
['ALTER TABLE mytable ALTER payload TYPE JSON'],
$this->platform->getAlterTableSQL($tableDiff),
);
}
}

0 comments on commit 2a13dd0

Please sign in to comment.