From 5c0885effc656ea7ff13b08ce399a0671ed09fdf Mon Sep 17 00:00:00 2001 From: Ustimenko Alexander Date: Sat, 26 Aug 2017 00:36:51 +0700 Subject: [PATCH 1/2] Write test that enum values must be read back from schema --- tests/Phinx/Db/Adapter/MysqlAdapterTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index 809c2d264..86a68f5cc 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -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 { @@ -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); From 8b46624fa34b5ff49f02310a7fac4f56b5975619 Mon Sep 17 00:00:00 2001 From: Ustimenko Alexander Date: Sat, 26 Aug 2017 00:44:58 +0700 Subject: [PATCH 2/2] Parse back MySQL enum's values * Document `getSqlType` call from `getPhinxType` --- src/Phinx/Db/Adapter/MysqlAdapter.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Db/Adapter/MysqlAdapter.php b/src/Phinx/Db/Adapter/MysqlAdapter.php index 2bbb5598b..ac7568f63 100644 --- a/src/Phinx/Db/Adapter/MysqlAdapter.php +++ b/src/Phinx/Db/Adapter/MysqlAdapter.php @@ -350,6 +350,10 @@ public function getColumns($tableName) $column->setIdentity(true); } + if (isset($phinxType['values'])) { + $column->setValues($phinxType['values']); + } + $columns[] = $column; } @@ -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 { @@ -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; } }