From 9a6ce1e7fdf0fa4e602ba5875b5bc9442ccaa115 Mon Sep 17 00:00:00 2001 From: reeperbahnause Date: Sat, 9 Jul 2022 20:53:51 +0200 Subject: [PATCH] Fix SqlServerAdapter returning empty string instead of null for column default (#2090) --- src/Phinx/Db/Adapter/SqlServerAdapter.php | 9 ++++++++- tests/Phinx/Db/Adapter/SqlServerAdapterTest.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index c2670a2c8..a758f463d 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -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') { diff --git a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php index 4497207a1..c4e4ce460 100644 --- a/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SqlServerAdapterTest.php @@ -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);