Skip to content

Commit 0febc80

Browse files
authored
Keep NVARCHAR(MAX) length as -1 (#6251)
<!-- 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.
1 parent 81502f0 commit 0febc80

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Schema/SQLServerSchemaManager.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,20 @@ protected function _getPortableTableColumnDefinition($tableColumn)
133133

134134
switch ($dbType) {
135135
case 'nchar':
136-
case 'nvarchar':
137136
case 'ntext':
138137
// Unicode data requires 2 bytes per character
139138
$length /= 2;
140139
break;
141140

141+
case 'nvarchar':
142+
if ($length === -1) {
143+
break;
144+
}
145+
146+
// Unicode data requires 2 bytes per character
147+
$length /= 2;
148+
break;
149+
142150
case 'varchar':
143151
// TEXT type is returned as VARCHAR(MAX) with a length of -1
144152
if ($length === -1) {

tests/Functional/Schema/SQLServerSchemaManagerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,19 @@ public function testPkOrdering(): void
262262
self::assertEquals('colB', $columns[0]);
263263
self::assertEquals('colA', $columns[1]);
264264
}
265+
266+
public function testNvarcharMaxIsLengthMinus1(): void
267+
{
268+
$sql = 'CREATE TABLE test_nvarchar_max (
269+
col_nvarchar_max NVARCHAR(MAX),
270+
col_nvarchar NVARCHAR(128)
271+
)';
272+
273+
$this->connection->executeStatement($sql);
274+
275+
$table = $this->schemaManager->introspectTable('test_nvarchar_max');
276+
277+
self::assertSame(-1, $table->getColumn('col_nvarchar_max')->getLength());
278+
self::assertSame(128, $table->getColumn('col_nvarchar')->getLength());
279+
}
265280
}

0 commit comments

Comments
 (0)