Skip to content

Commit 28e0a93

Browse files
committed
Integer fields default to nullable
1 parent 61f4899 commit 28e0a93

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Processor/CreateProcessor.php

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ private static function getDefinitionColumn(Query\MysqlColumnType $stmt) : Colum
147147
case DataType::BIT:
148148
case DataType::MEDIUMINT:
149149
case DataType::BIGINT:
150+
if ($stmt->null === null) {
151+
$stmt->null = true;
152+
}
153+
150154
return self::getIntegerDefinitionColumn($stmt);
151155

152156
case DataType::FLOAT:

tests/EndToEndTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,30 @@ public function testDropTable(string $table): void
983983
);
984984
}
985985

986+
public function testSelectNullableFields()
987+
{
988+
$pdo = self::getConnectionToFullDB(false);
989+
990+
$query = $pdo->prepare("SELECT nullable_field, nullable_field_default_0 FROM `video_game_characters` WHERE `id` = 1");
991+
$query->execute();
992+
993+
$this->assertSame(
994+
['nullable_field' => null, 'nullable_field_default_0' => 0],
995+
$query->fetch(\PDO::FETCH_ASSOC)
996+
);
997+
998+
$query = $pdo->prepare("UPDATE `video_game_characters` SET `nullable_field_default_0` = NULL, `nullable_field` = NULL WHERE `id` = 1");
999+
$query->execute();
1000+
1001+
$query = $pdo->prepare("SELECT nullable_field, nullable_field_default_0 FROM `video_game_characters` WHERE `id` = 1");
1002+
$query->execute();
1003+
1004+
$this->assertSame(
1005+
['nullable_field' => null, 'nullable_field_default_0' => null],
1006+
$query->fetch(\PDO::FETCH_ASSOC)
1007+
);
1008+
}
1009+
9861010
private static function getPdo(string $connection_string, bool $strict_mode = false) : \PDO
9871011
{
9881012
$options = $strict_mode ? [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="STRICT_ALL_TABLES"'] : [];

tests/fixtures/create_table.sql

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CREATE TABLE `video_game_characters` (
1010
`powerups` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
1111
`skills` varchar(1000) NOT NULL DEFAULT '',
1212
`nullable_field` tinyint(3) DEFAULT NULL,
13+
`nullable_field_default_0` tinyint(3) DEFAULT '0',
1314
`some_float` float DEFAULT '0.00',
1415
`total_games` int(11) UNSIGNED NOT NULL DEFAULT '0',
1516
`lives` int(11) UNSIGNED NOT NULL DEFAULT '0',

0 commit comments

Comments
 (0)