Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alter column with default null #282

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading