Skip to content

Commit

Permalink
Merge pull request cakephp#32 from zuker/0.1.x-dev
Browse files Browse the repository at this point in the history
cakephp#29 fix for ignoring default value 0
  • Loading branch information
robmorgan committed Dec 8, 2012
2 parents c804ae6 + 9237a1f commit 05bcbf0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,12 @@ protected function getColumnSqlDefinition(Column $column)
? '(' . ($column->getLimit() ? $column->getLimit() : $sqlType['limit']) . ')' : '';
$def .= ($column->isNull() == false) ? ' NOT NULL' : ' NULL';
$def .= ($column->isIdentity()) ? ' AUTO_INCREMENT' : '';
$def .= ($column->getDefault()) ? ' DEFAULT \'' . $column->getDefault() . '\'' : '';
$default = $column->getDefault();
if (is_numeric($default)) {
$def .= ' DEFAULT ' . $column->getDefault();
} else {
$def .= is_null($column->getDefault()) ? '' : ' DEFAULT \'' . $column->getDefault() . '\'';
}
// TODO - add precision & scale for decimals
return $def;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public function isNull()
*/
public function setDefault($default)
{
if ($default === false || $default === '') {
$default = null;
}
$this->default = $default;
return $this;
}
Expand Down
73 changes: 73 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ public function testAddColumn()
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM table1');
$this->assertEquals('realname', $rows[1]['Field']);
}

public function testAddColumnWithDefaultValue()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->save();
$table->addColumn('default_zero', 'string', array('default' => 'test'))
->save();
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM table1');
$this->assertEquals("test", $rows[1]['Default']);
}

public function testAddColumnWithDefaultZero()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->save();
$table->addColumn('default_zero', 'integer', array('default' => 0))
->save();
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM table1');
$this->assertNotNull($rows[1]['Default']);
$this->assertEquals("0", $rows[1]['Default']);
}

public function testAddColumnWithDefaultEmptyString()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$table->save();
$table->addColumn('default_zero', 'integer', array('default' => null))
->save();
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM table1');
$this->assertNull($rows[1]['Default']);
}

public function testRenameColumn()
{
Expand Down Expand Up @@ -247,6 +278,48 @@ public function testChangeColumn()
$this->assertFalse($this->adapter->hasColumn('t', 'column1'));
$this->assertTrue($this->adapter->hasColumn('t', 'column2'));
}

public function testChangeColumnDefaultValue()
{
$table = new \Phinx\Db\Table('t', array(), $this->adapter);
$table->addColumn('column1', 'string', array('default' => 'test'))
->save();
$newColumn1 = new \Phinx\Db\Table\Column();
$newColumn1->setDefault('test1')
->setType('string');
$table->changeColumn('column1', $newColumn1);
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM t');
$this->assertNotNull($rows[1]['Default']);
$this->assertEquals("test1", $rows[1]['Default']);
}


public function testChangeColumnDefaultToZero()
{
$table = new \Phinx\Db\Table('t', array(), $this->adapter);
$table->addColumn('column1', 'integer')
->save();
$newColumn1 = new \Phinx\Db\Table\Column();
$newColumn1->setDefault(0)
->setType('integer');
$table->changeColumn('column1', $newColumn1);
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM t');
$this->assertNotNull($rows[1]['Default']);
$this->assertEquals("0", $rows[1]['Default']);
}

public function testChangeColumnDefaultToNull()
{
$table = new \Phinx\Db\Table('t', array(), $this->adapter);
$table->addColumn('column1', 'string', array('default' => 'test'))
->save();
$newColumn1 = new \Phinx\Db\Table\Column();
$newColumn1->setDefault(null)
->setType('string');
$table->changeColumn('column1', $newColumn1);
$rows = $this->adapter->fetchAll('SHOW COLUMNS FROM t');
$this->assertNull($rows[1]['Default']);
}

public function testDropColumn()
{
Expand Down

0 comments on commit 05bcbf0

Please sign in to comment.