diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index 760568fff..42b10d9d3 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -458,7 +458,7 @@ public function getColumns($tableName) $rows = $this->fetchAll($sql); foreach ($rows as $columnInfo) { try { - $type = $this->getPhinxType($columnInfo['type']); + $type = $this->getPhinxType($columnInfo['type'], $columnInfo['char_length']); } catch (UnsupportedColumnTypeException $e) { $type = Literal::from($columnInfo['type']); } @@ -471,7 +471,8 @@ public function getColumns($tableName) ->setIdentity($columnInfo['identity'] === '1') ->setComment($this->getColumnComment($columnInfo['table_name'], $columnInfo['name'])); - if (!empty($columnInfo['char_length'])) { + if ($type !== self::PHINX_TYPE_TEXT && !empty($columnInfo['char_length'])) { + // Do not set a limit for text, as that is handled elsewhere. $column->setLimit($columnInfo['char_length']); } @@ -609,9 +610,6 @@ protected function getChangeDefault($tableName, Column $newColumn) protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn) { $columns = $this->getColumns($tableName); - $changeDefault = - $newColumn->getDefault() !== $columns[$columnName]->getDefault() || - $newColumn->getType() !== $columns[$columnName]->getType(); $instructions = new AlterInstructions(); @@ -621,9 +619,7 @@ protected function getChangeColumnInstructions($tableName, $columnName, Column $ ); } - if ($changeDefault) { - $instructions->merge($this->getDropDefaultConstraint($tableName, $newColumn->getName())); - } + $instructions->merge($this->getDropDefaultConstraint($tableName, $newColumn->getName())); $instructions->addPostStep(sprintf( 'ALTER TABLE %s ALTER COLUMN %s %s', @@ -636,9 +632,7 @@ protected function getChangeColumnInstructions($tableName, $columnName, Column $ $instructions->addPostStep($this->getColumnCommentSqlDefinition($newColumn, $tableName)); } - if ($changeDefault) { - $instructions->merge($this->getChangeDefault($tableName, $newColumn)); - } + $instructions->merge($this->getChangeDefault($tableName, $newColumn)); return $instructions; } @@ -1069,7 +1063,7 @@ public function getSqlType($type, $limit = null) case static::PHINX_TYPE_CHAR: return ['name' => 'nchar', 'limit' => 255]; case static::PHINX_TYPE_TEXT: - return ['name' => 'ntext']; + return ['name' => 'nvarchar', 'limit' => 'max']; case static::PHINX_TYPE_INTEGER: return ['name' => 'int']; case static::PHINX_TYPE_TINY_INTEGER: @@ -1109,16 +1103,22 @@ public function getSqlType($type, $limit = null) * @internal param string $sqlType SQL type * * @param string $sqlType SQL Type definition + * @param int|null $charLength Column length value * * @throws \Phinx\Db\Adapter\UnsupportedColumnTypeException * * @return string Phinx type */ - public function getPhinxType($sqlType) + public function getPhinxType($sqlType, $charLength = null) { switch ($sqlType) { case 'nvarchar': case 'varchar': + if ($charLength == -1) { + // max char length + return static::PHINX_TYPE_TEXT; + } + return static::PHINX_TYPE_STRING; case 'char': case 'nchar':