Skip to content

Commit 38e4707

Browse files
committed
Do not use ColumnDiff::$changedProperties in PostgreSQLPlatform::getAlterTableSQL()
1 parent f84b9d9 commit 38e4707

File tree

2 files changed

+95
-35
lines changed

2 files changed

+95
-35
lines changed

src/Platforms/PostgreSQLPlatform.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
99
use Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords;
10-
use Doctrine\DBAL\Schema\ColumnDiff;
1110
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1211
use Doctrine\DBAL\Schema\Identifier;
1312
use Doctrine\DBAL\Schema\Index;
1413
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager;
1514
use Doctrine\DBAL\Schema\Sequence;
1615
use Doctrine\DBAL\Schema\TableDiff;
1716
use Doctrine\DBAL\TransactionIsolationLevel;
18-
use Doctrine\DBAL\Types\BinaryType;
19-
use Doctrine\DBAL\Types\BlobType;
2017
use UnexpectedValueException;
2118

22-
use function array_diff;
2319
use function array_merge;
2420
use function array_unique;
2521
use function array_values;
26-
use function count;
2722
use function explode;
2823
use function implode;
2924
use function in_array;
@@ -265,10 +260,6 @@ public function getAlterTableSQL(TableDiff $diff): array
265260
continue;
266261
}
267262

268-
if ($this->isUnchangedBinaryColumn($columnDiff)) {
269-
continue;
270-
}
271-
272263
$fromColumn = $columnDiff->fromColumn;
273264

274265
$oldColumnName = $fromColumn->getQuotedName($this);
@@ -372,32 +363,6 @@ public function getAlterTableSQL(TableDiff $diff): array
372363
return array_merge($sql, $tableSql, $columnSql);
373364
}
374365

375-
/**
376-
* Checks whether a given column diff is a logically unchanged binary type column.
377-
*
378-
* Used to determine whether a column alteration for a binary type column can be skipped.
379-
* Doctrine's {@see BinaryType} and {@see BlobType} are mapped to the same database column type on this platform
380-
* as this platform does not have a native VARBINARY/BINARY column type. Therefore the comparator
381-
* might detect differences for binary type columns which do not have to be propagated
382-
* to database as there actually is no difference at database level.
383-
*/
384-
private function isUnchangedBinaryColumn(ColumnDiff $columnDiff): bool
385-
{
386-
$columnType = $columnDiff->column->getType();
387-
388-
if (! $columnType instanceof BinaryType && ! $columnType instanceof BlobType) {
389-
return false;
390-
}
391-
392-
$fromColumnType = $columnDiff->fromColumn->getType();
393-
394-
if (! $fromColumnType instanceof BinaryType && ! $fromColumnType instanceof BlobType) {
395-
return false;
396-
}
397-
398-
return count(array_diff($columnDiff->changedProperties, ['type', 'length', 'fixed'])) === 0;
399-
}
400-
401366
/**
402367
* {@inheritdoc}
403368
*/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Schema\PostgreSQL;
6+
7+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
8+
use Doctrine\DBAL\Schema\AbstractSchemaManager;
9+
use Doctrine\DBAL\Schema\Column;
10+
use Doctrine\DBAL\Schema\Comparator;
11+
use Doctrine\DBAL\Schema\Table;
12+
use Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils;
13+
use Doctrine\DBAL\Tests\FunctionalTestCase;
14+
use Doctrine\DBAL\Types\Type;
15+
use Doctrine\DBAL\Types\Types;
16+
17+
final class ComparatorTest extends FunctionalTestCase
18+
{
19+
private AbstractSchemaManager $schemaManager;
20+
21+
private Comparator $comparator;
22+
23+
protected function setUp(): void
24+
{
25+
if (! $this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
26+
self::markTestSkipped('This test covers PostgreSQL-specific schema comparison scenarios.');
27+
}
28+
29+
$this->schemaManager = $this->connection->createSchemaManager();
30+
$this->comparator = $this->schemaManager->createComparator();
31+
}
32+
33+
/**
34+
* The PostgreSQL platform maps both BLOB and BINARY columns to the BYTEA column type.
35+
*
36+
* @see PostgreSQLPlatform::getBlobTypeDeclarationSQL()
37+
*/
38+
public function testCompareBinaryAndBlob(): void
39+
{
40+
$this->testColumnModification(static function (Table $table, string $name): Column {
41+
return $table->addColumn($name, Types::BINARY);
42+
}, static function (Column $column): void {
43+
$column->setType(Type::getType(Types::BLOB));
44+
});
45+
}
46+
47+
/**
48+
* The PostgreSQL platform maps both BINARY and VARBINARY columns to the BYTEA column type.
49+
*
50+
* @see PostgreSQLPlatform::getVarbinaryTypeDeclarationSQLSnippet()
51+
*/
52+
public function testCompareBinaryAndVarbinary(): void
53+
{
54+
$this->testColumnModification(static function (Table $table, string $name): Column {
55+
return $table->addColumn($name, Types::BINARY);
56+
}, static function (Column $column): void {
57+
$column->setFixed(true);
58+
});
59+
}
60+
61+
/**
62+
* The PostgreSQL platform disregards the "length" attribute of BINARY and VARBINARY columns.
63+
*
64+
* @see PostgreSQLPlatform::getBinaryTypeDeclarationSQLSnippet()
65+
*/
66+
public function testCompareBinariesOfDifferentLength(): void
67+
{
68+
$this->testColumnModification(static function (Table $table, string $name): Column {
69+
return $table->addColumn($name, Types::BINARY, ['length' => 16]);
70+
}, static function (Column $column): void {
71+
$column->setLength(32);
72+
});
73+
}
74+
75+
private function testColumnModification(callable $addColumn, callable $modifyColumn): void
76+
{
77+
$table = new Table('comparator_test');
78+
$column = $addColumn($table, 'id');
79+
$this->dropAndCreateTable($table);
80+
81+
$modifyColumn($column);
82+
83+
self::assertNull(ComparatorTestUtils::diffFromActualToDesiredTable(
84+
$this->schemaManager,
85+
$this->comparator,
86+
$table,
87+
));
88+
89+
self::assertNull(ComparatorTestUtils::diffFromDesiredToActualTable(
90+
$this->schemaManager,
91+
$this->comparator,
92+
$table,
93+
));
94+
}
95+
}

0 commit comments

Comments
 (0)