Skip to content

Commit ff5d43c

Browse files
authored
Fix alter column with default null (#282)
* Fix alter column with default null * Add line to CHANGELOG.md
1 parent 9f3d91c commit ff5d43c

4 files changed

Lines changed: 279 additions & 235 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Bug #275: Refactor `DMLQueryBuilder`, related with yiisoft/db#746 (@Tigrov)
66
- Bug #278: Remove `RECURSIVE` expression from CTE queries (@Tigrov)
77
- Bug #280: Fix type boolean (@terabytesoftw)
8+
- Bug #282: Fix `DDLQueryBuilder::alterColumn()` for columns with default null (@Tigrov)
89
- Enh #283: Move methods from `Command` to `AbstractPdoCommand` class (@Tigrov)
910

1011
## 1.0.1 July 24, 2023

src/DDLQueryBuilder.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public function addDefaultValue(string $table, string $name, string $column, mix
5353
*/
5454
public function alterColumn(string $table, string $column, ColumnInterface|string $type): string
5555
{
56-
$sqlAfter = [$this->dropConstraintsForColumn($table, $column, 'D')];
57-
56+
$sqlAfter = [];
5857
$columnName = $this->quoter->quoteColumnName($column);
5958
$tableName = $this->quoter->quoteTableName($table);
6059
$constraintBase = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column);
@@ -85,11 +84,11 @@ public function alterColumn(string $table, string $column, ColumnInterface|strin
8584
}
8685
}
8786

88-
return 'ALTER TABLE ' . $tableName
89-
. ' ALTER COLUMN '
90-
. $columnName . ' '
91-
. $this->queryBuilder->getColumnType($type) . "\n"
92-
. implode("\n", $sqlAfter);
87+
return implode("\n", [
88+
$this->dropConstraintsForColumn($table, $column, 'D'),
89+
"ALTER TABLE $tableName ALTER COLUMN $columnName {$this->queryBuilder->getColumnType($type)}",
90+
...$sqlAfter,
91+
]);
9392
}
9493

9594
/**

tests/CommandTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Yiisoft\Db\Exception\InvalidConfigException;
1313
use Yiisoft\Db\Exception\NotSupportedException;
1414
use Yiisoft\Db\Expression\Expression;
15+
use Yiisoft\Db\Mssql\Column;
1516
use Yiisoft\Db\Mssql\Connection;
1617
use Yiisoft\Db\Mssql\Dsn;
1718
use Yiisoft\Db\Mssql\Driver;
@@ -353,4 +354,27 @@ public function testShowDatabases(): void
353354
$this->assertSame('sqlsrv:Server=localhost,1433;', $db->getDriver()->getDsn());
354355
$this->assertSame(['yiitest'], $command->showDatabases());
355356
}
357+
358+
/** @link https://github.com/yiisoft/db-migration/issues/11 */
359+
public function testAlterColumnWithDefaultNull()
360+
{
361+
$db = $this->getConnection();
362+
$command = $db->createCommand();
363+
364+
if ($db->getTableSchema('column_with_constraint', true) !== null) {
365+
$command->dropTable('column_with_constraint')->execute();
366+
}
367+
368+
$command->createTable('column_with_constraint', ['id' => 'pk'])->execute();
369+
$command->addColumn('column_with_constraint', 'field', (new Column('integer'))->null()->asString())->execute();
370+
$command->alterColumn('column_with_constraint', 'field', (new Column('string', 40))->notNull()->asString())->execute();
371+
372+
$fieldCol = $db->getTableSchema('column_with_constraint', true)->getColumn('field');
373+
374+
$this->assertFalse($fieldCol->isAllowNull());
375+
$this->assertNull($fieldCol->getDefaultValue());
376+
$this->assertSame('nvarchar(40)', $fieldCol->getDbType());
377+
378+
$command->dropTable('column_with_constraint');
379+
}
356380
}

0 commit comments

Comments
 (0)