Skip to content

Commit

Permalink
Fix alter column with default null (#282)
Browse files Browse the repository at this point in the history
* Fix alter column with default null

* Add line to CHANGELOG.md
  • Loading branch information
Tigrov authored Nov 10, 2023
1 parent 9f3d91c commit ff5d43c
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 235 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Bug #275: Refactor `DMLQueryBuilder`, related with yiisoft/db#746 (@Tigrov)
- Bug #278: Remove `RECURSIVE` expression from CTE queries (@Tigrov)
- Bug #280: Fix type boolean (@terabytesoftw)
- Bug #282: Fix `DDLQueryBuilder::alterColumn()` for columns with default null (@Tigrov)
- Enh #283: Move methods from `Command` to `AbstractPdoCommand` class (@Tigrov)

## 1.0.1 July 24, 2023
Expand Down
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 ff5d43c

Please sign in to comment.