Skip to content

Commit d22812d

Browse files
authored
Merge pull request #8478 from kenjis/fix-sqlsrv-forge-enum
refactor: [SQLSRV] convert ENUM to VARCHAR(n) in Forge
2 parents 6126950 + 88362f9 commit d22812d

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

system/Database/SQLSRV/Forge.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,18 @@ protected function _attributeType(array &$attributes)
326326
break;
327327

328328
case 'ENUM':
329-
$attributes['TYPE'] = 'TEXT';
330-
$attributes['CONSTRAINT'] = null;
329+
// in char(n) and varchar(n), the n defines the string length in
330+
// bytes (0 to 8,000).
331+
// https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-ver16#remarks
332+
$maxLength = max(
333+
array_map(
334+
static fn ($value) => strlen($value),
335+
$attributes['CONSTRAINT']
336+
)
337+
);
338+
339+
$attributes['TYPE'] = 'VARCHAR';
340+
$attributes['CONSTRAINT'] = $maxLength;
331341
break;
332342

333343
case 'TIMESTAMP':

tests/_support/Database/Migrations/20160428212500_Create_test_tables.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public function up(): void
8585
if ($this->db->DBDriver === 'Postgre') {
8686
unset(
8787
$dataTypeFields['type_real'],
88-
$dataTypeFields['type_decimal']
88+
$dataTypeFields['type_decimal'],
89+
$dataTypeFields['type_enum'],
8990
);
9091
}
9192

@@ -95,7 +96,6 @@ public function up(): void
9596

9697
if ($this->db->DBDriver === 'Postgre' || $this->db->DBDriver === 'SQLSRV') {
9798
unset(
98-
$dataTypeFields['type_enum'],
9999
$dataTypeFields['type_set'],
100100
$dataTypeFields['type_mediumtext'],
101101
$dataTypeFields['type_double'],

tests/system/Database/Live/GetTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public function testGetFieldData(): void
188188
$this->assertNull($typeTest[10]->type_name); // DATETIME
189189
$this->assertSame('bigint', $typeTest[11]->type_name); // BIGINT
190190
$this->assertSame('real', $typeTest[12]->type_name); // REAL
191-
$this->assertSame('decimal', $typeTest[13]->type_name); // DECIMAL
191+
$this->assertSame('varchar', $typeTest[13]->type_name); // ENUM
192+
$this->assertSame('decimal', $typeTest[14]->type_name); // DECIMAL
192193
}
193194
}
194195

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ Changes
403403
has been fixed from lowercase to uppercase.
404404
- **Exceptions:** Unused ``CodeIgniter\Exceptions\AlertError`` and
405405
``CodeIgniter\Exceptions\EmergencyError`` were removed.
406+
- **Forge:** ``SQLSRV`` Forge now converts ``ENUM`` data types to ``VARCHAR(n)``
407+
when you add table columns. In previous version, it converted to ``TEXT`` that
408+
is deprecated in SQL Server.
406409
- ``declare(strict_types=1)`` has been added to most framework codebase.
407410

408411
Deprecations

user_guide_src/source/dbmgmt/forge.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ TEXT
158158
``TEXT`` should not be used on SQLSRV. It is deprecated.
159159
See `ntext, text, and image (Transact-SQL) - SQL Server | Microsoft Learn <https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql?view=sql-server-ver16>`_.
160160

161+
ENUM
162+
^^^^
163+
164+
Not all databases support ``ENUM``.
165+
166+
Starting with v4.5.0, ``SQLSRV`` Forge converts ``ENUM`` data types to ``VARCHAR(n)``.
167+
Previous versions converted to ``TEXT``.
168+
161169
.. _forge-addfield-default-value-rawsql:
162170

163171
Raw Sql Strings as Default Values

0 commit comments

Comments
 (0)