From 8dacd2aad7ca17289514b8c95aaf49df1754ba39 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Axel=20T=C3=B3masson?= <axel@isnic.is>
Date: Tue, 18 Jun 2024 09:30:42 +0000
Subject: [PATCH] Fix #20195: Do not set non abstract values into
 `ColumnSchema->type` on MSSQL version less then 2017

---
 framework/CHANGELOG.md        |  1 +
 framework/db/mssql/Schema.php | 32 +++++++++-----------------------
 2 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md
index aa238ecd229..561c88adc86 100644
--- a/framework/CHANGELOG.md
+++ b/framework/CHANGELOG.md
@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
 2.0.51 under development
 ------------------------
 
+- Bug #20195: Do not set non abstract values into `ColumnSchema->type` on MSSQL version less then 2017 (axeltomasson)
 - Bug #16116: Codeception: oci does not support enabling/disabling integrity check (@terabytesoftw)
 - Bug #20191: Fix `ActiveRecord::getDirtyAttributes()` for JSON columns with multi-dimensional array values (brandonkelly)
 - Bug #20175: Fix bad result for pagination when used with GridView (@lav45)
diff --git a/framework/db/mssql/Schema.php b/framework/db/mssql/Schema.php
index 96c09905801..35908437f48 100644
--- a/framework/db/mssql/Schema.php
+++ b/framework/db/mssql/Schema.php
@@ -408,7 +408,15 @@ protected function loadColumnSchema($info)
                 }
 
                 if ($isVersion2017orLater === false) {
-                    $column->type = $this->booleanTypeLegacy($column->size, $type);
+                    if ($column->size === 1 && ($type === 'tinyint' || $type === 'bit')) {
+                        $column->type = 'boolean';
+                    } elseif ($type === 'bit') {
+                        if ($column->size > 32) {
+                            $column->type = 'bigint';
+                        } elseif ($column->size === 32) {
+                            $column->type = 'integer';
+                        }
+                    }
                 }
             }
         }
@@ -816,26 +824,4 @@ public function createColumnSchemaBuilder($type, $length = null)
         return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]);
     }
 
-    /**
-     * Assigns a type boolean for the column type bit, for legacy versions of MSSQL.
-     *
-     * @param int $size column size.
-     * @param string $type column type.
-     *
-     * @return string column type.
-     */
-    private function booleanTypeLegacy($size, $type)
-    {
-        if ($size === 1 && ($type === 'tinyint' || $type === 'bit')) {
-            return 'boolean';
-        } elseif ($type === 'bit') {
-            if ($size > 32) {
-                return 'bigint';
-            } elseif ($size === 32) {
-                return 'integer';
-            }
-        }
-
-        return $type;
-    }
 }