Skip to content

Commit ff34953

Browse files
committed
Fixed the bug
1 parent 6c4acef commit ff34953

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎system/Database/Postgre/Builder.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function increment(string $column, int $value = 1)
9797
{
9898
$column = $this->db->protectIdentifiers($column);
9999

100-
$sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') + {$value}"]);
100+
$sql = $this->_update($this->QBFrom[0], [$column => "CAST({$column} AS numeric) + {$value}"]);
101101

102102
if (! $this->testMode) {
103103
$this->resetWrite();
@@ -119,7 +119,7 @@ public function decrement(string $column, int $value = 1)
119119
{
120120
$column = $this->db->protectIdentifiers($column);
121121

122-
$sql = $this->_update($this->QBFrom[0], [$column => "to_number({$column}, '9999999') - {$value}"]);
122+
$sql = $this->_update($this->QBFrom[0], [$column => "CAST({$column} AS numeric) - {$value}"]);
123123

124124
if (! $this->testMode) {
125125
$this->resetWrite();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace CodeIgniter\Database\Live\Postgre;
4+
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
use CodeIgniter\Test\DatabaseTestTrait;
7+
use PHPUnit\Framework\Attributes\Group;
8+
use Tests\Support\Database\Seeds\CITestSeeder;
9+
10+
#[Group('DatabaseLive')]
11+
class IncrementTest extends CIUnitTestCase
12+
{
13+
use DatabaseTestTrait;
14+
15+
protected $refresh = true;
16+
protected $seed = CITestSeeder::class;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
if ($this->db->DBDriver !== 'Postgre') {
23+
$this->markTestSkipped('This test is only for Postgre.');
24+
}
25+
}
26+
27+
public function testIncrementWithNumericColumns(): void
28+
{
29+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
30+
31+
$this->db->table('job')
32+
->where('name', 'incremental')
33+
->increment('created_at');
34+
35+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]);
36+
}
37+
38+
public function testIncrementWithNumericColumnsAndValue(): void
39+
{
40+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
41+
42+
$this->db->table('job')
43+
->where('name', 'incremental')
44+
->increment('created_at', 2);
45+
46+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 8]);
47+
}
48+
49+
public function testDecrementWithNumericColumns(): void
50+
{
51+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
52+
53+
$this->db->table('job')
54+
->where('name', 'incremental')
55+
->decrement('created_at');
56+
57+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 5]);
58+
}
59+
60+
public function testDecrementWithNumericColumnsAndValue(): void
61+
{
62+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
63+
64+
$this->db->table('job')
65+
->where('name', 'incremental')
66+
->decrement('created_at', 2);
67+
68+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 4]);
69+
}
70+
}

‎user_guide_src/source/changelogs/v4.7.3.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bugs Fixed
4141
- **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.
4242
- **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.
4343
- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false.
44+
- **Database:** Fixed a bug where the PostgreSQL driver's ``increment()`` and ``decrement()`` methods were not working for numeric columns.
4445
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
4546
- **Time:** Fixed a bug where ``Time::createFromTimestamp()`` could fail for microsecond timestamps when ``LC_NUMERIC`` used a comma decimal separator.
4647
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.

0 commit comments

Comments
 (0)