diff --git a/CHANGELOG.md b/CHANGELOG.md index d182acd0b..aab41b47c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ - Enh #1201: Make `$psrCache` parameter of `SchemaCache` constructor and `$schemaCache` parameter of `AbstractSchema` and `AbstractPdoConnection` constructors optional, remove `psr/simple-cache-implementation` from `require` in `composer.json` (@vjik) +- Bug #1202: Fix `ColumnDefinitionParser` to parse an unquoted `DEFAULT NULL` as SQL `NULL` instead of the string + `'NULL'` (@Lugat) ## 2.0.1 February 09, 2026 diff --git a/src/Syntax/ColumnDefinitionParser.php b/src/Syntax/ColumnDefinitionParser.php index 0bd557f74..528f89af5 100644 --- a/src/Syntax/ColumnDefinitionParser.php +++ b/src/Syntax/ColumnDefinitionParser.php @@ -9,6 +9,7 @@ use function preg_match_all; use function preg_replace; use function str_replace; +use function strcasecmp; use function strlen; use function strtolower; use function substr; @@ -22,7 +23,7 @@ * check?: string, * collation?: string, * comment?: string, - * defaultValueRaw?: string, + * defaultValueRaw?: string|null, * extra?: string, * notNull?: bool, * unique?: bool, @@ -42,7 +43,7 @@ class ColumnDefinitionParser * check?: string, * collation?: string, * comment?: string, - * defaultValueRaw?: string, + * defaultValueRaw?: string|null, * dimension?: positive-int, * extra?: string, * notNull?: bool, @@ -115,6 +116,11 @@ protected function extraInfo(string $extra): array $extra = $this->parseBoolValue($extra, '/\s*\bUNIQUE\b/i', 'unique', $info); $extra = $this->parseBoolValue($extra, '/\s*\bNOT\s+NULL\b/i', 'notNull', $info); + /** @psalm-var ExtraInfo $info */ + if (isset($info['defaultValueRaw']) && strcasecmp($info['defaultValueRaw'], 'NULL') === 0) { + $info['defaultValueRaw'] = null; + } + if (empty($info['notNull'])) { $extra = $this->parseBoolValue($extra, '/\s*\bNULL\b/i', 'notNull', $info); diff --git a/tests/Provider/ColumnDefinitionParserProvider.php b/tests/Provider/ColumnDefinitionParserProvider.php index 07d0eb781..275b06184 100644 --- a/tests/Provider/ColumnDefinitionParserProvider.php +++ b/tests/Provider/ColumnDefinitionParserProvider.php @@ -21,7 +21,11 @@ public static function parse(): array ['text NULL', ['type' => 'text', 'notNull' => false]], ['text COLLATE utf8mb4', ['type' => 'text', 'collation' => 'utf8mb4']], ["text COMPRESSION 'LZ4'", ['type' => 'text', 'extra' => "COMPRESSION 'LZ4'"]], - ['text DEFAULT NULL', ['type' => 'text', 'defaultValueRaw' => 'NULL']], + ['text DEFAULT NULL', ['type' => 'text', 'defaultValueRaw' => null]], + ['int default null', ['type' => 'int', 'defaultValueRaw' => null]], + ['int NULL DEFAULT NULL', ['type' => 'int', 'defaultValueRaw' => null, 'notNull' => false]], + ['int DEFAULT 5', ['type' => 'int', 'defaultValueRaw' => '5']], + ["varchar(10) DEFAULT 'NULL'", ['type' => 'varchar', 'size' => 10, 'defaultValueRaw' => "'NULL'"]], ["text DEFAULT 'value'", ['type' => 'text', 'defaultValueRaw' => "'value'"]], ['varchar(36) DEFAULT uuid()', ['type' => 'varchar', 'size' => 36, 'defaultValueRaw' => 'uuid()']], ['varchar(36) DEFAULT uuid()::varchar(36)', ['type' => 'varchar', 'size' => 36, 'defaultValueRaw' => 'uuid()::varchar(36)']],