From 770202319147a9626003ce91c3209df88f6cbbff Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Mon, 21 May 2018 12:49:26 +0100 Subject: [PATCH 1/2] correctly produce alter table sql to update column comment under postgres --- .../DBAL/Platforms/PostgreSqlPlatform.php | 10 ++++++-- .../AbstractPostgreSqlPlatformTestCase.php | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 165b40e0ed3..7643bd67df0 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -580,11 +580,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') || ($columnDiff->fromColumn !== null && $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 5c0da268107..b7312f376ad 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php @@ -853,6 +853,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} */ From 8a7208d18072b187bd879bb8910af95afdb76bb7 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Sat, 16 Jun 2018 17:34:58 +0100 Subject: [PATCH 2/2] refactor out getting old column comment --- lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 7643bd67df0..242c0fd4f28 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -580,11 +580,8 @@ public function getAlterTableSQL(TableDiff $diff) } } - $oldComment = null; $newComment = $this->getColumnComment($column); - if (null !== $columnDiff->fromColumn) { - $oldComment = $this->getColumnComment($columnDiff->fromColumn); - } + $oldComment = $this->getOldColumnComment($columnDiff); if ($columnDiff->hasChanged('comment') || ($columnDiff->fromColumn !== null && $oldComment !== $newComment)) { $commentsSQL[] = $this->getCommentOnColumnSQL( @@ -1254,4 +1251,9 @@ private function isNumericType(Type $type) : bool { return $type instanceof IntegerType || $type instanceof BigIntType; } + + private function getOldColumnComment(ColumnDiff $columnDiff) : ?string + { + return $columnDiff->fromColumn ? $this->getColumnComment($columnDiff->fromColumn) : null; + } }