Skip to content

Commit

Permalink
Fix SqlServerAdapter returning empty string instead of null for colum…
Browse files Browse the repository at this point in the history
…n default (#2090)
  • Loading branch information
reeperbahnause authored Jul 9, 2022
1 parent bc2d695 commit 9a6ce1e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,18 @@ public function getColumns($tableName)
}

/**
* @param string $default Default
* @param string|null $default Default
* @return int|string|null
*/
protected function parseDefault($default)
{
// if a column is non-nullable and has no default, the value of column_default is null,
// otherwise it should be a string value that we parse below, including "(NULL)" which
// also stands for a null default
if ($default === null) {
return null;
}

$result = preg_replace(["/\('(.*)'\)/", "/\(\((.*)\)\)/", "/\((.*)\)/"], '$1', $default);

if (strtoupper($result) === 'NULL') {
Expand Down
15 changes: 15 additions & 0 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,21 @@ public function testAddColumnWithDefaultNull()
}
}

public function testAddColumnWithNotNullableNoDefault()
{
$table = new \Phinx\Db\Table('table1', [], $this->adapter);
$table
->addColumn('col', 'string', ['null' => false])
->create();

$columns = $this->adapter->getColumns('table1');
$this->assertCount(2, $columns);
$this->assertArrayHasKey('id', $columns);
$this->assertArrayHasKey('col', $columns);
$this->assertFalse($columns['col']->isNull());
$this->assertNull($columns['col']->getDefault());
}

public function testAddColumnWithDefaultBool()
{
$table = new \Phinx\Db\Table('table1', [], $this->adapter);
Expand Down

0 comments on commit 9a6ce1e

Please sign in to comment.