Skip to content

Commit

Permalink
Fix alter column with default null
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Oct 26, 2023
1 parent 1be5ddb commit b4355c8
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 235 deletions.
13 changes: 6 additions & 7 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public function addDefaultValue(string $table, string $name, string $column, mix
*/
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
{
$sqlAfter = [$this->dropConstraintsForColumn($table, $column, 'D')];

$sqlAfter = [];
$columnName = $this->quoter->quoteColumnName($column);
$tableName = $this->quoter->quoteTableName($table);
$constraintBase = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column);
Expand Down Expand Up @@ -85,11 +84,11 @@ public function alterColumn(string $table, string $column, ColumnInterface|strin
}
}

return 'ALTER TABLE ' . $tableName
. ' ALTER COLUMN '
. $columnName . ' '
. $this->queryBuilder->getColumnType($type) . "\n"
. implode("\n", $sqlAfter);
return implode("\n", [
$this->dropConstraintsForColumn($table, $column, 'D'),
"ALTER TABLE $tableName ALTER COLUMN $columnName {$this->queryBuilder->getColumnType($type)}",
...$sqlAfter,
]);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\Column;
use Yiisoft\Db\Mssql\Connection;
use Yiisoft\Db\Mssql\Dsn;
use Yiisoft\Db\Mssql\Driver;
Expand Down Expand Up @@ -353,4 +354,27 @@ public function testShowDatabases(): void
$this->assertSame('sqlsrv:Server=localhost,1433;', $db->getDriver()->getDsn());
$this->assertSame(['yiitest'], $command->showDatabases());
}

/** @link https://github.com/yiisoft/db-migration/issues/11 */
public function testAlterColumnWithDefaultNull()
{
$db = $this->getConnection();
$command = $db->createCommand();

if ($db->getTableSchema('column_with_constraint', true) !== null) {
$command->dropTable('column_with_constraint')->execute();
}

$command->createTable('column_with_constraint', ['id' => 'pk'])->execute();
$command->addColumn('column_with_constraint', 'field', (new Column('integer'))->null()->asString())->execute();
$command->alterColumn('column_with_constraint', 'field', (new Column('string', 40))->notNull()->asString())->execute();

$fieldCol = $db->getTableSchema('column_with_constraint', true)->getColumn('field');

$this->assertFalse($fieldCol->isAllowNull());
$this->assertNull($fieldCol->getDefaultValue());
$this->assertSame('nvarchar(40)', $fieldCol->getDbType());

$command->dropTable('column_with_constraint');
}
}
Loading

0 comments on commit b4355c8

Please sign in to comment.