-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathMySQLTest.php
More file actions
85 lines (67 loc) · 2.25 KB
/
MySQLTest.php
File metadata and controls
85 lines (67 loc) · 2.25 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Tests\E2E\Adapter;
use Redis;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Database;
use Utopia\Database\Exception;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Exception\Limit;
use Utopia\Database\PDO;
class MySQLTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;
/**
* @return Database
* @throws Duplicate
* @throws Exception
* @throws Limit
*/
public function getDatabase(): Database
{
if (!is_null(self::$database)) {
return self::$database;
}
$dbHost = 'mysql';
$dbPort = '3307';
$dbUser = 'root';
$dbPass = 'password';
$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MySQL::getPDOAttributes());
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
$database = new Database(new MySQL($pdo), $cache);
$database
->setAuthorization(self::$authorization)
->setDatabase('utopiaTests')
->setNamespace(static::$namespace = 'myapp_' . uniqid());
if ($database->exists()) {
$database->delete();
}
$database->create();
self::$pdo = $pdo;
return self::$database = $database;
}
protected function getPDO(): mixed
{
return self::$pdo;
}
protected function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
self::$pdo->exec($sql);
return true;
}
protected function deleteIndex(string $collection, string $index): bool
{
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
self::$pdo->exec($sql);
return true;
}
}