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

SQL Server adapter uses nvarchar(max) instead of ntext and always updates default constraint #1977

Open
wants to merge 5 commits into
base: 0.x
Choose a base branch
from
Open
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
26 changes: 13 additions & 13 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand All @@ -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']);
}

Expand Down Expand Up @@ -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();

Expand All @@ -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',
Expand All @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down