Skip to content

Commit

Permalink
Keep NVARCHAR(MAX) length as -1 (#6251)
Browse files Browse the repository at this point in the history
<!-- Fill in the relevant information below to help triage your pull
request. -->

|      Q       |   A
|------------- | -----------
| Type         | bug
| Fixed issues | #6038

#### Summary

As described in the mentioned issue, `-0.5` from `-1 / 2` will raise
type error when trying to `setLength`

```
Argument #1 ($length) must be of type ?int, float given
```

https://github.com/doctrine/dbal/blob/8bc0d9ae42f20af54120396ef7a1b3248479c2b5/src/Schema/Column.php#L78

This PR check if length is `-1` and keep it as is before perform
division.
  • Loading branch information
kitloong committed Jan 20, 2024
1 parent 81502f0 commit 0febc80
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 0febc80

Please sign in to comment.