Skip to content

Commit

Permalink
Integer fields default to nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 7, 2021
1 parent 61f4899 commit 28e0a93
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Processor/CreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ private static function getDefinitionColumn(Query\MysqlColumnType $stmt) : Colum
case DataType::BIT:
case DataType::MEDIUMINT:
case DataType::BIGINT:
if ($stmt->null === null) {
$stmt->null = true;
}

return self::getIntegerDefinitionColumn($stmt);

case DataType::FLOAT:
Expand Down
24 changes: 24 additions & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,30 @@ public function testDropTable(string $table): void
);
}

public function testSelectNullableFields()
{
$pdo = self::getConnectionToFullDB(false);

$query = $pdo->prepare("SELECT nullable_field, nullable_field_default_0 FROM `video_game_characters` WHERE `id` = 1");
$query->execute();

$this->assertSame(
['nullable_field' => null, 'nullable_field_default_0' => 0],
$query->fetch(\PDO::FETCH_ASSOC)
);

$query = $pdo->prepare("UPDATE `video_game_characters` SET `nullable_field_default_0` = NULL, `nullable_field` = NULL WHERE `id` = 1");
$query->execute();

$query = $pdo->prepare("SELECT nullable_field, nullable_field_default_0 FROM `video_game_characters` WHERE `id` = 1");
$query->execute();

$this->assertSame(
['nullable_field' => null, 'nullable_field_default_0' => null],
$query->fetch(\PDO::FETCH_ASSOC)
);
}

private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
{
$options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CREATE TABLE `video_game_characters` (
`powerups` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
`skills` varchar(1000) NOT NULL DEFAULT '',
`nullable_field` tinyint(3) DEFAULT NULL,
`nullable_field_default_0` tinyint(3) DEFAULT '0',
`some_float` float DEFAULT '0.00',
`total_games` int(11) UNSIGNED NOT NULL DEFAULT '0',
`lives` int(11) UNSIGNED NOT NULL DEFAULT '0',
Expand Down

0 comments on commit 28e0a93

Please sign in to comment.