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/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/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``.