Skip to content

Commit 6eb8f72

Browse files
committed
feat: do not throw exceptions during transactions by default
1 parent e329f52 commit 6eb8f72

File tree

3 files changed

+118
-48
lines changed

3 files changed

+118
-48
lines changed

system/Database/BaseConnection.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* @property string $DBDriver
3030
* @property string $DBPrefix
3131
* @property string $DSN
32-
* @property mixed $encrypt
32+
* @property array|bool $encrypt
3333
* @property array $failover
3434
* @property string $hostname
3535
* @property Query $lastQuery
@@ -320,6 +320,11 @@ abstract class BaseConnection implements ConnectionInterface
320320
*/
321321
protected $transFailure = false;
322322

323+
/**
324+
* Whether to throw exceptions during transaction
325+
*/
326+
protected bool $transException = false;
327+
323328
/**
324329
* Array of table aliases.
325330
*
@@ -610,7 +615,15 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
610615
$this->transStatus = false;
611616
}
612617

613-
if ($this->DBDebug) {
618+
if (
619+
$this->DBDebug
620+
&& (
621+
// Not in transactions
622+
$this->transDepth === 0
623+
// In transactions, do not throw exception by default.
624+
|| $this->transException
625+
)
626+
) {
614627
// We call this function in order to roll-back queries
615628
// if transactions are enabled. If we don't call this here
616629
// the error message will trigger an exit, causing the
@@ -721,6 +734,18 @@ public function transStart(bool $testMode = false): bool
721734
return $this->transBegin($testMode);
722735
}
723736

737+
/**
738+
* If set to true, exceptions are thrown during transactions.
739+
*
740+
* @return $this
741+
*/
742+
public function transException(bool $transExcetion)
743+
{
744+
$this->transException = $transExcetion;
745+
746+
return $this;
747+
}
748+
724749
/**
725750
* Complete Transaction
726751
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\Live;
13+
14+
/**
15+
* @group DatabaseLive
16+
*
17+
* @internal
18+
*/
19+
final class TransactionDBDebugFalseTest extends TransactionDBDebugTrueTest
20+
{
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->disableDBDebug();
26+
}
27+
28+
protected function tearDown(): void
29+
{
30+
parent::tearDown();
31+
32+
$this->enableDBDebug();
33+
}
34+
35+
public function testTransStartTransException()
36+
{
37+
$builder = $this->db->table('job');
38+
39+
$this->db->transException(true)->transStart();
40+
41+
$jobData = [
42+
'name' => 'Grocery Sales',
43+
'description' => 'Discount!',
44+
];
45+
$builder->insert($jobData);
46+
47+
// Duplicate entry '1' for key 'PRIMARY'
48+
$jobData = [
49+
'id' => 1,
50+
'name' => 'Comedian',
51+
'description' => 'Theres something in your teeth',
52+
];
53+
$builder->insert($jobData);
54+
55+
$this->db->transComplete();
56+
57+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
58+
}
59+
}

tests/system/Database/Live/TransactionTest.php renamed to tests/system/Database/Live/TransactionDBDebugTrueTest.php

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
* @group DatabaseLive
2222
*
2323
* @internal
24+
*
25+
* @no-final
2426
*/
25-
final class TransactionTest extends CIUnitTestCase
27+
class TransactionDBDebugTrueTest extends CIUnitTestCase
2628
{
2729
use DatabaseTestTrait;
2830

@@ -55,13 +57,38 @@ protected function enableDBDebug(): void
5557
$this->setPrivateProperty($this->db, 'DBDebug', true);
5658
}
5759

58-
public function testTransStartDBDebugTrue()
60+
public function testTransStart()
61+
{
62+
$builder = $this->db->table('job');
63+
64+
$this->db->transStart();
65+
66+
$jobData = [
67+
'name' => 'Grocery Sales',
68+
'description' => 'Discount!',
69+
];
70+
$builder->insert($jobData);
71+
72+
// Duplicate entry '1' for key 'PRIMARY'
73+
$jobData = [
74+
'id' => 1,
75+
'name' => 'Comedian',
76+
'description' => 'Theres something in your teeth',
77+
];
78+
$builder->insert($jobData);
79+
80+
$this->db->transComplete();
81+
82+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
83+
}
84+
85+
public function testTransStartTransException()
5986
{
6087
$builder = $this->db->table('job');
6188
$e = null;
6289

6390
try {
64-
$this->db->transStart();
91+
$this->db->transException(true)->transStart();
6592

6693
$jobData = [
6794
'name' => 'Grocery Sales',
@@ -86,43 +113,8 @@ public function testTransStartDBDebugTrue()
86113
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
87114
}
88115

89-
public function testTransStartDBDebugFalse()
116+
public function testTransStrictTrue()
90117
{
91-
$this->disableDBDebug();
92-
93-
$builder = $this->db->table('job');
94-
95-
$this->db->transStart();
96-
97-
$jobData = [
98-
'name' => 'Grocery Sales',
99-
'description' => 'Discount!',
100-
];
101-
$builder->insert($jobData);
102-
103-
$this->assertTrue($this->db->transStatus());
104-
105-
// Duplicate entry '1' for key 'PRIMARY'
106-
$jobData = [
107-
'id' => 1,
108-
'name' => 'Comedian',
109-
'description' => 'Theres something in your teeth',
110-
];
111-
$builder->insert($jobData);
112-
113-
$this->assertFalse($this->db->transStatus());
114-
115-
$this->db->transComplete();
116-
117-
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
118-
119-
$this->enableDBDebug();
120-
}
121-
122-
public function testTransStrictTrueAndDBDebugFalse()
123-
{
124-
$this->disableDBDebug();
125-
126118
$builder = $this->db->table('job');
127119

128120
// The first transaction group
@@ -164,14 +156,10 @@ public function testTransStrictTrueAndDBDebugFalse()
164156
$this->db->transComplete();
165157

166158
$this->dontSeeInDatabase('job', ['name' => 'Comedian']);
167-
168-
$this->enableDBDebug();
169159
}
170160

171-
public function testTransStrictFalseAndDBDebugFalse()
161+
public function testTransStrictFalse()
172162
{
173-
$this->disableDBDebug();
174-
175163
$builder = $this->db->table('job');
176164

177165
$this->db->transStrict(false);
@@ -215,7 +203,5 @@ public function testTransStrictFalseAndDBDebugFalse()
215203
$this->db->transComplete();
216204

217205
$this->seeInDatabase('job', ['name' => 'Comedian']);
218-
219-
$this->enableDBDebug();
220206
}
221207
}

0 commit comments

Comments
 (0)