Skip to content

Commit 13aaf2a

Browse files
committed
Merge branch '4.2.x' into 4.3.x
* 4.2.x: Enable platformOptions to be considered for ColumnDiff (PostgreSQL jsonb support) (doctrine#6693)
2 parents 804103e + 2a13dd0 commit 13aaf2a

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
@@ -257,6 +257,7 @@ public function getAlterTableSQL(TableDiff $diff): array
257257
|| $columnDiff->hasScaleChanged()
258258
|| $columnDiff->hasFixedChanged()
259259
|| $columnDiff->hasLengthChanged()
260+
|| $columnDiff->hasPlatformOptionsChanged()
260261
) {
261262
$type = $newColumn->getType();
262263

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)