-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-test.php
42 lines (38 loc) · 1.49 KB
/
db-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
use Amp\Postgres\PostgresConfig;
use Amp\Postgres\PostgresConnectionPool;
use function Amp\Redis\createRedisClient;
require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
if ($_ENV['DB'] === 'redis') {
echo "database is redis\n";
$def_host = gethostbyname('redis') !== 'redis' ? 'redis' : '127.0.0.1';
$redis = createRedisClient('tcp://' . $def_host);
echo $redis->echo('connecting successfully.');
$redis->ping();
} elseif ($_ENV['DB'] === 'postgres') {
echo "database is postgres\n";
$def_host = gethostbyname('postgres') !== 'postgres' ? 'postgres' : '127.0.0.1';
$config = new PostgresConfig(
$def_host,
user: $_ENV['DB_USERNAME'] ?? 'postgres',
password: $_ENV['DB_PASSWORD'] ?? 'postgres',
);
$postgresConnectionPool = new PostgresConnectionPool($config);
$res = $postgresConnectionPool->query('SELECT datname FROM pg_database;');
var_dump($res->fetchRow());
} elseif ($_ENV['DB'] === 'mysql') {
echo "database is mysql\n";
$def_host = gethostbyname('mysql') !== 'mysql' ? 'mysql' : '127.0.0.1';
$config = new \Amp\Mysql\MysqlConfig(
$def_host,
user: $_ENV['DB_USERNAME'] ?? 'mysql',
password: $_ENV['DB_PASSWORD'] ?? 'password',
);
$connectionPool = new \Amp\Mysql\MysqlConnectionPool($config);
$res = $connectionPool->query('show DATABASES;');
var_dump($res->fetchRow());
} else {
exit('env.DB is not supported!');
}