Skip to content

Commit

Permalink
Fill up column values from schema (#1155)
Browse files Browse the repository at this point in the history
* Write test that enum values must be read back from schema

* Parse back MySQL enum's values

* Document `getSqlType` call from `getPhinxType`
  • Loading branch information
garex authored and lorenzo committed Aug 30, 2017
1 parent 0d818ce commit 72e1533
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ public function getColumns($tableName)
$column->setIdentity(true);
}

if (isset($phinxType['values'])) {
$column->setValues($phinxType['values']);
}

$columns[] = $column;
}

Expand Down Expand Up @@ -841,6 +845,7 @@ public function getSqlType($type, $limit = null)
*/
public function getPhinxType($sqlTypeDef)
{
$matches = array();
if (!preg_match('/^([\w]+)(\(([\d]+)*(,([\d]+))*\))*(.+)*$/', $sqlTypeDef, $matches)) {
throw new \RuntimeException('Column type ' . $sqlTypeDef . ' is not supported');
} else {
Expand Down Expand Up @@ -926,13 +931,20 @@ public function getPhinxType($sqlTypeDef)
break;
}

// Call this to check if parsed type is supported.
$this->getSqlType($type, $limit);

return array(
$phinxType = array(
'name' => $type,
'limit' => $limit,
'precision' => $precision
);

if (static::PHINX_TYPE_ENUM == $type) {
$phinxType['values'] = explode("','", trim($matches[6], "()'"));
}

return $phinxType;
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\StreamOutput;
use Phinx\Db\Adapter\AdapterInterface;

class MysqlAdapterTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -1062,6 +1063,21 @@ public function testAddEnumColumn()
$this->assertEquals("enum('one','two')", $rows[1]['Type']);
}

public function testEnumColumnValuesFilledUpFromSchema()
{
// Creating column with values
(new \Phinx\Db\Table('table1', array(), $this->adapter))
->addColumn('enum_column', 'enum', array('values' => array('one', 'two')))
->save();

// Reading them back
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
$columns = $table->getColumns();
$enumColumn = end($columns);
$this->assertEquals(AdapterInterface::PHINX_TYPE_ENUM, $enumColumn->getType());
$this->assertEquals(array('one', 'two'), $enumColumn->getValues());
}

public function testHasColumn()
{
$table = new \Phinx\Db\Table('table1', array(), $this->adapter);
Expand Down

0 comments on commit 72e1533

Please sign in to comment.