Skip to content

Commit

Permalink
Fixed bug that PostgresSQL can't work when create connection timed ou…
Browse files Browse the repository at this point in the history
…t. (#5327)



Co-authored-by: 李铭昕 <[email protected]>
  • Loading branch information
her-cat and limingxinleo authored Jan 15, 2023
1 parent 9e51de6 commit 595c4c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Connectors/PostgresSqlSwooleExtConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function createConnection(array $config): PostgreSQL
));

if ($result === false) {
throw new Exception($connection->error);
throw new Exception($connection->error ?? 'Create connection failed, Please check the database configuration.');
}

return $connection;
Expand Down
22 changes: 22 additions & 0 deletions tests/Cases/PostgreSqlSwooleExtConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace HyperfTest\Database\PgSQL\Cases;

use Exception;
use Hyperf\Database\Connection;
use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Exception\QueryException;
Expand Down Expand Up @@ -89,4 +90,25 @@ public function testAffectingStatementWithWrongSql()

$connection->affectingStatement('UPDATE xx SET x = 1 WHERE id = 1');
}

public function testCreateConnectionTimedOut()
{
if (SWOOLE_MAJOR_VERSION < 5) {
$this->markTestSkipped('PostgreSql requires Swoole version >= 5.0.0');
}

$connection = $this->connectionFactory->make([
'driver' => 'pgsql-swoole',
'host' => 'non-existent-host.internal',
'port' => 5432,
'database' => 'postgres',
'username' => 'postgres',
'password' => 'postgres',
]);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Create connection failed, Please check the database configuration.');

$connection->affectingStatement('UPDATE xx SET x = 1 WHERE id = 1');
}
}

0 comments on commit 595c4c8

Please sign in to comment.