Skip to content

Commit 906405b

Browse files
committed
fix(Database): reset pretend mode in BaseConnection::prepare() on exception
BaseConnection::prepare() enables pretend mode while the user closure builds the query. If the closure threw, the following pretend(false) never ran, leaving the connection in pretend mode. Every later query on the same connection was then silently discarded instead of being executed, which is especially harmful in long-lived processes such as Worker Mode. Wrap the closure call in try/finally so pretend mode is always reset, and add a regression test covering the throwing closure.
1 parent ae19ffd commit 906405b

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Release Date: Unreleased
2020

2121
- **Cache:** Fixed a bug where `PredisHandler::increment()` and `PredisHandler::decrement()` updated the unused `'data'` hash field instead of `'__ci_value'`, causing `get()` to return stale values for existing counters.
2222
- **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers.
23+
- **Database:** Fixed a bug where `BaseConnection::prepare()` did not reset pretend mode when the closure threw an exception, leaving the connection in pretend mode so that subsequent queries on it were silently discarded.
2324
- **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping.
2425
- **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping.
2526
- **Database:** Fixed a bug where `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters.

‎system/Database/BaseConnection.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,9 +1188,11 @@ public function prepare(Closure $func, array $options = [])
11881188

11891189
$this->pretend();
11901190

1191-
$sql = $func($this);
1192-
1193-
$this->pretend(false);
1191+
try {
1192+
$sql = $func($this);
1193+
} finally {
1194+
$this->pretend(false);
1195+
}
11941196

11951197
if ($sql instanceof QueryInterface) {
11961198
$sql = $sql->getOriginalQuery();

‎tests/system/Database/Live/PretendTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\DatabaseTestTrait;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use RuntimeException;
2021

2122
/**
2223
* @internal
@@ -46,4 +47,23 @@ public function testPretendReturnsQueryObject(): void
4647

4748
$this->assertInstanceOf(Query::class, $result);
4849
}
50+
51+
public function testPrepareResetsPretendWhenClosureThrowsException(): void
52+
{
53+
$this->db->pretend(false);
54+
55+
$exception = new RuntimeException('Error in prepare closure');
56+
57+
try {
58+
$this->db->prepare(static function () use ($exception): never {
59+
throw $exception;
60+
});
61+
62+
$this->fail('prepare() must rethrow the exception thrown by the closure.');
63+
} catch (RuntimeException $e) {
64+
$this->assertSame($exception, $e, 'prepare() must rethrow the exception thrown by the closure.');
65+
}
66+
67+
$this->assertFalse($this->db->pretend, 'prepare() must reset pretend mode when the closure throws.');
68+
}
4969
}

0 commit comments

Comments
 (0)