Skip to content

Commit

Permalink
Feature libpq (#168)
Browse files Browse the repository at this point in the history
* 解决扩展依赖扩展

* 添加启用单个扩展 例子 脚本

* fix script path error

* macos 默认启用bison库

* 改进

* fix error

* swoole 添加 libpq 可选库

* 调整github action

* macos build yaml 调整

* build yaml 调整

* 调整pgsql

* 准备测试 数据库 服务

* 更新github action yaml

* 更新设置CPU核数

* update github action config

* update README.md

* update README.md

* update README.md

* 更新macos 加载bison

* macos 检测 bison库调整

* add getPhpSrcDir

* update action config

* 构建环境获取ip地址

* 验证文件

* 验证文件

* 验证文件

* 验证文件

* fix 测试例子 运行环境

* update action config

* 修改扩展依赖扩展 功能

* 修改扩展依赖扩展 功能

* 验证

* 修改验证例子

* 添加验证例子

* 添加验证例子

* 添加验证例子

* 调整测试例子 文件位置

* 调整download box

* 调整快速进入构建环境的容器

* 调整快速进入构建环境的容器

* update action config

* change function dpends to withDependentLibraries

* update action config

* update action config

* update action config

* update action config

* update options.md

* update 原生构建

* update action config

* update pgsql dependent library method

* add test case

* 添加SWOOLE PGSQL 测试例子

* 验证 swoole pgsql 例子

* 验证 swoole pgsql 例子

* 验证 swoole pgsql 例子
  • Loading branch information
jingjingxyk authored Jun 20, 2023
1 parent 9edfa1c commit b9253c6
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux-aarch64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [ push, pull_request ]

jobs:
linux-aarch64:
if: 1
if: 0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
# composer update --no-dev --optimize-autoloader
composer update --optimize-autoloader
php prepare.php +inotify +apcu +ds +xlswriter +ssh2
php prepare.php --with-swoole-pgsql=1 +inotify +apcu +ds +xlswriter +ssh2
chmod a+x make.sh
head -n 20 make.sh
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/linux-x86_64-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [ push, pull_request ]

jobs:
linux-x86_64:
if: 1
if: 0
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
# composer update --no-dev --optimize-autoloader
composer update --optimize-autoloader
php prepare.php +inotify +apcu +ds +xlswriter +ssh2
php prepare.php --with-swoole-pgsql=1 +inotify +apcu +ds +xlswriter +ssh2
chmod a+x make.sh
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/linux-x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
git submodule update --init
# 准备数据库容器
bash sapi/src/UnitTest/scripts/database/start.sh
- name: Prepare Libraries and Extensions
run: |
set -x
Expand All @@ -54,7 +57,7 @@ jobs:
# composer update --no-dev --optimize-autoloader
composer update --optimize-autoloader
php prepare.php +inotify +apcu +ds +xlswriter +ssh2
php prepare.php --with-swoole-pgsql=1 +inotify +apcu +ds +xlswriter +ssh2
chmod a+x make.sh
head -n 20 make.sh
Expand All @@ -79,6 +82,7 @@ jobs:
./bin/swoole-cli -r "echo PHP_VERSION;"
./bin/swoole-cli ./vendor/bin/phpunit ./sapi/src/UnitTest/MainTest.php --list-tests
./bin/swoole-cli ./vendor/bin/phpunit ./sapi/src/UnitTest/MainTest.php
./bin/swoole-cli ./vendor/bin/phpunit ./sapi/src/UnitTest/SwoolePGSQLTest.php
- name: production artifacts
uses: actions/upload-artifact@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/macos-x86_64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [ push, pull_request ]

jobs:
macos-x86_64:
if: 1
if: 0
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
# composer update --no-dev --optimize-autoloader
composer update --optimize-autoloader
php prepare.php --without-docker=1 --with-build-type=release +apcu +ds +xlswriter +ssh2
php prepare.php --without-docker=1 --with-build-type=release --with-swoole-pgsql=1 +ds +apcu +xlswriter +ssh2
- name: Cache all-library
uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows-cygwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [ push, pull_request ]

jobs:
windows-cygwin:
if: 1
if: 0
runs-on: windows-latest
steps:
- name: Prepare git
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"require-dev": {
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "^3.15",
"squizlabs/php_codesniffer": "^3.7"
"squizlabs/php_codesniffer": "^3.7",
"swoole/ide-helper": "@dev"
},
"scripts": {
"test": "phpunit",
Expand Down
2 changes: 2 additions & 0 deletions docs/macOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ ln -s /opt/homebrew/bin/glibtoolize /opt/homebrew/bin/libtoolize
## 缺少`gettext coreutils re2c flex bison`

```shell

brew install gettext coreutils re2c libunistring flex bison

```

## curl configure 检测不通过
Expand Down
8 changes: 8 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ with-downloader
php ./prepare.php --with-downloader=wget
```

with-swoole-pgsql
----
swoole 启用 --enable-swoole-pgsql

```shell
php ./prepare.php --with-swoole-pgsql=1
```

with-parallel-jobs
----
构建时最大并发进程数;
Expand Down
1 change: 1 addition & 0 deletions sapi/quickstart/enable-single-extension-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ php prepare.php \
-yaml \
-imagick \
-mongodb \
--with-swoole-pgsql=1 \
+swoole

224 changes: 224 additions & 0 deletions sapi/src/UnitTest/SwoolePGSQLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php

declare(strict_types=1);

namespace SwooleCli\UnitTest;

use PHPUnit\Framework\TestCase;
use Swoole\Coroutine\PostgreSQL;

use function Swoole\Coroutine\run;

final class SwoolePGSQLTest extends TestCase
{
private $pg = null;
private $pg_master = null;

public function testSwoolePGSQL(): void
{
run(function () {
$this->createDataBase();
$this->createTable();
$this->insertTableData();
$this->selectTableData();
$this->deleteTableData();
$this->dropTable();
$this->dropDatabase();
});
}


public function createDataBase()
{

$pg = new PostgreSQL();
$conn = $pg->connect("host=127.0.0.1 port=5432 dbname=postgres user=postgres password=example");
if (!$conn) {
$this->assertNotTrue($conn, 'pgsql connection postgres error,error info :' . $pg->error);
return false;
}
$this->pg_master = $pg;
$stmt = $pg->query("SELECT * FROM pg_database WHERE datname = 'user_center'");
$arr = $stmt->fetchAssoc();
if (empty($arr)) {
$sql = <<<EOF
CREATE DATABASE user_center
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
IS_TEMPLATE = False
EOF;

$pg->query($sql);
$this->assertEquals(0, $pg->errCode, 'create database user_center sucess' . $pg->error);
}

}

public function createTable()
{
$pg = new PostgreSQL();

$conn = $pg->connect("host=127.0.0.1 port=5432 dbname=user_center user=postgres password=example");
if (!$conn) {
$this->assertNotTrue($conn, 'erro_info' . $pg->error);
return;
}
$this->pg = $pg;
$sql = "select * from pg_tables where schemaname = 'public' and tablename='users'";
$stmt = $pg->query($sql);
$res = $stmt->fetchAll();
if (empty($res)) {
# USER 是PGSQL 关键字,不能用作表名
$table = <<<'EOF'
CREATE TABLE users (
id bigint NOT NULL ,
username varchar(32) NOT NULL ,
nickname varchar(40) NOT NULL,
password varchar(255) NOT NULL ,
salt varchar(255) NOT NULL,
avatar varchar(255) DEFAULT '' ,
email varchar(100) DEFAULT NULL ,
mobile varchar(16) DEFAULT NULL ,
created_at TIMESTAMPTZ DEFAULT current_timestamp ,
updated_at TIMESTAMPTZ DEFAULT NULL ,
login_at TIMESTAMPTZ DEFAULT NULL ,
status SMALLINT DEFAULT 0 ,
login_channel SMALLINT DEFAULT NULL ,
PRIMARY KEY (id)
) ;
CREATE SEQUENCE users_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
alter table users alter column id set default nextval('users_id_seq');

EOF;
$pg->query($table);
$this->assertEquals(0, $pg->errCode, 'create table users sucess' . $pg->error);
}
}

public function insertTableData()
{
$password = 'example';
$salt = bin2hex(openssl_random_pseudo_bytes(rand(4, 20)));
$password = hash('sha256', $password, false) . $salt;
# ISO8601
$time = date('c', time());
$time = date('Y-m-d\TH:i:s.Z\Z', time());

$sql = <<<EOF
INSERT INTO public.users(
username, nickname, password, salt, avatar, email, mobile, updated_at, login_channel)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
EOF;
$list = [
[
"username1",
"example1",
"{$password}",
"{$salt}",
"https://wenda.swoole.com/dist/skin1/images/logo.png",
"[email protected]",
"861888888888",
"{$time}",
"1"
],
[
"username2",
"example2",
"{$password}",
"{$salt}",
"https://wenda.swoole.com/dist/skin1/images/logo.png",
"[email protected]",
"861888888888",
"{$time}",
"1"
]
];

echo $sql;

$stmt = $this->pg->prepare($sql);

$i = 100;
while ($i >= 1) {
foreach ($list as $data) {
$res = $stmt->execute($data);
}
$i--;
}
$this->assertGreaterThanOrEqual(1, $stmt->affectedRows(), 'insert data sucess' . $this->pg->error);
var_dump($stmt->affectedRows());
}

public function selectTableData()
{
$sql = <<<EOF
SELECT * FROM public.users
ORDER BY id ASC
EOF;

echo $sql;

$stmt = $this->pg->query($sql);
$list = $stmt->fetchAll();
$this->assertGreaterThan(1, count($list), 'select data' . $this->pg->error);
}

public function deleteTableData()
{
$sql = <<<'EOF'
DELETE FROM users
WHERE username=$1

EOF;

echo $sql;

$stmt = $this->pg->prepare($sql);

$stmt->execute(['username2']);

$this->assertGreaterThan(10, $stmt->affectedRows(), 'delete data' . $this->pg->error);
}

public function dropTable()
{
$sql = <<<'EOF'
DROP TABLE users ;
DROP SEQUENCE users_id_seq ;
EOF;

echo $sql;

$this->pg->query($sql);
$this->assertEquals(0, $this->pg->errCode, 'drop table users' . $this->pg->error);
}

public function dropDatabase()
{
$sql = <<<'EOF'
DROP DATABASE IF EXISTS user_center
EOF;

echo $sql;

$this->pg_master->query($sql);
echo PHP_EOL . $this->pg->error . PHP_EOL;
$this->assertEquals(0, $this->pg_master->errCode, 'drop database user_center' . $this->pg_master->error);
}
}
38 changes: 38 additions & 0 deletions sapi/src/UnitTest/scripts/database/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3"
services:
postgresql-server:
# image: postgres:15-alpine
image: postgis/postgis:15-3.3-alpine
hostname: "postgresql"
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=example"
# 容器内数据目录 /var/lib/postgresql/data
# auth_user: postgres
# auth_password: example
mysql-server:
# image: mysql:8-debian
image: mariadb:10.6.14-focal
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --tls-version=''
environment:
MYSQL_ROOT_PASSWORD: "example"
# MARIADB_USER: root
MARIADB_PASSWORD: "example"
MARIADB_ROOT_PASSWORD: "example"
ports:
- "3306:3306"
# 容器内数据目录 /var/lib/mysql
redis-server:
image: redis:7-alpine
ports:
- "6379:6379"
# 容器内数据目录 /data
mongodb-server:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- "27017:27017"
# 容器内数据目录 /data/db
Loading

0 comments on commit b9253c6

Please sign in to comment.