Skip to content

Commit

Permalink
Keep NVARCHAR(MAX) length as -1
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong authored and derrabus committed Jan 20, 2024
1 parent 81502f0 commit 9a256b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Schema/SQLServerSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,20 @@ protected function _getPortableTableColumnDefinition($tableColumn)

switch ($dbType) {
case 'nchar':
case 'nvarchar':
case 'ntext':
// Unicode data requires 2 bytes per character
$length /= 2;
break;

case 'nvarchar':
if ($length === -1) {
break;
}

// Unicode data requires 2 bytes per character
$length /= 2;
break;

case 'varchar':
// TEXT type is returned as VARCHAR(MAX) with a length of -1
if ($length === -1) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Functional/Schema/SQLServerSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,19 @@ public function testPkOrdering(): void
self::assertEquals('colB', $columns[0]);
self::assertEquals('colA', $columns[1]);
}

public function testNvarcharMaxIsLengthMinus1(): void
{
$sql = 'CREATE TABLE test_nvarchar_max (
col_nvarchar_max NVARCHAR(MAX),
col_nvarchar NVARCHAR(128)
)';

$this->connection->executeStatement($sql);

$table = $this->schemaManager->introspectTable('test_nvarchar_max');

self::assertSame(-1, $table->getColumn('col_nvarchar_max')->getLength());
self::assertSame(128, $table->getColumn('col_nvarchar')->getLength());
}
}

0 comments on commit 9a256b9

Please sign in to comment.