Skip to content

refactor: [SQLSRV] convert ENUM to VARCHAR(n) in Forge #8478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,18 @@ protected function _attributeType(array &$attributes)
break;

case 'ENUM':
$attributes['TYPE'] = 'TEXT';
$attributes['CONSTRAINT'] = null;
// in char(n) and varchar(n), the n defines the string length in
// bytes (0 to 8,000).
// https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-ver16#remarks
$maxLength = max(
array_map(
static fn ($value) => strlen($value),
$attributes['CONSTRAINT']
)
);

$attributes['TYPE'] = 'VARCHAR';
$attributes['CONSTRAINT'] = $maxLength;
break;

case 'TIMESTAMP':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public function up(): void
if ($this->db->DBDriver === 'Postgre') {
unset(
$dataTypeFields['type_real'],
$dataTypeFields['type_decimal']
$dataTypeFields['type_decimal'],
$dataTypeFields['type_enum'],
);
}

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

if ($this->db->DBDriver === 'Postgre' || $this->db->DBDriver === 'SQLSRV') {
unset(
$dataTypeFields['type_enum'],
$dataTypeFields['type_set'],
$dataTypeFields['type_mediumtext'],
$dataTypeFields['type_double'],
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Database/Live/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ public function testGetFieldData(): void
$this->assertNull($typeTest[10]->type_name); // DATETIME
$this->assertSame('bigint', $typeTest[11]->type_name); // BIGINT
$this->assertSame('real', $typeTest[12]->type_name); // REAL
$this->assertSame('decimal', $typeTest[13]->type_name); // DECIMAL
$this->assertSame('varchar', $typeTest[13]->type_name); // ENUM
$this->assertSame('decimal', $typeTest[14]->type_name); // DECIMAL
}
}

Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ Changes
has been fixed from lowercase to uppercase.
- **Exceptions:** Unused ``CodeIgniter\Exceptions\AlertError`` and
``CodeIgniter\Exceptions\EmergencyError`` were removed.
- **Forge:** ``SQLSRV`` Forge now converts ``ENUM`` data types to ``VARCHAR(n)``
when you add table columns. In previous version, it converted to ``TEXT`` that
is deprecated in SQL Server.
- ``declare(strict_types=1)`` has been added to most framework codebase.

Deprecations
Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ TEXT
``TEXT`` should not be used on SQLSRV. It is deprecated.
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>`_.

ENUM
^^^^

Not all databases support ``ENUM``.

Starting with v4.5.0, ``SQLSRV`` Forge converts ``ENUM`` data types to ``VARCHAR(n)``.
Previous versions converted to ``TEXT``.

.. _forge-addfield-default-value-rawsql:

Raw Sql Strings as Default Values
Expand Down