-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathMySQLTest.php
More file actions
88 lines (69 loc) · 2.26 KB
/
MySQLTest.php
File metadata and controls
88 lines (69 loc) · 2.26 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
86
87
88
<?php
namespace Tests\E2E\Adapter\SharedTables;
use Redis;
use Tests\E2E\Adapter\Base;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Database;
use Utopia\Database\PDO;
class MySQLTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;
// Remove once all methods are implemented
/**
* Return name of adapter
*
* @return string
*/
public static function getAdapterName(): string
{
return "mysql";
}
/**
* @return Database
*/
public static 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
->setDatabase('utopiaTests')
->setSharedTables(true)
->setTenant(999)
->setNamespace(static::$namespace = '');
if ($database->exists()) {
$database->delete();
}
$database->create();
self::$pdo = $pdo;
return self::$database = $database;
}
protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
self::$pdo->exec($sql);
return true;
}
protected static function deleteIndex(string $collection, string $index): bool
{
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
self::$pdo->exec($sql);
return true;
}
}