Skip to content

Commit 2a13dd0

Browse files
authored
Enable platformOptions to be considered for ColumnDiff (PostgreSQL jsonb 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?
1 parent 8725a81 commit 2a13dd0

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/Platforms/PostgreSQLPlatform.php

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ public function getAlterTableSQL(TableDiff $diff): array
256256
|| $columnDiff->hasScaleChanged()
257257
|| $columnDiff->hasFixedChanged()
258258
|| $columnDiff->hasLengthChanged()
259+
|| $columnDiff->hasPlatformOptionsChanged()
259260
) {
260261
$type = $newColumn->getType();
261262

src/Schema/ColumnDiff.php

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function countChangedProperties(): int
2828
+ (int) $this->hasNotNullChanged()
2929
+ (int) $this->hasNameChanged()
3030
+ (int) $this->hasTypeChanged()
31+
+ (int) $this->hasPlatformOptionsChanged()
3132
+ (int) $this->hasCommentChanged();
3233
}
3334

@@ -124,6 +125,13 @@ public function hasCommentChanged(): bool
124125
});
125126
}
126127

128+
public function hasPlatformOptionsChanged(): bool
129+
{
130+
return $this->hasPropertyChanged(static function (Column $column): array {
131+
return $column->getPlatformOptions();
132+
});
133+
}
134+
127135
private function hasPropertyChanged(callable $property): bool
128136
{
129137
return $property($this->newColumn) !== $property($this->oldColumn);

tests/Functional/Schema/PostgreSQL/ComparatorTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ public function testCompareBinariesOfDifferentLength(): void
7272
});
7373
}
7474

75+
public function testPlatformOptionsChangedColumnComparison(): void
76+
{
77+
$table = new Table('update_json_to_jsonb_table');
78+
$table->addColumn('test', Types::JSON);
79+
80+
$onlineTable = clone $table;
81+
$table->getColumn('test')
82+
->setPlatformOption('jsonb', true);
83+
84+
$compareResult = $this->comparator->compareTables($onlineTable, $table);
85+
self::assertCount(1, $compareResult->getChangedColumns());
86+
self::assertCount(1, $compareResult->getModifiedColumns());
87+
88+
$changedColumn = $compareResult->getChangedColumns()['test'];
89+
90+
self::assertTrue($changedColumn->hasPlatformOptionsChanged());
91+
self::assertEquals(1, $changedColumn->countChangedProperties());
92+
}
93+
7594
private function testColumnModification(callable $addColumn, callable $modifyColumn): void
7695
{
7796
$table = new Table('comparator_test');

tests/Platforms/PostgreSQLPlatformTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
99
use Doctrine\DBAL\Schema\Column;
10+
use Doctrine\DBAL\Schema\ColumnDiff;
1011
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1112
use Doctrine\DBAL\Schema\Sequence;
1213
use Doctrine\DBAL\Schema\Table;
14+
use Doctrine\DBAL\Schema\TableDiff;
1315
use Doctrine\DBAL\TransactionIsolationLevel;
1416
use Doctrine\DBAL\Types\Type;
1517
use Doctrine\DBAL\Types\Types;
@@ -791,4 +793,46 @@ public function testGetListSequencesSQL(): void
791793
$this->platform->getListSequencesSQL('test_db'),
792794
);
793795
}
796+
797+
public function testAlterTableChangeJsonToJsonb(): void
798+
{
799+
$table = new Table('mytable');
800+
$table->addColumn('payload', Types::JSON);
801+
802+
$tableDiff = new TableDiff($table, changedColumns: [
803+
'payload' => new ColumnDiff(
804+
$table->getColumn('payload'),
805+
(new Column(
806+
'payload',
807+
Type::getType(Types::JSON),
808+
))->setPlatformOption('jsonb', true),
809+
),
810+
]);
811+
812+
self::assertSame(
813+
['ALTER TABLE mytable ALTER payload TYPE JSONB'],
814+
$this->platform->getAlterTableSQL($tableDiff),
815+
);
816+
}
817+
818+
public function testAlterTableChangeJsonbToJson(): void
819+
{
820+
$table = new Table('mytable');
821+
$table->addColumn('payload', Types::JSON)->setPlatformOption('jsonb', true);
822+
823+
$tableDiff = new TableDiff($table, changedColumns: [
824+
'payload' => new ColumnDiff(
825+
$table->getColumn('payload'),
826+
(new Column(
827+
'payload',
828+
Type::getType(Types::JSON),
829+
)),
830+
),
831+
]);
832+
833+
self::assertSame(
834+
['ALTER TABLE mytable ALTER payload TYPE JSON'],
835+
$this->platform->getAlterTableSQL($tableDiff),
836+
);
837+
}
794838
}

0 commit comments

Comments
 (0)