diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index dea7536de984..273e4c60c4d5 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -1684,9 +1684,11 @@ protected function getDriverFunctionPrefix(): string public function listTables(bool $constrainByPrefix = false) { if (isset($this->dataCache['table_names']) && $this->dataCache['table_names']) { - return $constrainByPrefix + $tables = $constrainByPrefix ? preg_grep("/^{$this->DBPrefix}/", $this->dataCache['table_names']) : $this->dataCache['table_names']; + + return array_values($tables); } $sql = $this->_listTables($constrainByPrefix); diff --git a/tests/system/Database/Live/MetadataTest.php b/tests/system/Database/Live/MetadataTest.php index ad70d281af0a..5030a6544231 100644 --- a/tests/system/Database/Live/MetadataTest.php +++ b/tests/system/Database/Live/MetadataTest.php @@ -78,7 +78,7 @@ private function dropExtraneousTable(): void $oldPrefix = $this->db->getPrefix(); $this->db->setPrefix('tmp_'); - Database::forge($this->DBGroup)->dropTable('widgets'); + Database::forge($this->DBGroup)->dropTable('widgets', true); $this->db->setPrefix($oldPrefix); } @@ -139,4 +139,22 @@ public function testListTablesConstrainedByExtraneousPrefixReturnsOnlyTheExtrane $this->dropExtraneousTable(); } } + + public function testListTablesReturnsListAfterCachedTableIsDropped(): void + { + try { + $this->createExtraneousTable(); + + $tables = $this->db->listTables(); + $this->assertSame(array_values($tables), $tables); + + $this->dropExtraneousTable(); + + $tables = $this->db->listTables(); + $this->assertSame(array_values($tables), $tables); + $this->assertNotContains('tmp_widgets', $tables); + } finally { + $this->dropExtraneousTable(); + } + } } diff --git a/user_guide_src/source/changelogs/v4.7.3.rst b/user_guide_src/source/changelogs/v4.7.3.rst index 469a2e8637a0..84feb6d280e8 100644 --- a/user_guide_src/source/changelogs/v4.7.3.rst +++ b/user_guide_src/source/changelogs/v4.7.3.rst @@ -41,8 +41,9 @@ Bugs Fixed - **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``tput`` error output (``tput: No value for $TERM and no -T specified``) to stderr when the ``stty`` fallback was reached and the ``TERM`` environment variable was not set. - **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. - **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. -- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false. +- **Database:** Fixed a bug where ``BaseConnection::listTables()`` could return a sparse array when using cached table names after a table was dropped. - **Database:** Fixed a bug where the PostgreSQL driver's ``increment()`` and ``decrement()`` methods were not working for numeric columns. +- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false. - **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets. - **Toolbar:** Fixed a bug where the Logs collector raised an undefined property error when using a third-party PSR-3 logger. - **Time:** Fixed a bug where ``Time::createFromTimestamp()`` could fail for microsecond timestamps when ``LC_NUMERIC`` used a comma decimal separator.