Skip to content

Commit

Permalink
Merge pull request #3158 from bendavies/postgres-alter-table-column-c…
Browse files Browse the repository at this point in the history
…omment

postgres: correctly produce alter table sql comment to update column definition
  • Loading branch information
morozov authored Oct 16, 2018
2 parents 6f627e6 + 8a7208d commit ff30f9a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,14 @@ public function getAlterTableSQL(TableDiff $diff)
}
}

if ($columnDiff->hasChanged('comment')) {
$newComment = $this->getColumnComment($column);
$oldComment = $this->getOldColumnComment($columnDiff);

if ($columnDiff->hasChanged('comment') || ($columnDiff->fromColumn !== null && $oldComment !== $newComment)) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->getName($this)->getQuotedName($this),
$column->getQuotedName($this),
$this->getColumnComment($column)
$newComment
);
}

Expand Down Expand Up @@ -1248,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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down

0 comments on commit ff30f9a

Please sign in to comment.