From dd929c0262a533536132015f5a37c8b4f38a36ab Mon Sep 17 00:00:00 2001 From: patel-vansh Date: Sun, 3 May 2026 16:02:51 +0530 Subject: [PATCH 1/3] Fix the bug --- system/Database/SQLSRV/Builder.php | 2 +- user_guide_src/source/changelogs/v4.7.3.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/system/Database/SQLSRV/Builder.php b/system/Database/SQLSRV/Builder.php index 96890bf45cb2..44af621658a6 100644 --- a/system/Database/SQLSRV/Builder.php +++ b/system/Database/SQLSRV/Builder.php @@ -269,7 +269,7 @@ public function decrement(string $column, int $value = 1) if ($this->castTextToInt) { $values = [$column => "CONVERT(VARCHAR(MAX),CONVERT(INT,CONVERT(VARCHAR(MAX), {$column})) - {$value})"]; } else { - $values = [$column => "{$column} + {$value}"]; + $values = [$column => "{$column} - {$value}"]; } $sql = $this->_update($this->QBFrom[0], $values); diff --git a/user_guide_src/source/changelogs/v4.7.3.rst b/user_guide_src/source/changelogs/v4.7.3.rst index 677e1072500b..a71b7982051d 100644 --- a/user_guide_src/source/changelogs/v4.7.3.rst +++ b/user_guide_src/source/changelogs/v4.7.3.rst @@ -41,6 +41,7 @@ Bugs Fixed - **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment. - **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown. - **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``. +- **SQLSRV Database Driver:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false. See the repo's `CHANGELOG.md `_ From f81eae17ff8a432f18f1aa66ec258629e4fccedd Mon Sep 17 00:00:00 2001 From: patel-vansh Date: Tue, 5 May 2026 15:13:24 +0530 Subject: [PATCH 2/3] Added tests --- .../Database/Live/SQLSRV/IncrementTest.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/system/Database/Live/SQLSRV/IncrementTest.php diff --git a/tests/system/Database/Live/SQLSRV/IncrementTest.php b/tests/system/Database/Live/SQLSRV/IncrementTest.php new file mode 100644 index 000000000000..b4e563905635 --- /dev/null +++ b/tests/system/Database/Live/SQLSRV/IncrementTest.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live\SQLSRV; + +use CodeIgniter\Database\SQLSRV\Builder; +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\DatabaseTestTrait; +use PHPUnit\Framework\Attributes\Group; +use Tests\Support\Database\Seeds\CITestSeeder; + +/** + * @internal + */ +#[Group('DatabaseLive')] +final class IncrementTest extends CIUnitTestCase +{ + use DatabaseTestTrait; + + protected $refresh = true; + protected $seed = CITestSeeder::class; + + protected function setUp(): void + { + parent::setUp(); + + if ($this->db->DBDriver !== 'SQLSRV') { + $this->markTestSkipped('This test is only for SQLSRV.'); + } + } + + public function testIncrementWhenCastTextToIntFalse(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->assertTrue($this->db->table('job') instanceof Builder); + + $this->db->table('job')->castTextToInt = false; + + $this->db->table('job') + ->where('name', 'incremental') + ->increment('created_at'); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]); + } + + public function testDecrementWhenCastTextToIntFalse(): void + { + $this->hasInDatabase('job', ['name' => 'decremental', 'created_at' => 6]); + + $this->assertTrue($this->db->table('job') instanceof Builder); + + $this->db->table('job')->castTextToInt = false; + + $this->db->table('job') + ->where('name', 'decremental') + ->decrement('created_at'); + + $this->seeInDatabase('job', ['name' => 'decremental', 'created_at' => 5]); + } +} From 242a4d1e25afe700bd95a31cba5c6f73ed4a1492 Mon Sep 17 00:00:00 2001 From: patel-vansh Date: Tue, 5 May 2026 15:16:03 +0530 Subject: [PATCH 3/3] change assertTrue to assertInstanceOf --- tests/system/Database/Live/SQLSRV/IncrementTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Database/Live/SQLSRV/IncrementTest.php b/tests/system/Database/Live/SQLSRV/IncrementTest.php index b4e563905635..0296f7d776f4 100644 --- a/tests/system/Database/Live/SQLSRV/IncrementTest.php +++ b/tests/system/Database/Live/SQLSRV/IncrementTest.php @@ -43,7 +43,7 @@ public function testIncrementWhenCastTextToIntFalse(): void { $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); - $this->assertTrue($this->db->table('job') instanceof Builder); + $this->assertInstanceOf(Builder::class, $this->db->table('job')); $this->db->table('job')->castTextToInt = false; @@ -58,7 +58,7 @@ public function testDecrementWhenCastTextToIntFalse(): void { $this->hasInDatabase('job', ['name' => 'decremental', 'created_at' => 6]); - $this->assertTrue($this->db->table('job') instanceof Builder); + $this->assertInstanceOf(Builder::class, $this->db->table('job')); $this->db->table('job')->castTextToInt = false;