From e29da7b25acd0a604db61136a0949968e848b627 Mon Sep 17 00:00:00 2001 From: odan Date: Sun, 19 Apr 2020 17:13:22 +0200 Subject: [PATCH] Add support for SET type #81 --- .../Adapter/Generator/PhinxMySqlGenerator.php | 12 +++---- tests/PhinxGeneratorTest.php | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Migration/Adapter/Generator/PhinxMySqlGenerator.php b/src/Migration/Adapter/Generator/PhinxMySqlGenerator.php index 0ae1eab46..be7e6d1d7 100644 --- a/src/Migration/Adapter/Generator/PhinxMySqlGenerator.php +++ b/src/Migration/Adapter/Generator/PhinxMySqlGenerator.php @@ -618,9 +618,9 @@ private function getPhinxColumnOptions(string $phinxType, array $columnData, arr // Numeric attributes $attributes = $this->getPhinxColumnOptionsNumeric($attributes, $columnData); - // Enum values - if ($phinxType === 'enum') { - $attributes = $this->getOptionEnumValue($attributes, $columnData); + // Enum and set values + if ($phinxType === 'enum' || $phinxType === 'set') { + $attributes = $this->getOptionEnumAndSetValues($attributes, $columnData); } // Collation @@ -841,12 +841,12 @@ private function getPhinxColumnOptionsNumeric(array $attributes, array $columnDa * * @return array Attributes */ - private function getOptionEnumValue(array $attributes, array $columnData): array + private function getOptionEnumAndSetValues(array $attributes, array $columnData): array { $match = null; - $pattern = '/enum\((.*)\)/'; + $pattern = '/(enum|set)\((.*)\)/'; if (preg_match($pattern, $columnData['COLUMN_TYPE'], $match) === 1) { - $values = str_getcsv($match[1], ',', "'", '\\'); + $values = str_getcsv($match[2], ',', "'", '\\'); $attributes['values'] = $values; } diff --git a/tests/PhinxGeneratorTest.php b/tests/PhinxGeneratorTest.php index d58df32a7..16ec04df3 100644 --- a/tests/PhinxGeneratorTest.php +++ b/tests/PhinxGeneratorTest.php @@ -296,6 +296,38 @@ public function testEnum(): void $this->assertSame($oldSchema, $newSchema); } + /** + * Test #81. + * + * @return void + */ + public function testSetValues(): void + { + $this->execSql("CREATE TABLE `test`( + `id` INT NOT NULL AUTO_INCREMENT, + `myset` SET('1','abc'), + PRIMARY KEY (`id`)) + ENGINE=INNODB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;"); + + $this->generate(); + + $this->execSql("ALTER TABLE `test` CHANGE `myset` `myset` SET('1','abc','new')"); + $oldSchema = $this->getTableSchema('test'); + $this->generateAgain(); + + // Reset + $this->dropTables(); + + // Run all generated migrations + $this->migrate(); + + $newSchema = $this->getTableSchema('test'); + $this->assertSame($oldSchema, $newSchema); + } + + + // + /** * Test. #46. *