From 81c9bb167dc359b7c6beceb02301302723786f54 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Mon, 21 May 2018 12:49:26 +0100 Subject: [PATCH] correctly produce alter table sql to update column comment under postgres --- .../DBAL/Platforms/PostgreSqlPlatform.php | 12 +++++++--- .../AbstractPostgreSqlPlatformTestCase.php | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 2cb0bf56f82..65c96b01c0f 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -25,8 +25,8 @@ use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\TableDiff; -use Doctrine\DBAL\Types\BinaryType; use Doctrine\DBAL\Types\BigIntType; +use Doctrine\DBAL\Types\BinaryType; use Doctrine\DBAL\Types\BlobType; use Doctrine\DBAL\Types\IntegerType; use Doctrine\DBAL\Types\Type; @@ -591,11 +591,17 @@ public function getAlterTableSQL(TableDiff $diff) } } - if ($columnDiff->hasChanged('comment')) { + $oldComment = null; + $newComment = $this->getColumnComment($column); + if (null !== $columnDiff->fromColumn) { + $oldComment = $this->getColumnComment($columnDiff->fromColumn); + } + + if ($columnDiff->hasChanged('comment') || (null !== $columnDiff->fromColumn && $oldComment !== $newComment)) { $commentsSQL[] = $this->getCommentOnColumnSQL( $diff->getName($this)->getQuotedName($this), $column->getQuotedName($this), - $this->getColumnComment($column) + $newComment ); } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php index 029092ff340..54da40445bf 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php @@ -819,6 +819,29 @@ public function testAltersTableColumnCommentWithExplicitlyQuotedIdentifiers() ); } + /** + * @group 3158 + */ + public function testAltersTableColumnCommentIfRequiredByType() + { + $table1 = new Table('"foo"', [new Column('"bar"', Type::getType('datetime'))]); + $table2 = new Table('"foo"', [new Column('"bar"', Type::getType('datetime_immutable'))]); + + $comparator = new Comparator(); + + $tableDiff = $comparator->diffTable($table1, $table2); + + $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff); + $this->assertSame( + [ + 'ALTER TABLE "foo" ALTER "bar" TYPE TIMESTAMP(0) WITHOUT TIME ZONE', + 'ALTER TABLE "foo" ALTER "bar" DROP DEFAULT', + 'COMMENT ON COLUMN "foo"."bar" IS \'(DC2Type:datetime_immutable)\'', + ], + $this->_platform->getAlterTableSQL($tableDiff) + ); + } + /** * {@inheritdoc} */