Skip to content

fix text and char column php code args #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Schema/Column/CharacterColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ public function getPhpCode() : string
}
}

return '(new \\' . static::class . '('
. $this->max_string_length
. ($this->character_set !== null ? ', \'' . $this->character_set . '\'' : '')
. ($this->collation !== null ? ', \'' . $this->collation . '\'' : '')
. '))'
$args = [
$this->max_string_length,
$this->character_set === null ? 'null' : "'{$this->character_set}'",
$this->collation === null ? 'null' : "'{$this->collation}'",
];

return '(new \\' . static::class . '(' . implode(', ', $args) . '))'
. $default
. $this->getNullablePhp();
}
Expand Down
13 changes: 7 additions & 6 deletions src/Schema/Column/TextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ trait TextTrait
public function getPhpCode() : string
{
$default = $this->getDefault() !== null ? '\'' . $this->getDefault() . '\'' : 'null';

return '(new \\' . static::class . '('
. ($this->character_set !== null && $this->collation !== null
? ', \'' . $this->character_set . '\'' . ', \'' . $this->collation . '\''
: '')
. '))'

$args = [
$this->character_set === null ? 'null' : "'{$this->character_set}'",
$this->collation === null ? 'null' : "'{$this->collation}'",
];

return '(new \\' . static::class . '(' . implode(', ', $args) . '))'
. ($this->hasDefault() ? '->setDefault(' . $default . ')' : '')
. $this->getNullablePhp();
}
Expand Down
10 changes: 10 additions & 0 deletions tests/CreateTableParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ public function testSimpleParse()

// specific parsing checks
$this->assertInstanceOf(TableDefinition::class, $table_defs['tweets']);
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['title']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['title']->getCollation());
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['text']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['text']->getCollation());

$this->assertInstanceOf(TableDefinition::class, $table_defs['texts']);
$this->assertEquals('utf8mb4', $table_defs['texts']->columns['title_char_col']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['texts']->columns['title_char_col']->getCollation());
$this->assertNull($table_defs['texts']->columns['title_col']->getCharacterSet());
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['texts']->columns['title_col']->getCollation());
$this->assertNull($table_defs['texts']->columns['title']->getCharacterSet());
$this->assertNull($table_defs['texts']->columns['title']->getCollation());
}
}
18 changes: 16 additions & 2 deletions tests/fixtures/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,25 @@ CREATE TABLE `orders`
`created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `tweets` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`text` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`title` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE `texts` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title_char_col` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`title_col` varchar(256) COLLATE utf8mb4_unicode_ci,
`title` varchar(256)
`text_char_col` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`text_col` text COLLATE utf8mb4_unicode_ci,
`text` text,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;