Skip to content

Commit b72e0f8

Browse files
authored
fix: PostgreSQL Builder's increment() and decrement() methods not working for numeric columns (#10172)
1 parent 3405fc5 commit b72e0f8

3 files changed

Lines changed: 47 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();

tests/system/Database/Live/IncrementTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ public function testIncrementWithValue(): void
5151
$this->seeInDatabase('job', ['name' => 'incremental', 'description' => '8']);
5252
}
5353

54+
public function testIncrementWithNumericColumns(): void
55+
{
56+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
57+
58+
$this->db->table('job')
59+
->where('name', 'incremental')
60+
->increment('created_at');
61+
62+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 7]);
63+
}
64+
65+
public function testIncrementWithNumericColumnsAndValue(): void
66+
{
67+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
68+
69+
$this->db->table('job')
70+
->where('name', 'incremental')
71+
->increment('created_at', 2);
72+
73+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 8]);
74+
}
75+
5476
public function testResetStateAfterIncrement(): void
5577
{
5678
$this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']);
@@ -87,6 +109,28 @@ public function testDecrementWithValue(): void
87109
$this->seeInDatabase('job', ['name' => 'incremental', 'description' => '4']);
88110
}
89111

112+
public function testDecrementWithNumericColumns(): void
113+
{
114+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
115+
116+
$this->db->table('job')
117+
->where('name', 'incremental')
118+
->decrement('created_at');
119+
120+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 5]);
121+
}
122+
123+
public function testDecrementWithNumericColumnsAndValue(): void
124+
{
125+
$this->hasInDatabase('job', ['name' => 'incremental', 'created_at' => 6]);
126+
127+
$this->db->table('job')
128+
->where('name', 'incremental')
129+
->decrement('created_at', 2);
130+
131+
$this->seeInDatabase('job', ['name' => 'incremental', 'created_at' => 4]);
132+
}
133+
90134
public function testResetStateAfterDecrement(): void
91135
{
92136
$this->hasInDatabase('job', ['name' => 'account1', 'description' => '10']);

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Bugs Fixed
4242
- **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.
4343
- **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.
4444
- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false.
45+
- **Database:** Fixed a bug where the PostgreSQL driver's ``increment()`` and ``decrement()`` methods were not working for numeric columns.
4546
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
4647
- **Toolbar:** Fixed a bug where the Logs collector raised an undefined property error when using a third-party PSR-3 logger.
4748
- **Time:** Fixed a bug where ``Time::createFromTimestamp()`` could fail for microsecond timestamps when ``LC_NUMERIC`` used a comma decimal separator.

0 commit comments

Comments
 (0)