-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 解决扩展依赖扩展 * 添加启用单个扩展 例子 脚本 * 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
1 parent
9edfa1c
commit b9253c6
Showing
17 changed files
with
418 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,5 +52,6 @@ php prepare.php \ | |
-yaml \ | ||
-imagick \ | ||
-mongodb \ | ||
--with-swoole-pgsql=1 \ | ||
+swoole | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.