Skip to content

Commit

Permalink
Add support to column comments in MySQL Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ferodss committed Oct 7, 2013
1 parent 8443d32 commit 5edaf5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,6 @@ protected function getColumnSqlDefinition(Column $column)
{
$sqlType = $this->getSqlType($column->getType());
$def = '';
$def = '';
$def .= strtoupper($sqlType['name']);
if ($column->getPrecision() && $column->getScale()) {
$def .= '(' . $column->getPrecision() . ',' . $column->getScale() . ')';
Expand All @@ -800,6 +799,10 @@ protected function getColumnSqlDefinition(Column $column)
$def .= is_null($column->getDefault()) ? '' : ' DEFAULT \'' . $column->getDefault() . '\'';
}

if ($column->getComment()) {
$def .= ' COMMENT ' . $this->getConnection()->quote($column->getComment());
}

if ($column->getUpdate()) {
$def .= ' ON UPDATE ' . $column->getUpdate();
}
Expand Down
31 changes: 29 additions & 2 deletions src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class Column
*/
protected $update;

/**
* @var string
*/
protected $comment;

/**
* Sets the column name.
*
Expand Down Expand Up @@ -327,6 +332,28 @@ public function getScale()
return $this->scale;
}

/**
* Sets the column comment.
*
* @param string $comment
* @return Column
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}

/**
* Gets the column comment.
*
* @return string
*/
public function getComment()
{
return $this->comment;
}

/**
* Utility method that maps an array of column options to this objects methods.
*
Expand All @@ -336,7 +363,7 @@ public function getScale()
public function setOptions($options)
{
// Valid Options
$validOptions = array('limit', 'length', 'default', 'null', 'precision', 'scale', 'after', 'update');
$validOptions = array('limit', 'length', 'default', 'null', 'precision', 'scale', 'after', 'update', 'comment');
foreach ($options as $option => $value) {
if (!in_array($option, $validOptions)) {
throw new \RuntimeException('\'' . $option . '\' is not a valid column option.');
Expand All @@ -352,4 +379,4 @@ public function setOptions($options)
$this->$method($value);
}
}
}
}
14 changes: 13 additions & 1 deletion tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,16 @@ public function testDropDatabase()
$this->assertTrue($this->adapter->hasDatabase('temp_phinx_database'));
$this->adapter->dropDatabase('temp_phinx_database');
}
}

public function testAddColumnWithComment()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->addColumn('column1', 'string', array('comment' => $comment = 'Comments from "column1"'))
->save();

$rows = $this->adapter->fetchAll('SELECT column_name, column_comment FROM information_schema.columns WHERE table_name = "table1"');
$columnWithComment = $rows[1];

$this->assertEquals($comment, $columnWithComment['column_comment'], 'Dont set column comment correctly');
}
}

0 comments on commit 5edaf5e

Please sign in to comment.