Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow removing column comment on some platforms
  • Loading branch information
deeky666 committed Mar 28, 2013
1 parent 6e70bb1 commit ef57a35
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
18 changes: 15 additions & 3 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Expand Up @@ -571,9 +571,21 @@ public function getAlterTableSQL(TableDiff $diff)
}

$column = $columnDiff->column;
$fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
$columnHasChangedComment = $columnDiff->hasChanged('comment');

/**
* Do not add query part if only comment has changed
*/
if ( ! ($columnHasChangedComment && count($columnDiff->changedProperties) === 1)) {
$fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
}

if ($columnHasChangedComment) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->name,
$column->getName(),
$this->getColumnComment($column)
);
}
}

Expand Down
18 changes: 16 additions & 2 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Expand Up @@ -432,8 +432,12 @@ public function getAlterTableSQL(TableDiff $diff)
}
}

if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
if ($columnDiff->hasChanged('comment')) {
$commentsSQL[] = $this->getCommentOnColumnSQL(
$diff->name,
$column->getName(),
$this->getColumnComment($column)
);
}

if ($columnDiff->hasChanged('length')) {
Expand Down Expand Up @@ -463,6 +467,16 @@ public function getAlterTableSQL(TableDiff $diff)
return array_merge($sql, $tableSql, $columnSql);
}

/**
* {@inheritdoc}
*/
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
$comment = $comment === null ? 'NULL' : "'$comment'";

return "COMMENT ON COLUMN $tableName.$columnName IS $comment";
}

/**
* {@inheritDoc}
*/
Expand Down
Expand Up @@ -535,6 +535,20 @@ public function testGetColumnComment()
$columns = $this->_sm->listTableColumns("column_comment_test");
$this->assertEquals(1, count($columns));
$this->assertEquals('This is a comment', $columns['id']->getComment());

$tableDiff = new \Doctrine\DBAL\Schema\TableDiff('column_comment_test');
$tableDiff->changedColumns['id'] = new \Doctrine\DBAL\Schema\ColumnDiff(
'id', new \Doctrine\DBAL\Schema\Column(
'id', \Doctrine\DBAL\Types\Type::getType('integer'), array('primary' => true)
),
array('comment')
);

$this->_sm->alterTable($tableDiff);

$columns = $this->_sm->listTableColumns("column_comment_test");
$this->assertEquals(1, count($columns));
$this->assertEmpty($columns['id']->getComment());
}

/**
Expand Down
Expand Up @@ -374,6 +374,12 @@ public function testAlterTableColumnComments()
{
$tableDiff = new TableDiff('mytable');
$tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
$tableDiff->changedColumns['foo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
'foo', new \Doctrine\DBAL\Schema\Column(
'foo', \Doctrine\DBAL\Types\Type::getType('string')
),
array('comment')
);
$tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
'bar', new \Doctrine\DBAL\Schema\Column(
'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
Expand Up @@ -203,7 +203,7 @@ public function getCreateTableColumnCommentsSQL()

public function getAlterTableColumnCommentsSQL()
{
return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE foo foo VARCHAR(255) NOT NULL, CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
}

public function getCreateTableColumnTypeCommentsSQL()
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Expand Up @@ -268,8 +268,8 @@ public function getAlterTableColumnCommentsSQL()
{
return array(
"ALTER TABLE mytable ADD (quota NUMBER(10) NOT NULL)",
"ALTER TABLE mytable MODIFY (baz VARCHAR2(255) NOT NULL)",
"COMMENT ON COLUMN mytable.quota IS 'A comment'",
"COMMENT ON COLUMN mytable.foo IS ''",
"COMMENT ON COLUMN mytable.baz IS 'B comment'",
);
}
Expand Down
Expand Up @@ -254,6 +254,7 @@ public function getAlterTableColumnCommentsSQL()
return array(
"ALTER TABLE mytable ADD quota INT NOT NULL",
"COMMENT ON COLUMN mytable.quota IS 'A comment'",
"COMMENT ON COLUMN mytable.foo IS NULL",
"COMMENT ON COLUMN mytable.baz IS 'B comment'",
);
}
Expand Down

0 comments on commit ef57a35

Please sign in to comment.