mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Add error checking for PostgreSQLStatement::execute
method. (#5121)
This commit is contained in:
parent
c2a4b587c4
commit
677c9da9bc
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
@ -27,6 +27,7 @@ jobs:
|
||||
env:
|
||||
SW_VERSION: ${{ matrix.sw-version }}
|
||||
MYSQL_VERSION: '5.7'
|
||||
PGSQL_VERSION: '14'
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
@ -64,6 +65,7 @@ jobs:
|
||||
- name: Setup Services
|
||||
run: |
|
||||
docker run --name mysql -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=true -d mysql:${MYSQL_VERSION} --bind-address=0.0.0.0 --default-authentication-plugin=mysql_native_password
|
||||
docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres:${PGSQL_VERSION}
|
||||
docker run --name redis -p 6379:6379 -d redis
|
||||
docker run -d --name dev-consul -e CONSUL_BIND_INTERFACE=eth0 -p 8500:8500 consul
|
||||
docker run --name nsq -p 4150:4150 -p 4151:4151 -p 4160:4160 -p 4161:4161 -p 4170:4170 -p 4171:4171 --entrypoint /bin/nsqd -d nsqio/nsq:latest
|
||||
@ -76,6 +78,8 @@ jobs:
|
||||
docker run -d --name tcp-server -p 10001:10001 tcp-server:latest
|
||||
- name: Setup Mysql
|
||||
run: export TRAVIS_BUILD_DIR=$(pwd) && bash ./.travis/setup.mysql.sh
|
||||
- name: Setup PostgreSQL
|
||||
run: export TRAVIS_BUILD_DIR=$(pwd) && bash ./.travis/setup.pgsql.sh
|
||||
- name: Run Scripts Before Test
|
||||
run: cp .travis/.env.example .env
|
||||
- name: Run Test Cases
|
||||
|
12
.travis/pgsql.sql
Normal file
12
.travis/pgsql.sql
Normal file
@ -0,0 +1,12 @@
|
||||
CREATE TABLE users
|
||||
(
|
||||
id SERIAL
|
||||
constraint users_id
|
||||
primary key,
|
||||
email varchar(255) not null
|
||||
constraint users_email
|
||||
unique,
|
||||
name varchar(255) not null
|
||||
);
|
||||
|
||||
INSERT INTO public.users (id, email, name) VALUES (DEFAULT, 'test@hyperf.io', 'hyperf');
|
15
.travis/setup.pgsql.sh
Executable file
15
.travis/setup.pgsql.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
CURRENT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
||||
TRAVIS_BUILD_DIR="${TRAVIS_BUILD_DIR:-$(dirname $(dirname $CURRENT_DIR))}"
|
||||
|
||||
echo -e "Init PostgreSQL database..."
|
||||
|
||||
echo "127.0.0.1:5432:postgres:postgres:postgres" > ~/.pgpass
|
||||
chmod 600 ~/.pgpass
|
||||
|
||||
psql -h 127.0.0.1 -p 5432 -d postgres -U postgres -f .travis/pgsql.sql
|
||||
|
||||
echo -e "Done\n"
|
||||
|
||||
wait
|
@ -159,3 +159,4 @@ composer analyse
|
||||
- [#4920](https://github.com/hyperf/hyperf/pull/4920) Fixed bug that the routing path is wrong (like `//foo`) when the routing prefix is end of '/'.
|
||||
- [#4940](https://github.com/hyperf/hyperf/pull/4940) Fixed memory leak caused by an exception which occurred in `Parallel`.
|
||||
- [#5100](https://github.com/hyperf/hyperf/pull/5100) Fixed bug that the tag `continue` cannot work when using `view-engine`.
|
||||
- [#5121](https://github.com/hyperf/hyperf/pull/5121) Fixed bug that the SQL is not valid but the correct error message cannot be obtained when using `pgsql`.
|
||||
|
@ -106,7 +106,7 @@ class PostgreSqlSwooleExtConnection extends Connection
|
||||
|
||||
$result = $statement->execute($this->prepareBindings($bindings));
|
||||
|
||||
if ($result === false) {
|
||||
if ($result === false || ! empty($this->pdo->error)) {
|
||||
throw new QueryException($query, [], new \Exception($this->pdo->error));
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace HyperfTest\Database\PgSQL\Cases;
|
||||
|
||||
use Hyperf\Database\Connection;
|
||||
use Hyperf\Database\Connectors\ConnectionFactory;
|
||||
use Hyperf\Database\Exception\QueryException;
|
||||
use Hyperf\Database\PgSQL\Connectors\PostgresSqlSwooleExtConnector;
|
||||
use Hyperf\Database\PgSQL\PostgreSqlSwooleExtConnection;
|
||||
use Hyperf\Database\Query\Builder;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class PostgreSqlSwooleExtConnectionTest extends TestCase
|
||||
{
|
||||
protected ConnectionFactory $connectionFactory;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$container = \Mockery::mock(ContainerInterface::class);
|
||||
$container->shouldReceive('has')->andReturn(true);
|
||||
$container->shouldReceive('get')->with('db.connector.pgsql-swoole')->andReturn(new PostgresSqlSwooleExtConnector());
|
||||
|
||||
$this->connectionFactory = new ConnectionFactory($container);
|
||||
|
||||
Connection::resolverFor('pgsql-swoole', static function ($connection, $database, $prefix, $config) {
|
||||
return new PostgreSqlSwooleExtConnection($connection, $database, $prefix, $config);
|
||||
});
|
||||
}
|
||||
|
||||
public function testSelectMethodDuplicateKeyValueException()
|
||||
{
|
||||
if (SWOOLE_MAJOR_VERSION < 5) {
|
||||
$this->markTestSkipped('PostgreSql requires Swoole version >= 5.0.0');
|
||||
}
|
||||
|
||||
$connection = $this->connectionFactory->make([
|
||||
'driver' => 'pgsql-swoole',
|
||||
'host' => '127.0.0.1',
|
||||
'port' => 5432,
|
||||
'database' => 'postgres',
|
||||
'username' => 'postgres',
|
||||
'password' => 'postgres',
|
||||
]);
|
||||
|
||||
$builder = new Builder($connection);
|
||||
|
||||
$this->expectException(QueryException::class);
|
||||
$this->expectExceptionMessage('ERROR: duplicate key value violates unique constraint "users_email"');
|
||||
|
||||
$id = $builder->from('users')->insertGetId(['email' => 'test@hyperf.io', 'name' => 'hyperf'], 'id');
|
||||
$id2 = $builder->from('users')->insertGetId(['email' => 'test@hyperf.io', 'name' => 'hyperf'], 'id');
|
||||
|
||||
// Never here
|
||||
$this->assertIsNumeric($id);
|
||||
$this->assertIsNumeric($id2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user