From ff34953ae7b366fb72b9a7fd17aa01c1d26e95c1 Mon Sep 17 00:00:00 2001 From: patel-vansh Date: Thu, 7 May 2026 14:52:56 +0530 Subject: [PATCH 1/3] Fixed the bug --- system/Database/Postgre/Builder.php | 4 +- .../Database/Live/Postgre/IncrementTest.php | 70 +++++++++++++++++++ user_guide_src/source/changelogs/v4.7.3.rst | 1 + 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/system/Database/Live/Postgre/IncrementTest.php diff --git a/system/Database/Postgre/Builder.php b/system/Database/Postgre/Builder.php index 126ab5741892..c5381162cb22 100644 --- a/system/Database/Postgre/Builder.php +++ b/system/Database/Postgre/Builder.php @@ -97,7 +97,7 @@ public function increment(string $column, int $value = 1) { $column = $this->db->protectIdentifiers($column); - $sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') + {$value}"]); + $sql = $this->_update($this->QBFrom[0], [$column => "CAST({$column} AS numeric) + {$value}"]); if (! $this->testMode) { $this->resetWrite(); @@ -119,7 +119,7 @@ public function decrement(string $column, int $value = 1) { $column = $this->db->protectIdentifiers($column); - $sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') - {$value}"]); + $sql = $this->_update($this->QBFrom[0], [$column => "CAST({$column} AS numeric) - {$value}"]); if (! $this->testMode) { $this->resetWrite(); diff --git a/tests/system/Database/Live/Postgre/IncrementTest.php b/tests/system/Database/Live/Postgre/IncrementTest.php new file mode 100644 index 000000000000..c35a1e12eec2 --- /dev/null +++ b/tests/system/Database/Live/Postgre/IncrementTest.php @@ -0,0 +1,70 @@ +db->DBDriver !== 'Postgre') { + $this->markTestSkipped('This test is only for Postgre.'); + } + } + + public function testIncrementWithNumericColumns(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->increment('created_at'); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]); + } + + public function testIncrementWithNumericColumnsAndValue(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->increment('created_at', 2); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 8]); + } + + public function testDecrementWithNumericColumns(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->decrement('created_at'); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 5]); + } + + public function testDecrementWithNumericColumnsAndValue(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->decrement('created_at', 2); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 4]); + } +} diff --git a/user_guide_src/source/changelogs/v4.7.3.rst b/user_guide_src/source/changelogs/v4.7.3.rst index 06418cf9e5e8..699b2bfedefb 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. - **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false. +- **Database:** Fixed a bug where the PostgreSQL driver's ``increment()`` and ``decrement()`` methods were not working for numeric columns. - **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets. - **Time:** Fixed a bug where ``Time::createFromTimestamp()`` could fail for microsecond timestamps when ``LC_NUMERIC`` used a comma decimal separator. - **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``. From 6bd4dfe86494e04cab8d3061d918d0063f547f1a Mon Sep 17 00:00:00 2001 From: patel-vansh Date: Thu, 7 May 2026 14:55:15 +0530 Subject: [PATCH 2/3] cs-fix --- .../Database/Live/Postgre/IncrementTest.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/system/Database/Live/Postgre/IncrementTest.php b/tests/system/Database/Live/Postgre/IncrementTest.php index c35a1e12eec2..9f6889686099 100644 --- a/tests/system/Database/Live/Postgre/IncrementTest.php +++ b/tests/system/Database/Live/Postgre/IncrementTest.php @@ -1,5 +1,16 @@ + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + namespace CodeIgniter\Database\Live\Postgre; use CodeIgniter\Test\CIUnitTestCase; @@ -7,8 +18,11 @@ use PHPUnit\Framework\Attributes\Group; use Tests\Support\Database\Seeds\CITestSeeder; +/** + * @internal + */ #[Group('DatabaseLive')] -class IncrementTest extends CIUnitTestCase +final class IncrementTest extends CIUnitTestCase { use DatabaseTestTrait; From 7f61f6dfe44cd4e95a6d93654a9801ec6b4f4093 Mon Sep 17 00:00:00 2001 From: patel-vansh Date: Thu, 7 May 2026 18:18:29 +0530 Subject: [PATCH 3/3] move tests from separate file to Live/IncrementTest.php --- tests/system/Database/Live/IncrementTest.php | 44 ++++++++++ .../Database/Live/Postgre/IncrementTest.php | 84 ------------------- 2 files changed, 44 insertions(+), 84 deletions(-) delete mode 100644 tests/system/Database/Live/Postgre/IncrementTest.php diff --git a/tests/system/Database/Live/IncrementTest.php b/tests/system/Database/Live/IncrementTest.php index 0051e1a9039d..3a9155566e9c 100644 --- a/tests/system/Database/Live/IncrementTest.php +++ b/tests/system/Database/Live/IncrementTest.php @@ -51,6 +51,28 @@ public function testIncrementWithValue(): void $this->seeInDatabase('job', ['name' => 'incremental', 'description' => '8']); } + public function testIncrementWithNumericColumns(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->increment('created_at'); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]); + } + + public function testIncrementWithNumericColumnsAndValue(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->increment('created_at', 2); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 8]); + } + public function testResetStateAfterIncrement(): void { $this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']); @@ -87,6 +109,28 @@ public function testDecrementWithValue(): void $this->seeInDatabase('job', ['name' => 'incremental', 'description' => '4']); } + public function testDecrementWithNumericColumns(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->decrement('created_at'); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 5]); + } + + public function testDecrementWithNumericColumnsAndValue(): void + { + $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); + + $this->db->table('job') + ->where('name', 'incremental') + ->decrement('created_at', 2); + + $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 4]); + } + public function testResetStateAfterDecrement(): void { $this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']); diff --git a/tests/system/Database/Live/Postgre/IncrementTest.php b/tests/system/Database/Live/Postgre/IncrementTest.php deleted file mode 100644 index 9f6889686099..000000000000 --- a/tests/system/Database/Live/Postgre/IncrementTest.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace CodeIgniter\Database\Live\Postgre; - -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 !== 'Postgre') { - $this->markTestSkipped('This test is only for Postgre.'); - } - } - - public function testIncrementWithNumericColumns(): void - { - $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); - - $this->db->table('job') - ->where('name', 'incremental') - ->increment('created_at'); - - $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]); - } - - public function testIncrementWithNumericColumnsAndValue(): void - { - $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); - - $this->db->table('job') - ->where('name', 'incremental') - ->increment('created_at', 2); - - $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 8]); - } - - public function testDecrementWithNumericColumns(): void - { - $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); - - $this->db->table('job') - ->where('name', 'incremental') - ->decrement('created_at'); - - $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 5]); - } - - public function testDecrementWithNumericColumnsAndValue(): void - { - $this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]); - - $this->db->table('job') - ->where('name', 'incremental') - ->decrement('created_at', 2); - - $this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 4]); - } -}