Skip to content

Commit eff9af0

Browse files
Fix issue with empty default value and apostrophes in comments (#83)
1 parent b334164 commit eff9af0

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/Tokenizers/BaseTokenizer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ abstract class BaseTokenizer
1010

1111
private const SPACE_REPLACER = '&!@';
1212
private const SINGLE_QUOTE_REPLACER = '!*@';
13+
private const EMPTY_STRING_REPLACER = '$$EMPTY_STRING';
1314

1415
public function __construct(string $value)
1516
{
1617
$this->value = $value;
1718
$prune = false;
1819
$pruneSingleQuotes = false;
1920

21+
if (preg_match("/(DEFAULT|COMMENT) ''/", $value, $matches)) {
22+
$value = str_replace($matches[1] . ' \'\'', $matches[1] . ' ' . self::EMPTY_STRING_REPLACER, $value);
23+
}
24+
2025
//first get rid of any single quoted stuff with '' around it
2126
if (preg_match_all('/\'\'(.+?)\'\'/', $value, $matches)) {
2227
foreach ($matches[0] as $key => $singleQuoted) {
@@ -25,9 +30,6 @@ public function __construct(string $value)
2530
$pruneSingleQuotes = true;
2631
}
2732
}
28-
if (preg_match('/\'\'/', $value)) {
29-
$value = str_replace('\'\'', '$$EMPTY_STRING', $value);
30-
}
3133

3234
if (preg_match_all("/'(.+?)'/", $value, $matches)) {
3335
foreach ($matches[0] as $quoteWithSpace) {
@@ -38,7 +40,7 @@ public function __construct(string $value)
3840
$prune = true;
3941
}
4042
}
41-
$value = str_replace('$$EMPTY_STRING', '\'\'', $value);
43+
$value = str_replace(self::EMPTY_STRING_REPLACER, '\'\'', $value);
4244
$this->tokens = array_map(function ($item) {
4345
return trim($item, ', ');
4446
}, str_getcsv($value, ' ', "'"));

tests/Unit/GeneratorManagers/MySQLGeneratorManagerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public function test_can_remove_database_prefix()
7171
$mocked->addTableDefinition($definition);
7272
$this->assertEquals('posts', $definition->getTableName());
7373

74-
7574
config()->set('database.connections.' . $connection . '.prefix', '');
7675

7776
$definition = (new TableDefinition())->setTableName('wp_posts');

tests/Unit/Tokenizers/MySQL/ColumnTokenizerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ public function test_it_tokenizes_varchar_with_default_empty_string_and_comment(
149149
$this->assertEquals('this is the "comment"', $columnDefinition->getComment());
150150
}
151151

152+
public function test_it_tokenizes_varchar_with_default_empty_string_and_comment_with_apostrophe()
153+
{
154+
$columnTokenizer = ColumnTokenizer::parse("`testing` varchar(255) DEFAULT '' COMMENT 'this is the \"comment\" ''inside single quote''");
155+
$columnDefinition = $columnTokenizer->definition();
156+
$this->assertEquals('', $columnDefinition->getDefaultValue());
157+
$this->assertEquals('this is the "comment" \'inside single quote\'', $columnDefinition->getComment());
158+
}
159+
152160
public function test_it_tokenizes_varchar_with_boolean_literal_default()
153161
{
154162
$columnTokenizer = ColumnTokenizer::parse("`testing` bit(2) DEFAULT b'10'");

0 commit comments

Comments
 (0)