From 38e4707cb658cfa2eecd3d8a6cab015b3c9c568b Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 31 Aug 2022 19:49:20 -0700 Subject: [PATCH] Do not use ColumnDiff::$changedProperties in PostgreSQLPlatform::getAlterTableSQL() --- src/Platforms/PostgreSQLPlatform.php | 35 ------- .../Schema/PostgreSQL/ComparatorTest.php | 95 +++++++++++++++++++ 2 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 tests/Functional/Schema/PostgreSQL/ComparatorTest.php diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 15063665af6..db5545aee51 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -7,7 +7,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Platforms\Keywords\KeywordList; use Doctrine\DBAL\Platforms\Keywords\PostgreSQLKeywords; -use Doctrine\DBAL\Schema\ColumnDiff; use Doctrine\DBAL\Schema\ForeignKeyConstraint; use Doctrine\DBAL\Schema\Identifier; use Doctrine\DBAL\Schema\Index; @@ -15,15 +14,11 @@ use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\TransactionIsolationLevel; -use Doctrine\DBAL\Types\BinaryType; -use Doctrine\DBAL\Types\BlobType; use UnexpectedValueException; -use function array_diff; use function array_merge; use function array_unique; use function array_values; -use function count; use function explode; use function implode; use function in_array; @@ -265,10 +260,6 @@ public function getAlterTableSQL(TableDiff $diff): array continue; } - if ($this->isUnchangedBinaryColumn($columnDiff)) { - continue; - } - $fromColumn = $columnDiff->fromColumn; $oldColumnName = $fromColumn->getQuotedName($this); @@ -372,32 +363,6 @@ public function getAlterTableSQL(TableDiff $diff): array return array_merge($sql, $tableSql, $columnSql); } - /** - * Checks whether a given column diff is a logically unchanged binary type column. - * - * Used to determine whether a column alteration for a binary type column can be skipped. - * Doctrine's {@see BinaryType} and {@see BlobType} are mapped to the same database column type on this platform - * as this platform does not have a native VARBINARY/BINARY column type. Therefore the comparator - * might detect differences for binary type columns which do not have to be propagated - * to database as there actually is no difference at database level. - */ - private function isUnchangedBinaryColumn(ColumnDiff $columnDiff): bool - { - $columnType = $columnDiff->column->getType(); - - if (! $columnType instanceof BinaryType && ! $columnType instanceof BlobType) { - return false; - } - - $fromColumnType = $columnDiff->fromColumn->getType(); - - if (! $fromColumnType instanceof BinaryType && ! $fromColumnType instanceof BlobType) { - return false; - } - - return count(array_diff($columnDiff->changedProperties, ['type', 'length', 'fixed'])) === 0; - } - /** * {@inheritdoc} */ diff --git a/tests/Functional/Schema/PostgreSQL/ComparatorTest.php b/tests/Functional/Schema/PostgreSQL/ComparatorTest.php new file mode 100644 index 00000000000..522cfd64a9b --- /dev/null +++ b/tests/Functional/Schema/PostgreSQL/ComparatorTest.php @@ -0,0 +1,95 @@ +connection->getDatabasePlatform() instanceof PostgreSQLPlatform) { + self::markTestSkipped('This test covers PostgreSQL-specific schema comparison scenarios.'); + } + + $this->schemaManager = $this->connection->createSchemaManager(); + $this->comparator = $this->schemaManager->createComparator(); + } + + /** + * The PostgreSQL platform maps both BLOB and BINARY columns to the BYTEA column type. + * + * @see PostgreSQLPlatform::getBlobTypeDeclarationSQL() + */ + public function testCompareBinaryAndBlob(): void + { + $this->testColumnModification(static function (Table $table, string $name): Column { + return $table->addColumn($name, Types::BINARY); + }, static function (Column $column): void { + $column->setType(Type::getType(Types::BLOB)); + }); + } + + /** + * The PostgreSQL platform maps both BINARY and VARBINARY columns to the BYTEA column type. + * + * @see PostgreSQLPlatform::getVarbinaryTypeDeclarationSQLSnippet() + */ + public function testCompareBinaryAndVarbinary(): void + { + $this->testColumnModification(static function (Table $table, string $name): Column { + return $table->addColumn($name, Types::BINARY); + }, static function (Column $column): void { + $column->setFixed(true); + }); + } + + /** + * The PostgreSQL platform disregards the "length" attribute of BINARY and VARBINARY columns. + * + * @see PostgreSQLPlatform::getBinaryTypeDeclarationSQLSnippet() + */ + public function testCompareBinariesOfDifferentLength(): void + { + $this->testColumnModification(static function (Table $table, string $name): Column { + return $table->addColumn($name, Types::BINARY, ['length' => 16]); + }, static function (Column $column): void { + $column->setLength(32); + }); + } + + private function testColumnModification(callable $addColumn, callable $modifyColumn): void + { + $table = new Table('comparator_test'); + $column = $addColumn($table, 'id'); + $this->dropAndCreateTable($table); + + $modifyColumn($column); + + self::assertNull(ComparatorTestUtils::diffFromActualToDesiredTable( + $this->schemaManager, + $this->comparator, + $table, + )); + + self::assertNull(ComparatorTestUtils::diffFromDesiredToActualTable( + $this->schemaManager, + $this->comparator, + $table, + )); + } +}