From 0a45ea8a35afdecc51020553faadec29310a7ec1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 25 May 2024 09:33:37 +0900 Subject: [PATCH 1/3] test: add test for Postgre error message --- .../Database/Live/Postgre/ConnectTest.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/system/Database/Live/Postgre/ConnectTest.php diff --git a/tests/system/Database/Live/Postgre/ConnectTest.php b/tests/system/Database/Live/Postgre/ConnectTest.php new file mode 100644 index 000000000000..29de0976eb21 --- /dev/null +++ b/tests/system/Database/Live/Postgre/ConnectTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Database\Live\Postgre; + +use CodeIgniter\Database\Exceptions\DatabaseException; +use CodeIgniter\Test\CIUnitTestCase; +use Config\Database; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('DatabaseLive')] +final class ConnectTest extends CIUnitTestCase +{ + protected function setUp(): void + { + parent::setUp(); + + $this->db = Database::connect($this->DBGroup); + + if ($this->db->DBDriver !== 'Postgre') { + $this->markTestSkipped('This test is only for Postgre.'); + } + } + + public function testShowErrorMessageWhenSettingInvalidCharset(): void + { + $this->expectException(DatabaseException::class); + $this->expectExceptionMessage( + 'Unable to connect to the database. +Main connection [Postgre]: ERROR: invalid value for parameter "client_encoding": "utf8mb4"' + ); + + $config = config('Database'); + $group = $config->tests; + // Sets invalid charset. + $group['charset'] = 'utf8mb4'; + $db = Database::connect($group); + + // Actually connect to DB. + $db->initialize(); + } +} From 35dc6429c364cb3c9c8a294de97eecbd0e0433a6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 25 May 2024 09:15:53 +0900 Subject: [PATCH 2/3] fix: missing Postgre error message --- system/Database/BaseConnection.php | 1 + system/Database/Postgre/Connection.php | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index 77c9bd39ef3e..6325fd691093 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -420,6 +420,7 @@ public function initialize() // Connect to the database and set the connection ID $this->connID = $this->connect($this->pConnect); } catch (Throwable $e) { + $this->connID = false; $connectionErrors[] = sprintf('Main connection [%s]: %s', $this->DBDriver, $e->getMessage()); log_message('error', 'Error connecting to the database: ' . $e); } diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php index 126aa98bb1c2..b1b41d411b7e 100644 --- a/system/Database/Postgre/Connection.php +++ b/system/Database/Postgre/Connection.php @@ -80,7 +80,9 @@ public function connect(bool $persistent = false) if ($this->connID !== false) { if ($persistent === true && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD && pg_ping($this->connID) === false ) { - return false; + $error = pg_last_error($this->connID); + + throw new DatabaseException($error); } if (! empty($this->schema)) { @@ -88,7 +90,9 @@ public function connect(bool $persistent = false) } if ($this->setClientEncoding($this->charset) === false) { - return false; + $error = pg_last_error($this->connID); + + throw new DatabaseException($error); } } From 8b3e4e377c2a7cd995781468b27882adbe73d6fe Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 25 May 2024 09:20:20 +0900 Subject: [PATCH 3/3] style: break long lines --- system/Database/BaseConnection.php | 13 +++++++++++-- system/Database/Postgre/Connection.php | 5 ++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index 6325fd691093..57c37d8a0a6c 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -421,7 +421,11 @@ public function initialize() $this->connID = $this->connect($this->pConnect); } catch (Throwable $e) { $this->connID = false; - $connectionErrors[] = sprintf('Main connection [%s]: %s', $this->DBDriver, $e->getMessage()); + $connectionErrors[] = sprintf( + 'Main connection [%s]: %s', + $this->DBDriver, + $e->getMessage() + ); log_message('error', 'Error connecting to the database: ' . $e); } @@ -442,7 +446,12 @@ public function initialize() // Try to connect $this->connID = $this->connect($this->pConnect); } catch (Throwable $e) { - $connectionErrors[] = sprintf('Failover #%d [%s]: %s', ++$index, $this->DBDriver, $e->getMessage()); + $connectionErrors[] = sprintf( + 'Failover #%d [%s]: %s', + ++$index, + $this->DBDriver, + $e->getMessage() + ); log_message('error', 'Error connecting to the database: ' . $e); } diff --git a/system/Database/Postgre/Connection.php b/system/Database/Postgre/Connection.php index b1b41d411b7e..c22dc8605f56 100644 --- a/system/Database/Postgre/Connection.php +++ b/system/Database/Postgre/Connection.php @@ -78,7 +78,10 @@ public function connect(bool $persistent = false) $this->connID = $persistent === true ? pg_pconnect($this->DSN) : pg_connect($this->DSN); if ($this->connID !== false) { - if ($persistent === true && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD && pg_ping($this->connID) === false + if ( + $persistent === true + && pg_connection_status($this->connID) === PGSQL_CONNECTION_BAD + && pg_ping($this->connID) === false ) { $error = pg_last_error($this->connID);