diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index d817d9932f94..38442a8c6201 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -20,6 +20,7 @@ Release Date: Unreleased - **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. - **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. +- **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. - **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping. - **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping. - **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. diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index d025453f74ba..9060931d4c55 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -1188,9 +1188,11 @@ public function prepare(Closure $func, array $options = []) $this->pretend(); - $sql = $func($this); - - $this->pretend(false); + try { + $sql = $func($this); + } finally { + $this->pretend(false); + } if ($sql instanceof QueryInterface) { $sql = $sql->getOriginalQuery(); diff --git a/tests/system/Database/Live/PretendTest.php b/tests/system/Database/Live/PretendTest.php index b3e6506c1be6..4e9605951057 100644 --- a/tests/system/Database/Live/PretendTest.php +++ b/tests/system/Database/Live/PretendTest.php @@ -17,6 +17,7 @@ use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\DatabaseTestTrait; use PHPUnit\Framework\Attributes\Group; +use RuntimeException; /** * @internal @@ -46,4 +47,23 @@ public function testPretendReturnsQueryObject(): void $this->assertInstanceOf(Query::class, $result); } + + public function testPrepareResetsPretendWhenClosureThrowsException(): void + { + $this->db->pretend(false); + + $exception = new RuntimeException('Error in prepare closure'); + + try { + $this->db->prepare(static function () use ($exception): never { + throw $exception; + }); + + $this->fail('prepare() must rethrow the exception thrown by the closure.'); + } catch (RuntimeException $e) { + $this->assertSame($exception, $e, 'prepare() must rethrow the exception thrown by the closure.'); + } + + $this->assertFalse($this->db->pretend, 'prepare() must reset pretend mode when the closure throws.'); + } }