Skip to content

Commit

Permalink
MSSQL migration alterColumn on a column with default value NULL creat…
Browse files Browse the repository at this point in the history
…es wrong CONSTRAINT (#488)

* MSSQL migration alterColumn on a column with default value NULL creates wrong CONSTRAINT
yiisoft/yii2#18617

* styleci
  • Loading branch information
darkdef committed Jan 21, 2023
1 parent d211ac6 commit d5e2878
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Schema/AbstractColumnSchemaBuilder.php
Expand Up @@ -224,26 +224,37 @@ protected function buildUniqueString(): string
}

/**
* Builds the default value specification for the column.
* Return the default value for the column.
*
* @return string A string containing the DEFAULT keyword and the default value.
* @return string|null string with default value of column.
*/
protected function buildDefaultString(): string
protected function buildDefaultValue()
{
if ($this->default === null) {
return $this->isNotNull === false ? ' DEFAULT NULL' : '';
return $this->isNotNull === false ? 'NULL' : null;
}

$string = ' DEFAULT ';

$string .= match (gettype($this->default)) {
return match (gettype($this->default)) {
'object', 'integer' => (string)$this->default,
'double' => StringHelper::normalizeFloat((string)$this->default),
'boolean' => $this->default ? 'TRUE' : 'FALSE',
default => "'$this->default'",
};
}

/**
* Builds the default value specification for the column.
*
* @return string A string containing the DEFAULT keyword and the default value.
*/
protected function buildDefaultString(): string
{
$defaultValue = $this->buildDefaultValue();
if ($defaultValue === null) {
return '';
}

return $string;
return ' DEFAULT ' . $defaultValue;
}

/**
Expand Down

0 comments on commit d5e2878

Please sign in to comment.