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 committed Jan 3, 2024
1 parent 8bc0d9a commit 62b57ae
Show file tree
Hide file tree
Showing 2 changed files with 22 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 @@ -83,12 +83,20 @@ protected function _getPortableTableColumnDefinition(array $tableColumn): Column

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
13 changes: 13 additions & 0 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,19 @@ protected function findTableByName(array $tables, string $name): ?Table

return null;
}

public function testNvarcharMaxIsLengthMinus1(): void
{
$createTableSQL = 'CREATE TABLE test_nvarchar_max (
col_nvarchar_max NVARCHAR(MAX),
col_nvarchar NVARCHAR(128)
)';
$this->connection->executeStatement($createTableSQL);

$table = $this->schemaManager->introspectTable('test_nvarchar_max');
self::assertSame(-1, $table->getColumn('col_nvarchar_max')->getLength());
self::assertSame(128, $table->getColumn('col_nvarchar')->getLength());
}
}

interface ListTableColumnsDispatchEventListener
Expand Down

0 comments on commit 62b57ae

Please sign in to comment.