diff --git a/src/Tokenizers/BaseTokenizer.php b/src/Tokenizers/BaseTokenizer.php index 744e1a0..3722eb4 100644 --- a/src/Tokenizers/BaseTokenizer.php +++ b/src/Tokenizers/BaseTokenizer.php @@ -10,6 +10,7 @@ abstract class BaseTokenizer private const SPACE_REPLACER = '&!@'; private const SINGLE_QUOTE_REPLACER = '!*@'; + private const EMPTY_STRING_REPLACER = '$$EMPTY_STRING'; public function __construct(string $value) { @@ -17,6 +18,10 @@ public function __construct(string $value) $prune = false; $pruneSingleQuotes = false; + if (preg_match("/(DEFAULT|COMMENT) ''/", $value, $matches)) { + $value = str_replace($matches[1] . ' \'\'', $matches[1] . ' ' . self::EMPTY_STRING_REPLACER, $value); + } + //first get rid of any single quoted stuff with '' around it if (preg_match_all('/\'\'(.+?)\'\'/', $value, $matches)) { foreach ($matches[0] as $key => $singleQuoted) { @@ -25,9 +30,6 @@ public function __construct(string $value) $pruneSingleQuotes = true; } } - if (preg_match('/\'\'/', $value)) { - $value = str_replace('\'\'', '$$EMPTY_STRING', $value); - } if (preg_match_all("/'(.+?)'/", $value, $matches)) { foreach ($matches[0] as $quoteWithSpace) { @@ -38,7 +40,7 @@ public function __construct(string $value) $prune = true; } } - $value = str_replace('$$EMPTY_STRING', '\'\'', $value); + $value = str_replace(self::EMPTY_STRING_REPLACER, '\'\'', $value); $this->tokens = array_map(function ($item) { return trim($item, ', '); }, str_getcsv($value, ' ', "'")); diff --git a/tests/Unit/GeneratorManagers/MySQLGeneratorManagerTest.php b/tests/Unit/GeneratorManagers/MySQLGeneratorManagerTest.php index 74615c4..cffa79c 100644 --- a/tests/Unit/GeneratorManagers/MySQLGeneratorManagerTest.php +++ b/tests/Unit/GeneratorManagers/MySQLGeneratorManagerTest.php @@ -71,7 +71,6 @@ public function test_can_remove_database_prefix() $mocked->addTableDefinition($definition); $this->assertEquals('posts', $definition->getTableName()); - config()->set('database.connections.' . $connection . '.prefix', ''); $definition = (new TableDefinition())->setTableName('wp_posts'); diff --git a/tests/Unit/Tokenizers/MySQL/ColumnTokenizerTest.php b/tests/Unit/Tokenizers/MySQL/ColumnTokenizerTest.php index ab97662..be10bc6 100644 --- a/tests/Unit/Tokenizers/MySQL/ColumnTokenizerTest.php +++ b/tests/Unit/Tokenizers/MySQL/ColumnTokenizerTest.php @@ -149,6 +149,14 @@ public function test_it_tokenizes_varchar_with_default_empty_string_and_comment( $this->assertEquals('this is the "comment"', $columnDefinition->getComment()); } + public function test_it_tokenizes_varchar_with_default_empty_string_and_comment_with_apostrophe() + { + $columnTokenizer = ColumnTokenizer::parse("`testing` varchar(255) DEFAULT '' COMMENT 'this is the \"comment\" ''inside single quote''"); + $columnDefinition = $columnTokenizer->definition(); + $this->assertEquals('', $columnDefinition->getDefaultValue()); + $this->assertEquals('this is the "comment" \'inside single quote\'', $columnDefinition->getComment()); + } + public function test_it_tokenizes_varchar_with_boolean_literal_default() { $columnTokenizer = ColumnTokenizer::parse("`testing` bit(2) DEFAULT b'10'");