From 677c9da9bc07d21745fcdfbaffcf4817fdf424fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=B9=E5=92=8C=E5=A5=B9=E7=9A=84=E7=8C=AB?= Date: Tue, 11 Oct 2022 10:37:54 +0800 Subject: [PATCH] Add error checking for `PostgreSQLStatement::execute` method. (#5121) --- .github/workflows/test.yml | 6 +- .travis/pgsql.sql | 12 ++++ .travis/setup.pgsql.sh | 15 ++++ CHANGELOG-3.0.md | 1 + docs/zh-cn/awesome-components.md | 2 +- docs/zh-cn/release-planning.md | 2 +- .../src/PostgreSqlSwooleExtConnection.php | 2 +- .../PostgreSqlSwooleExtConnectionTest.php | 71 +++++++++++++++++++ 8 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 .travis/pgsql.sql create mode 100755 .travis/setup.pgsql.sh create mode 100644 src/database-pgsql/tests/Cases/PostgreSqlSwooleExtConnectionTest.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cf7de2f63..d83e3f1e7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -48,7 +49,7 @@ jobs: rm swoole.tar.gz cd swoole phpize - if [ _${{ matrix.sw-version }} = '_v5.0.0' ] || [ _${{ matrix.sw-version }} = '_master'] + if [ _${{ matrix.sw-version }} = '_v5.0.0' ] || [ _${{ matrix.sw-version }} = '_master' ] then ./configure --enable-openssl --enable-swoole-curl --enable-cares --enable-swoole-pgsql else @@ -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 diff --git a/.travis/pgsql.sql b/.travis/pgsql.sql new file mode 100644 index 000000000..a8663c86a --- /dev/null +++ b/.travis/pgsql.sql @@ -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'); diff --git a/.travis/setup.pgsql.sh b/.travis/setup.pgsql.sh new file mode 100755 index 000000000..0af542abb --- /dev/null +++ b/.travis/setup.pgsql.sh @@ -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 diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index 231079eff..029a5d2d3 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -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`. diff --git a/docs/zh-cn/awesome-components.md b/docs/zh-cn/awesome-components.md index deb86773d..d0f30c5bb 100644 --- a/docs/zh-cn/awesome-components.md +++ b/docs/zh-cn/awesome-components.md @@ -102,7 +102,7 @@ ## 文档生成 - [hyperf/swagger](https://github.com/hyperf/swagger) Hyperf 官方提供的 Swagger 文档自动生成组件 (beta) -- [tangwei/swagger](https://github.com/tw2066/api-docs) 一个基于PHP类型(DTO)自动生成swagger文档组件,启动自动扫描、自动生成路由(UI)、注解验证 +- [tangwei/swagger](https://github.com/tw2066/api-docs) 一个基于 PHP 类型(DTO)自动生成 swagger 文档组件,启动自动扫描、自动生成路由(UI)、注解验证 ## Graphql diff --git a/docs/zh-cn/release-planning.md b/docs/zh-cn/release-planning.md index 42c016a9f..31ddd3395 100644 --- a/docs/zh-cn/release-planning.md +++ b/docs/zh-cn/release-planning.md @@ -4,7 +4,7 @@ | 版本 | 状态 | 积极支持截止时间 | 安全维护截止时间 | 发布或预计发布时间 | | ---- |--------|------------|------------|------------| -| 3.0 | 研发中 (RC1已发布) | 2023-06-20 | 2023-12-31 | 2022-08-30~2022-09-30 | +| 3.0 | 研发中 (RC1 已发布) | 2023-06-20 | 2023-12-31 | 2022-08-30~2022-09-30 | | 2.2 | 积极支持中 | 2022-06-20 | 2022-12-31 | 2021-07-19 | | 2.1 | 停止维护 | 2021-06-30 | 2021-12-31 | 2020-12-28 | | 2.0 | 停止维护 | 2020-12-28 | 2021-06-30 | 2020-06-22 | diff --git a/src/database-pgsql/src/PostgreSqlSwooleExtConnection.php b/src/database-pgsql/src/PostgreSqlSwooleExtConnection.php index 72c38cbeb..6ab390c58 100644 --- a/src/database-pgsql/src/PostgreSqlSwooleExtConnection.php +++ b/src/database-pgsql/src/PostgreSqlSwooleExtConnection.php @@ -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)); } diff --git a/src/database-pgsql/tests/Cases/PostgreSqlSwooleExtConnectionTest.php b/src/database-pgsql/tests/Cases/PostgreSqlSwooleExtConnectionTest.php new file mode 100644 index 000000000..efa82c701 --- /dev/null +++ b/src/database-pgsql/tests/Cases/PostgreSqlSwooleExtConnectionTest.php @@ -0,0 +1,71 @@ +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); + } +}